mkim/montyhallsecond

From ggc

Jump to: navigation, search

001 package mkim;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 
007 /**
008  * All about my game.
009  @author My Name Here
010  */
011 public class montyhallsecond extends Game
012 {
013   private ImageSprite door1, door2, door3;
014   private ImageSprite loser1, loser2;
015   private ImageSprite winner;
016   private StringSprite header, answer;
017   private StringSprite num1, num2, num3;
018   private StringSprite win, fail;
019   private int doorpicked;
020   private int stage = 0;
021   private int clickcount = 0;
022   private int q;
023   private int w;
024   private int e;
025   private int r;
026   private int open;
027   private int notopen;
028   /**sets up the game*/
029   public void setup()
030   {
031     clickcount=0;
032     makeSprite();
033     addSprite();
034   }
035   private void makeSprite()
036   {
037     door1=new ImageSprite("Door123444.jpg");
038     door1.setLocation(.2.5);
039     door1.setScale(.5);
040     door1.setVisible(true);
041 
042     door2=new ImageSprite("Door123444.jpg");
043     door2.setLocation(.5.5);
044     door2.setScale(.5);
045     door2.setVisible(true);
046 
047     door3=new ImageSprite("Door123444.jpg");
048     door3.setLocation(.8.5);
049     door3.setScale(.5);
050     door3.setVisible(true);
051 
052     loser1=new ImageSprite("Loser0000.JPG");
053     loser1.setLocation(.2.5);
054     loser1.setScale(.1);
055 
056     loser2=new ImageSprite("Loser0000.JPG");
057     loser2.setLocation(.5.5);
058     loser2.setScale(.1);
059 
060     winner=new ImageSprite("Win12333333.jpg");
061     winner.setLocation(.8.5);
062     winner.setScale(.1);
063 
064     header=new StringSprite("Choose a Door");
065     header.setLocation(.5.2);
066     header.setSize(.3);
067     canvas.addSprite(header);
068 
069     num1=new StringSprite("1");
070     num1.setLocation(.2.85);
071     num1.setSize(.15);
072 
073     num2=new StringSprite("2");
074     num2.setLocation(.5.85);
075     num2.setSize(.15);
076 
077     num3=new StringSprite("3");
078     num3.setLocation(.8.85);
079     num3.setSize(.15);
080 
081     answer=new StringSprite("Are You Sure?");
082     answer.setLocation(.5.1);
083     answer.setSize(.5);
084     answer.setVisible(false);
085     canvas.addSprite(answer);
086 
087     win=new StringSprite("You Win");
088     win.setLocation(.5.3);
089     win.setSize(.25);
090 
091     fail=new StringSprite("Failed");
092     fail.setLocation(.5.3);
093     fail.setSize(.25);
094   }
095   private void addSprite()
096   {
097     canvas.addSprite(door1);
098     canvas.addSprite(door2);
099     canvas.addSprite(door3);
100     canvas.addSprite(num1);
101     canvas.addSprite(num2);
102     canvas.addSprite(num3);
103   }
104   /**handle input and game events*/
105   public void advance()
106   {
107     if(stage==0)
108     {
109       Point2D.Double click=getPlayer().getMouse().getClickLocation();
110       if(click!=null && clickcount == 0)
111       {
112         if (door1.intersects(click))
113         {
114           clickcount=1;
115           q=random.nextInt(2);
116           doorpicked=1;
117           if(q==0)
118           {
119             loser1.setLocation(door2.getLocation());
120             canvas.removeSprite(door2);
121             addSprite(loser1);
122             open=2;
123             notopen=3;
124           }
125           if(q==1)
126           {
127             loser1.setLocation(door3.getLocation());
128             canvas.removeSprite(door3);
129             addSprite(loser1);
130             open=3;
131             notopen=2;
132           }
133           answer.setVisible(true);
134           header.setVisible(false);
135           stage=1;
136         }
137         if (door2.intersects(click))
138         {
139           clickcount=1;
140           w=random.nextInt(2);
141           doorpicked=2;
142           if(w==0)
143           {
144             loser1.setLocation(door1.getLocation());
145             canvas.removeSprite(door1);
146             addSprite(loser1);
147             open=1;
148             notopen=3;
149           }
150           if(w==1)
151           {
152             loser1.setLocation(door3.getLocation());
153             canvas.removeSprite(door3);
154             addSprite(loser1);
155             open=3;
156             notopen=1;
157           }
158           answer.setVisible(true);
159           header.setVisible(false);
160           stage=1;
161         }
162         if (door3.intersects(click))
163         {
164           clickcount=1;
165           e=random.nextInt(2);
166           doorpicked=3;
167           if(e==0)
168           {
169             loser1.setLocation(door1.getLocation());
170             canvas.removeSprite(door1);
171             addSprite(loser1);
172             open=1;
173             notopen=2;
174           }
175           if(e==1)
176           {
177             loser1.setLocation(door2.getLocation());
178             canvas.removeSprite(door2);
179             addSprite(loser1);
180             open=2;
181             notopen=1;
182           }
183           answer.setVisible(true);
184           header.setVisible(false);
185           stage=1;
186         }
187       }
188     }
189     else if(stage==1)
190     {
191       Point2D.Double click=getPlayer().getMouse().getClickLocation();
192       if(click!=null && clickcount == 1)
193       {
194         r=random.nextInt(2);
195         clickcount=2;
196         if(door1.intersects(click&& open == 2)
197         {
198           if(r==0)
199           {
200             winner.setLocation(door1.getLocation());
201             loser2.setLocation(door3.getLocation());
202             canvas.addSprite(win);
203           }
204           else if(r==1)
205           {
206             loser2.setLocation(door1.getLocation());
207             winner.setLocation(door3.getLocation());
208             canvas.addSprite(fail);
209           }
210           canvas.removeSprite(door1);
211           canvas.removeSprite(door2);
212           canvas.removeSprite(door3);
213           addSprite(loser2);
214           addSprite(winner);
215         }
216         if(door1.intersects(click&& open==3)
217         {
218           if(r==0)
219           {
220             winner.setLocation(door1.getLocation());
221             loser2.setLocation(door2.getLocation());
222             canvas.addSprite(win);
223           }
224           else if(r==1)
225           {
226             loser2.setLocation(door1.getLocation());
227             winner.setLocation(door2.getLocation());
228             canvas.addSprite(fail);
229           }
230           canvas.removeSprite(door1);
231           canvas.removeSprite(door2);
232           canvas.removeSprite(door3);
233           addSprite(loser2);
234           addSprite(winner);
235         }
236         if(door2.intersects(click&& open==1)
237         {
238           if(r==0)
239           {
240             winner.setLocation(door2.getLocation());
241             loser2.setLocation(door1.getLocation());
242             canvas.addSprite(win);
243           }
244           else if(r==1)
245           {
246             loser2.setLocation(door2.getLocation());
247             winner.setLocation(door1.getLocation());
248             canvas.addSprite(fail);
249           }
250           canvas.removeSprite(door1);
251           canvas.removeSprite(door2);
252           canvas.removeSprite(door3);
253           addSprite(loser2);
254           addSprite(winner);
255         }
256         if(door2.intersects(click&& open==3)
257         {
258           if(r==0)
259           {
260             winner.setLocation(door2.getLocation());
261             loser2.setLocation(door3.getLocation());
262             canvas.addSprite(win);
263           }
264           else if(r==1)
265           {
266             loser2.setLocation(door2.getLocation());
267             winner.setLocation(door3.getLocation());
268             canvas.addSprite(fail);
269           }
270           canvas.removeSprite(door1);
271           canvas.removeSprite(door2);
272           canvas.removeSprite(door3);
273           addSprite(loser2);
274           addSprite(winner);
275         }
276         if(door3.intersects(click&& open==1)
277         {
278           if(r==0)
279           {
280             winner.setLocation(door3.getLocation());
281             loser2.setLocation(door1.getLocation());
282             canvas.addSprite(win);
283           }
284           else if(r==1)
285           {
286             loser2.setLocation(door3.getLocation());
287             winner.setLocation(door1.getLocation());
288             canvas.addSprite(fail);
289           }
290           canvas.removeSprite(door1);
291           canvas.removeSprite(door2);
292           canvas.removeSprite(door3);
293           addSprite(loser2);
294           addSprite(winner);
295         }
296         if(door3.intersects(click&& open==2)
297         {
298           if(r==0)
299           {
300             winner.setLocation(door3.getLocation());
301             loser2.setLocation(door2.getLocation());
302             canvas.addSprite(win);
303           }
304           else if(r==1)
305           {
306             loser2.setLocation(door3.getLocation());
307             winner.setLocation(door2.getLocation());
308             canvas.addSprite(fail);
309           }
310           canvas.removeSprite(door1);
311           canvas.removeSprite(door2);
312           canvas.removeSprite(door3);
313           addSprite(loser2);
314           addSprite(winner);
315         }
316       }
317     }
318   }
319 }


Download/View mkim/montyhallsecond.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