Schwarz/Monty Hall

From ggc

Jump to: navigation, search

001 package Schwarz;
002 
003 import fang.*;
004 import java.awt.*;
005 import java.awt.geom.*;
006 
007 /**
008  * Creates a game where the user selects a door. Used Kevin Rohrer's project to help.
009  @author Drew Schwarz
010  */
011 public class Monty_Hall extends Game
012 {
013   ImageSprite prizedoor = new ImageSprite("Rohrer-Door.jpg");
014   ImageSprite faildoor = new ImageSprite("Rohrer-Door.jpg");
015   ImageSprite faildoor2 = new ImageSprite("Rohrer-Door.jpg");
016   ImageSprite prizeGood = new ImageSprite("Stack-o-money.jpg");
017   ImageSprite prizeBad1 = new ImageSprite("HDD_broken.JPG");
018   ImageSprite prizeBad2 = new ImageSprite("Broken_HDD.jpg");
019   StringSprite step1 = new StringSprite("Click a Door to Begin");
020   StringSprite step2 = new StringSprite("Choose to stay with your choice or change it");
021   StringSprite step3 = new StringSprite("Press the (B) key to begin again");
022   StringSprite win = new StringSprite("YOU'RE A WINNER!!!");
023   StringSprite lose = new StringSprite("FAILURE  (T-T) ");
024   StringSprite num = new StringSprite ("1    2    3");
025   private int numClicks, PlacePrize, RemoveDoors;
026 
027 
028   /**opens the game with instructions*/
029   public void setup()
030   {
031     StringSprite begin = new StringSprite("To Begin Click Start");
032     begin.setLocation(.5,.4);
033     begin.setSize(.75);
034     addSprite(begin);
035   }
036 
037   /** places the iformation for the player when they win the game*/
038   public void Win()
039   {
040     prizedoor.setVisible(false);
041     faildoor.setVisible(false);
042     faildoor2.setVisible(false);
043     step2.setVisible(false);
044     win.setSize(.5);
045     win.setLocation(.5.65);
046     addSprite(win);
047     win.setVisible(true);
048     step3.setVisible(true);
049     num.setVisible(false);
050   }
051 
052   /**places the information for the player when the loos the game*/
053   public void Lose()
054   {
055     prizedoor.setVisible(false);
056     faildoor.setVisible(false);
057     faildoor2.setVisible(false);
058     step2.setVisible(false);
059     lose.setLocation(.5.65);
060     lose.setSize(.5);
061     addSprite(lose);
062     lose.setVisible(true);
063     step3.setVisible(true);
064     num.setVisible(false);
065   }
066 
067   /** creates the winning door with the prize behind it*/
068   public void WiningDoor(double x, double y)
069   {
070     prizeGood.setLocation(x, .5);
071     prizeGood.setSize(.2);
072     addSprite(prizeGood);
073     prizeGood.setVisible(true);
074 
075     prizedoor.setLocation(y,.5);
076     prizedoor.setSize(.5);
077     addSprite(prizedoor);
078     prizedoor.setVisible(true);
079   }
080 
081   /**creates the first loosing door*/
082   public void LosingDoor(double x, double y)
083   {
084     prizeBad1.setLocation(x, .5);
085     prizeBad1.setSize(.2);
086     addSprite(prizeBad1);
087     prizeBad1.setVisible(true);
088 
089     faildoor.setLocation(y,.5);
090     faildoor.setSize(.5);
091     addSprite(faildoor);
092     faildoor.setVisible(true);
093   }
094 
095   /** creates the second loosing door*/
096   public void LosingDoor2(double x, double y)
097   {
098     prizeBad2.setLocation(x,.5);
099     prizeBad2.setSize(.2);
100     addSprite(prizeBad2);
101     prizeBad2.setVisible(true);
102 
103     faildoor2.setLocation(y,.5);
104     faildoor2.setSize(.5);
105     addSprite(faildoor2);
106     faildoor2.setVisible(true);
107   }
108 
109   /** Places the doors on the program */
110   public void AddDoors()
111   {
112     if (PlacePrize==1)
113     {
114       WiningDoor(.8,.8);
115       LosingDoor(.5,.5);
116       LosingDoor2(.2,.2);
117     }
118     else if (PlacePrize==2)
119     {
120       WiningDoor(.5,.5);
121       LosingDoor(.2,.2);
122       LosingDoor2(.8,.8);
123     }
124     else if (PlacePrize==3)
125     {
126       WiningDoor(.2,.2);
127       LosingDoor(.8,.8);
128       LosingDoor2(.5,.5);
129     }
130   }
131   /**removes door that is not chosen and is not the prize apon first door choice*/
132   public void RemoveDoor()
133   {
134 
135     if (getClick2D()!= null && prizeGood.intersects(getClick2D()))
136     {
137       RemoveDoors = random.nextInt(2)+1;
138       if(RemoveDoors == 1)
139       {
140         faildoor.setVisible(false);
141       }
142       else
143       {
144         faildoor2.setVisible(false);
145       }
146     }
147     else if (getClick2D()!= null && faildoor.intersects(getClick2D()))
148     {
149       faildoor2.setVisible(false);
150     }
151     else if(getClick2D()!= null && faildoor2.intersects(getClick2D()))
152     {
153       faildoor.setVisible(false);
154     }
155   }
156 
157   /**handle input and game events*/
158   public void Intro()
159   {
160     step1.setLocation(.5,.15);
161     step1.setSize(.8);
162     addSprite(step1);
163     step1.setVisible(true);
164 
165     step2.setLocation(.5,.15);
166     step2.setSize(.8);
167     addSprite(step2);
168     step2.setVisible(false);
169 
170     step3.setLocation(.5,.15);
171     step3.setSize(.8);
172     addSprite(step3);
173     step3.setVisible(false);
174 
175     num.setLocation(.5,.8);
176     num.setSize(.7);
177     addSprite(num);
178     num.setVisible(false);
179   }
180   /** creates the user interface*/
181   public void advance()
182   {
183     if(   (getClick2D()!= null && prizeGood.intersects(getClick2D()))
184             || (getClick2D()!= null && faildoor.intersects(getClick2D()))
185             || (getClick2D()!= null && faildoor2.intersects(getClick2D())))
186     {
187       numClicks++;
188     }
189 
190 
191     if(numClicks==0)
192     {
193       PlacePrize = random.nextInt(3)+1;
194       removeAllSprites();
195       Intro();
196       AddDoors();
197       num.setVisible(true);
198     }
199     if(numClicks==1)
200     {
201       step1.setVisible(false);
202       step2.setVisible(true);
203       RemoveDoor();
204     }
205     if(numClicks==2)
206     {
207       step1.setVisible(false);
208       if(getClick2D()!= null && prizeGood.intersects(getClick2D()))
209       {
210         Win();
211       }
212       else if(  (getClick2D()!= null && faildoor.intersects(getClick2D()))
213                 || (getClick2D()!= null && faildoor2.intersects(getClick2D())))
214       {
215         Lose();
216       }
217     }
218 
219 
220     if(getKeyPressed()=='b' && numClicks >= 2)
221     {
222       numClicks = 0;
223     }
224 
225 
226 
227 
228   }
229 }


Download/View Schwarz/Monty_Hall.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