|
001 packagepackage is used to name the directory or folder a class is in scg.ch05;
002
003 importimport means to make the classes and/or packages available in this program fang2.core.Game;
004 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
005
006 /**
007 * Simplified craps game: Player wagers 1 matchstick per round. 7/11 on
008 * roll out wins; elseelse is what happens when the if condition is false 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 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects EasyDice
014 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
015 /** bank balance; game ends when thisthis means the current object (the implicit parameter) goes to 0 after a bet */
016 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer bank;// value
017 privateprivate is used to restrict access to the current class only StringSprite bankDisplay;// display
018
019 /** player's current bet */
020 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer bet;// value
021 privateprivate is used to restrict access to the current class only StringSprite betDisplay;// display
022
023 /** the button */
024 privateprivate is used to restrict access to the current class only EasyButton button;
025
026 /** the left die */
027 privateprivate is used to restrict access to the current class only OneDie dieLeft;
028
029 /** the right die */
030 privateprivate is used to restrict access to the current class only OneDie dieRight;
031
032 /** the value the player is trying to match */
033 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer point;
034
035 /** the left point die */
036 privateprivate is used to restrict access to the current class only OneDie pointDieLeft;
037
038 /** the right point die */
039 privateprivate is used to restrict access to the current class only OneDie pointDieRight;
040
041 /** is there an active bet? */
042 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false rolling;
043
044 /**
045 * Advance one frame: either waiting forfor is a looping structure for repeatedly executing a block of code player to bet or waiting forfor is a looping structure for repeatedly executing a block of code
046 * player to roll. Determined by rolling flag.
047 *
048 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT time since last advance; used forfor is a looping structure for repeatedly executing a block of code animation
049 */
050 @Override
051 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
052 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueisGameOver()) {open braces start code blocks and must be matched with a close brace
053 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to truerolling) {open braces start code blocks and must be matched with a close brace
054 advanceWaiting(dT);
055 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
056 advanceRolling(dT);
057 }close braces end code blocks and must match an earlier open brace
058 }close braces end code blocks and must match an earlier open brace
059 }close braces end code blocks and must match an earlier open brace
060
061 /**
062 * Set up the game forfor is a looping structure for repeatedly executing a block of code play. Initializes all of the sprites (either
063 * here or in other setup functions).
064 */
065 @Override
066 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
067 setBackground(getColor("green"));
068 rolling =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
069 buttonSetup();
070 bankSetup();
071 betSetup();
072 diceSetup();
073 }close braces end code blocks and must match an earlier open brace
074
075 /**
076 * Advance one rolling frame. If button pressed, roll dice and handle
077 * non-first roll.
078 *
079 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT time since last advance; used forfor is a looping structure for repeatedly executing a block of code animation
080 */
081 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value advanceRolling(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
082 ifif executes the next statement only if the condition in parenthesis evaluates to true (button.isPressed()) {open braces start code blocks and must be matched with a close brace
083 intint is the type for whole numbers and it is short for integer roll =this assignment operator makes the left side equal to the right side rollTheDice();
084 // game wins, loses, or keeps going. Nothing to do to keep going
085 ifif executes the next statement only if the condition in parenthesis evaluates to true (roll ==this is the comparison operator which evaluates to true if both sides are the same 7) {open braces start code blocks and must be matched with a close brace// lose
086 lose();
087 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (roll ==this is the comparison operator which evaluates to true if both sides are the same point) {open braces start code blocks and must be matched with a close brace// win
088 win();
089 }close braces end code blocks and must match an earlier open brace
090 }close braces end code blocks and must match an earlier open brace
091 }close braces end code blocks and must match an earlier open brace
092
093 /**
094 * Advance one waiting frame. If button pressed, make wager, roll
095 * dice, handle first roll.
096 *
097 * @paramthis is the Javadoc tag for documenting the purpose of parameters secondsSinceLastCall time since last advance; used forfor is a looping structure for repeatedly executing a block of code
098 * animation
099 */
100 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value advanceWaiting(doubledouble is the type for numbers that can contain decimal fractions secondsSinceLastCall) {open braces start code blocks and must be matched with a close brace
101 ifif executes the next statement only if the condition in parenthesis evaluates to true (button.isPressed()) {open braces start code blocks and must be matched with a close brace
102 // place and show wager
103 bet =this assignment operator makes the left side equal to the right side 1;
104 bank =this assignment operator makes the left side equal to the right side bank - bet;
105 betDisplay.setText("Bet: " +adds two numbers together or concatenates Strings together bet);
106 bankDisplay.setText("Bank: " +adds two numbers together or concatenates Strings together bank);
107
108 intint is the type for whole numbers and it is short for integer roll =this assignment operator makes the left side equal to the right side rollTheDice();
109
110 // check for a win on the first roll
111 ifif executes the next statement only if the condition in parenthesis evaluates to true ((roll ==this is the comparison operator which evaluates to true if both sides are the same 7) ||this is boolean or, meaning if either or both are true then the result is true (roll ==this is the comparison operator which evaluates to true if both sides are the same 11)) {open braces start code blocks and must be matched with a close brace
112 win();
113 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
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 =this assignment operator makes the left side equal to the right side roll;
120 rolling =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
121 button.setText("Roll to match point");
122 }close braces end code blocks and must match an earlier open brace
123 }close braces end code blocks and must match an earlier open brace
124 }close braces end code blocks and must match an earlier open brace
125
126 /**
127 * Initialize match pile and display sprite
128 */
129 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value bankSetup() {open braces start code blocks and must be matched with a close brace
130 bank =this assignment operator makes the left side equal to the right side 10;// initial value for the bank in matchsticks
131 bankDisplay =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
132 bankDisplay.setLineHeight(0.10);
133 bankDisplay.leftJustify();
134 bankDisplay.setLocation(0.0, 0.1);
135 bankDisplay.setColor(getColor("white"));
136 addSprite(bankDisplay);
137
138 bankDisplay.setText("Matchsticks: " +adds two numbers together or concatenates Strings together bank);
139 }close braces end code blocks and must match an earlier open brace
140
141 /**
142 * Initialize wager and display sprite.
143 */
144 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value betSetup() {open braces start code blocks and must be matched with a close brace
145 bet =this assignment operator makes the left side equal to the right side 0;
146 betDisplay =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
147 betDisplay.setLineHeight(0.05);
148 betDisplay.rightJustify();
149 betDisplay.setLocation(1.0, 0.2);
150 betDisplay.setColor(getColor("white"));
151 addSprite(betDisplay);
152
153 betDisplay.setText("Bet: " +adds two numbers together or concatenates Strings together bet);
154 }close braces end code blocks and must match an earlier open brace
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 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value buttonSetup() {open braces start code blocks and must be matched with a close brace
162 button =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor EasyButton();
163 button.setScale(0.5);
164 button.setLocation(0.5, 0.85);
165 button.setColor(getColor("red"));
166 button.setTextColor(getColor("white"));
167 addSprite(button);
168
169 button.setText("Bet 1 matchstick");
170 }close braces end code blocks and must match an earlier open brace
171
172 /**
173 * Create and position four dice, two large and two small.
174 */
175 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value diceSetup() {open braces start code blocks and must be matched with a close brace
176 dieLeft =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OneDie();
177 dieLeft.setScale(0.33);
178 dieLeft.setLocation(5.0 / 18.0, 0.6);
179 addSprite(dieLeft);
180
181 dieRight =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OneDie();
182 dieRight.setScale(0.33);
183 dieRight.setLocation(13.0 / 18.0, 0.6);
184 addSprite(dieRight);
185
186 pointDieLeft =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OneDie();
187 pointDieLeft.setScale(0.1);
188 pointDieLeft.setLocation(0.75, 0.1);
189 addSprite(pointDieLeft);
190
191 pointDieRight =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OneDie();
192 pointDieRight.setScale(0.1);
193 pointDieRight.setLocation(0.9, 0.1);
194 addSprite(pointDieRight);
195 }close braces end code blocks and must match an earlier open brace
196
197 /**
198 * Post rolling clean-up. Here to avoid repeating in win() and lose()
199 */
200 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value finishRolling() {open braces start code blocks and must be matched with a close brace
201 bet =this assignment operator makes the left side equal to the right side 0;
202 bankDisplay.setText("Bank: " +adds two numbers together or concatenates Strings together bank);
203 betDisplay.setText("Bet: " +adds two numbers together or concatenates Strings together bet);
204
205 rolling =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
206 button.setText("Bet 1 matchstick");
207 }close braces end code blocks and must match an earlier open brace
208
209 /**
210 * Player lost wager; bank remains unchanged, finish rolling.
211 */
212 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value lose() {open braces start code blocks and must be matched with a close brace
213 ifif executes the next statement only if the condition in parenthesis evaluates to true (bank ==this is the comparison operator which evaluates to true if both sides are the same 0) {open braces start code blocks and must be matched with a close brace
214 // if out of matches, game is over
215 setGameOver(truetrue is the boolean value that is the opposite of false);
216 }close braces end code blocks and must match an earlier open brace
217 finishRolling();
218 }close braces end code blocks and must match an earlier open brace
219
220 /**
221 * Roll the game dice. Return the sum of the pair of dice.
222 *
223 * @returnnull the sum of the newly rolled dice
224 */
225 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer rollTheDice() {open braces start code blocks and must be matched with a close brace
226 dieRight.roll();
227 dieLeft.roll();
228 returnreturn means to provide the result of the method and/or cease execution of the method immediately dieLeft.getFace() +adds two numbers together or concatenates Strings together dieRight.getFace();
229 }close braces end code blocks and must match an earlier open brace
230
231 /**
232 * Player won wager; returnreturn means to provide the result of the method and/or cease execution of the method immediately twice wager to bank finish rolling.
233 */
234 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value win() {open braces start code blocks and must be matched with a close brace
235 bank =this assignment operator makes the left side equal to the right side bank +adds two numbers together or concatenates Strings together (2 * bet);
236 finishRolling();
237 }close braces end code blocks and must match an earlier open brace
238 }close braces end code blocks and must match an earlier open brace
239
240 //Uploaded on Mon Mar 29 21:40:03 EDT 2010
|