FA/Assignment5 Monty

From ggc

Jump to: navigation, search

Frank,

The problem is on line 170. It appears you are dividing by zero. Make sure the denominator on this line is not zero before dividing.

-Jam


001 package FA;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 import wiki.Wiki;
007 
008 /**
009  * The Monty Hall game.
010  * @ Frank Anderson
011  */
012 public class Assignment5_Monty extends Game
013 {// Begin Main
014 
015   private RectangleSprite door[];
016   private OvalSprite dot[];
017   private StringSprite label[];
018   private StringSprite mes;
019   private StringSprite mes1;
020   private StringSprite instructions;
021   private int otherDoor ;
022   private int firstDoor ;
023   private int prize;
024   private int numClicks;
025 
026 
027   /**sets up the game*/
028   public void setup()
029   {//begin setup()
030 
031     /*Initialize variables. */
032     door = new RectangleSprite[4];
033     dot = new OvalSprite[4];
034     label = new StringSprite[4];
035     prize = random.nextInt(3)+1// prize is a random number between 1 and 3
036 
037     numClicks = 1;
038 
039 
040 
041 
042     /*Display instruction to start the game at the bottom area of the screen. */
043     instructions = new StringSprite("Click the 'START' button to begin the game.");
044     instructions.setSize(.8);
045     instructions.setLocation(.5,.9);
046     addSprite(instructions);
047 
048     forint = 1; i <= 3; i++)
049     {// begin making dots (prizes)
050       dot[i] = new OvalSprite(1,2);
051       dot[i].setColor(getColor("green"));
052       if (== prize)
053         dot[i].setColor(getColor("yellow"));
054       dot[i].setSize(.1);
055       dot[i].setLocation(i*1.0/4.4);
056       dot[i].setVisible(true);
057       addSprite(dot[i]);
058     } // end of displaying dots
059 
060     for (int = 1; i <= 3; i++)
061     {// begin for   display the doors
062       door[i]=new RectangleSprite(1,2);
063       door[i].setColor(getColor("red"));
064       door[i].setSize(.35);
065       door[i].setLocation(i*1.0/4.5);
066       door[i].setVisible(true);
067       addSprite(door[i]);
068 
069       label[i] = new StringSprite("door "+i);
070       label[i].setSize(.1);
071       label[i].setLocation(i*1.0/4.45);
072       label[i].setVisible(true);
073       addSprite(label[i]);
074     } // end for dispaying doors
075 
076 
077 
078   }// end setup
079 
080   /* Display the wining message. */
081   private void winMes()
082   {
083     // begin winMes
084     mes = new StringSprite("YOU WIN !!");
085     mes.setLocation(.5,.5);
086     mes.setSize(.8);
087     mes.setVisible(true);
088     addSprite(mes);
089   }
090   // end winMes
091 
092   /*Display the losing message*/
093   private void loseMes()
094   { // begin loseMes
095     StringSprite mes = new StringSprite("You lose. Never gamble!!");
096     mes.setLocation(.5,.5);
097     mes.setSize(.8);
098     mes.setVisible(true);
099     addSprite(mes);
100   } // end loseMes
101 
102 
103   /*Determine which door is being clicked*/
104   private int whichDoor()
105   // begin whichDoor()
106   {
107     int dr;
108     dr=0;
109     for (int = 1; i <=3; i++)
110     {// start for.
111       if(getClick2D() != null &&   door[i].intersects(getClick2D()))
112       { // open  if
113         dr = i;
114       } // end  if
115     } //end for
116 
117     return dr; // Returns the number of the door clicked.
118   }// ends whichDoor()
119 
120   /**handle input and game events*/
121   public void advance()
122   {// open advanced
123     if (numClicks == 1)
124     {
125       /*turn off the START instruction. */
126       instructions.setVisible(false);
127 
128       /* Instructions for selecting a door.*/
129       mes1 = new StringSprite("Click on your first choice..");
130       mes1.setLocation(.5,.8);
131       mes1.setColor(getColor("blue"));
132       mes1.setSize(.7);
133       mes1.setVisible(true);
134       addSprite(mes1);
135 
136       /*Calls the method which checks for a mouse click and returns the door clicked. */
137       firstDoor = whichDoor();
138       if(firstDoor>=&& firstDoor<=3)
139       {  //open  if    fc gte 1 and lte 3 and numClicks = 1
140         numClicks = 2;
141         // begin using firstDoor
142         instructions.setVisible(false);  // remove 'click START' message.
143 
144         /* if firstDoor = prize, display the winning message and open other two doors,
145         else open the other non wining doors as well as the winning door.*/
146         if(firstDoor == prize )
147         {
148           /* open if fd = prize; remove doors and lables; display win message */
149           winMes();
150           for (int = 1; k <= 3; k++// open all doors
151           {
152             door[k].setVisible(false);
153             label[k].setVisible(false);
154           }
155         }  // close if  fd = prize
156         else
157           // open the other non-winning door if the first door is not a winner.
158         {//begin else
159           otherDoor = 6/(firstDoor*prize);
160           door[otherDoor].setVisible(false);
161           mes1.setVisible(false)// turn off 'choose any door' message
162           // display 'Click next door'
163           StringSprite door2 = new StringSprite("Choose a second door.");
164           door2.setSize(.9);
165           door2.setColor(getColor("white"));
166           door2.setLocation(.5,.8);
167           door2.setVisible(true);
168           addSprite(door2);
169         }// end else
170 
171       }// end fd gte 1 and lte 3
172 
173     }// end numclicks 1
174 
175     /* Check to see which door was clicked the second time.*/
176     else if(numClicks ==&& firstDoor != prize)
177     {// open num clicks 2
178       /*Check to see which door was chosen the second time. */
179       otherDoor = whichDoor();
180       if (otherDoor >=&& otherDoor <=3)
181       {  // checking value of otherDoor
182         /*Open both remaining doors */
183         door[otherDoor].setVisible(false); label[otherDoor].setVisible(false);
184         door[prize].setVisible(false); label[prize].setVisible(false);
185         /*Check to see if otherDoor is the winning and display "You win".*/
186         if(otherDoor == prize// prize = 2
187         {// begin  if    you win
188           for (int = 1; k <= 3; k++// open all doors
189           {
190             door[k].setVisible(false);
191             label[k].setVisible(false);
192           }
193           winMes();
194         }// end if    you win
195 
196         else
197           /*Display "you lose" if a non-winning door for second choice*/
198         { // open else for numClicks = 2
199           for (int = 1; k <= 3; k++// open all doors
200           {
201             door[k].setVisible(false);
202             label[k].setVisible(false);
203           }
204 
205           loseMes();
206         } // end else for numClicks = 2
207       } // end of checking value of otherDoor
208     } // numclicks = 2
209   }// end advanced()
210 }//end main


Download/View FA/Assignment5_Monty.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter