scg/ch05/EasyDice

From FANG

Jump to: navigation, search

001 package scg.ch05;
002 
003 import fang2.core.Game;
004 import fang2.sprites.StringSprite;
005 
006 /**
007  * Simplified craps game: Player wagers 1 matchstick per round. 7/11 on
008  * roll out wins; else roll out sets point. Subsequent rolls in round
009  * lose on 7, win on point, push on other. There are four dice on
010  * screen: the rolling pair and a pair showing the point when it was set
011  * during the first roll.
012  */
013 public class EasyDice
014   extends Game {
015   /** bank balance; game ends when this goes to 0 after a bet */
016   private int bank;// value
017   private StringSprite bankDisplay;// display
018 
019   /** player's current bet */
020   private int bet;// value
021   private StringSprite betDisplay;// display
022 
023   /** the button */
024   private EasyButton button;
025 
026   /** the left die */
027   private OneDie dieLeft;
028 
029   /** the right die */
030   private OneDie dieRight;
031 
032   /** the value the player is trying to match */
033   private int point;
034 
035   /** the left point die */
036   private OneDie pointDieLeft;
037 
038   /** the right point die */
039   private OneDie pointDieRight;
040 
041   /** is there an active bet? */
042   private boolean rolling;
043 
044   /**
045    * Advance one frame: either waiting for player to bet or waiting for
046    * player to roll. Determined by rolling flag.
047    *
048    @param  dT  time since last advance; used for animation
049    */
050   @Override
051   public void advance(double dT{
052     if (!isGameOver()) {
053       if (!rolling{
054         advanceWaiting(dT);
055       } else {
056         advanceRolling(dT);
057       }
058     }
059   }
060   
061   /**
062    Set up the game for play. Initializes all of the sprites (either
063    * here or in other setup functions).
064    */
065   @Override
066   public void setup() {
067     setBackground(getColor("green"));
068     rolling = false;
069     buttonSetup();
070     bankSetup();
071     betSetup();
072     diceSetup();
073   }
074 
075   /**
076    * Advance one rolling frame. If button pressed, roll dice and handle
077    * non-first roll.
078    *
079    @param  dT  time since last advance; used for animation
080    */
081   private void advanceRolling(double dT{
082     if (button.isPressed()) {
083       int roll = rollTheDice();
084       // game wins, loses, or keeps going. Nothing to do to keep going
085       if (roll == 7{// lose
086         lose();
087       } else if (roll == point{// win
088         win();
089       }
090     }
091   }
092 
093   /**
094    * Advance one waiting frame. If button pressed, make wager, roll
095    * dice, handle first roll.
096    *
097    @param  secondsSinceLastCall  time since last advance; used for
098    *                               animation
099    */
100   private void advanceWaiting(double secondsSinceLastCall{
101     if (button.isPressed()) {
102       // place and show wager
103       bet = 1;
104       bank = bank - bet;
105       betDisplay.setText("Bet: " + bet);
106       bankDisplay.setText("Bank: " + bank);
107 
108       int roll = rollTheDice();
109 
110       // check for a win on the first roll
111       if ((roll == 7|| (roll == 11)) {
112         win();
113       } else {
114         // copy roll dice up to the point
115         pointDieLeft.setFace(dieLeft.getFace());
116         pointDieRight.setFace(dieRight.getFace());
117 
118         // set new point, set rolling flag, and change button text
119         point = roll;
120         rolling = true;
121         button.setText("Roll to match point");
122       }
123     }
124   }
125 
126   /**
127    * Initialize match pile and display sprite
128    */
129   private void bankSetup() {
130     bank = 10;// initial value for the bank in matchsticks
131     bankDisplay = new StringSprite();
132     bankDisplay.setLineHeight(0.10);
133     bankDisplay.leftJustify();
134     bankDisplay.setLocation(0.00.1);
135     bankDisplay.setColor(getColor("white"));
136     addSprite(bankDisplay);
137 
138     bankDisplay.setText("Matchsticks: " + bank);
139   }
140 
141   /**
142    * Initialize wager and display sprite.
143    */
144   private void betSetup() {
145     bet = 0;
146     betDisplay = new StringSprite();
147     betDisplay.setLineHeight(0.05);
148     betDisplay.rightJustify();
149     betDisplay.setLocation(1.00.2);
150     betDisplay.setColor(getColor("white"));
151     addSprite(betDisplay);
152 
153     betDisplay.setText("Bet: " + bet);
154   }
155 
156   /**
157    * Create the button (at the bottom center of the screen). The fields
158    * button and message are initialized (and the text of message can
159    * subsequently be changed to change the function of the button)
160    */
161   private void buttonSetup() {
162     button = new EasyButton();
163     button.setScale(0.5);
164     button.setLocation(0.50.85);
165     button.setColor(getColor("red"));
166     button.setTextColor(getColor("white"));
167     addSprite(button);
168 
169     button.setText("Bet 1 matchstick");
170   }
171 
172   /**
173    * Create and position four dice, two large and two small.
174    */
175   private void diceSetup() {
176     dieLeft = new OneDie();
177     dieLeft.setScale(0.33);
178     dieLeft.setLocation(5.0 18.00.6);
179     addSprite(dieLeft);
180 
181     dieRight = new OneDie();
182     dieRight.setScale(0.33);
183     dieRight.setLocation(13.0 18.00.6);
184     addSprite(dieRight);
185 
186     pointDieLeft = new OneDie();
187     pointDieLeft.setScale(0.1);
188     pointDieLeft.setLocation(0.750.1);
189     addSprite(pointDieLeft);
190 
191     pointDieRight = new OneDie();
192     pointDieRight.setScale(0.1);
193     pointDieRight.setLocation(0.90.1);
194     addSprite(pointDieRight);
195   }
196 
197   /**
198    * Post rolling clean-up. Here to avoid repeating in win() and lose()
199    */
200   private void finishRolling() {
201     bet = 0;
202     bankDisplay.setText("Bank: " + bank);
203     betDisplay.setText("Bet: " + bet);
204 
205     rolling = false;
206     button.setText("Bet 1 matchstick");
207   }
208 
209   /**
210    * Player lost wager; bank remains unchanged, finish rolling.
211    */
212   private void lose() {
213     if (bank == 0{
214       // if out of matches, game is over
215       setGameOver(true);
216     }
217     finishRolling();
218   }
219 
220   /**
221    * Roll the game dice. Return the sum of the pair of dice.
222    *
223    @return  the sum of the newly rolled dice
224    */
225   private int rollTheDice() {
226     dieRight.roll();
227     dieLeft.roll();
228     return dieLeft.getFace() + dieRight.getFace();
229   }
230 
231   /**
232    * Player won wager; return twice wager to bank finish rolling.
233    */
234   private void win() {
235     bank = bank + (* bet);
236     finishRolling();
237   }
238 }
239 
240 //Uploaded on Mon Mar 29 21:40:03 EDT 2010


Download/View scg/ch05/EasyDice.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

Games
Games