|
001 packagepackage is used to name the directory or folder a class is in scg.ch10;
002
003 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
004 importimport means to make the classes and/or packages available in this program java.util.Scanner;
005
006 /**
007 * Multiple players (at a single computer) can play Pig. Pig is a dice
008 * game. Players take turns rolling one die. They keep track of the sum
009 * of the rolls until they add it to their total (and pass the die) or
010 * the roll a 1 (a Pig) in which casecase is used with switch for multiple alternatives (like if/else if...) they lose the points and the die.
011 * First player to 100 wins.
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 Pig {open braces start code blocks and must be matched with a close brace
014 /** score to which _all_ players play */
015 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer GAME_SCORE =this assignment operator makes the left side equal to the right side 100;
016
017 /** scanner wrapped around standard input */
018 staticstatic means that an instance is not required for access (class level access) Scanner keyboard =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(System.in);
019
020 /** all of the players with names and scores */
021 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<Player> standings;
022
023 /**
024 * Construct a newnew is used to create objects by calling the constructor instance of the {open braces start code blocks and must be matched with a close brace@link Pig}close braces end code blocks and must match an earlier open brace game. The game has a
025 * list of {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open braces which is filled in in the {open braces start code blocks and must be matched with a close brace@link
026 * #getPlayersNames()}close braces end code blocks and must match an earlier open brace method.
027 */
028 privateprivate is used to restrict access to the current class only Pig() {open braces start code blocks and must be matched with a close brace
029 standings =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Player>();
030 }close braces end code blocks and must match an earlier open brace
031
032 /**
033 * Print the prompt on the standard output until the user answers
034 * either 'y', 'n', 'yes', or 'no'.
035 *
036 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt to show the user
037 *
038 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true user answered 'y' or 'yes'; falsefalse is a value for the boolean type and means not true otherwise
039 */
040 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) booleanboolean is a type that is either true or false answersYes(String prompt) {open braces start code blocks and must be matched with a close brace
041 String userAnswer =this assignment operator makes the left side equal to the right side "";
042 // sentinel: userAnswer is a valid answer
043 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("y") &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&)
044 !this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("n") &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&)
045 !this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("yes") &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&)
046 !this is the not operator, which changes true to false and false to trueuserAnswer.equalsIgnoreCase("no")) {open braces start code blocks and must be matched with a close brace
047 userAnswer =this assignment operator makes the left side equal to the right side getLine(prompt);
048 }close braces end code blocks and must match an earlier open brace
049 // userAnswer: "y", "yes", "n", or "no"; first letter differentiates
050 returnreturn means to provide the result of the method and/or cease execution of the method immediately userAnswer.substring(0, 1).equalsIgnoreCase("y");
051 }close braces end code blocks and must match an earlier open brace
052
053 /**
054 * Print the prompt on standard output, read the next line from the
055 * standard input and returnreturn means to provide the result of the method and/or cease execution of the method immediately that value.
056 *
057 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt/question to show the user
058 *
059 * @returnnull the {open braces start code blocks and must be matched with a close brace@link String}close braces end code blocks and must match an earlier open brace typed in by the user; it could be empty
060 */
061 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) String getLine(String prompt) {open braces start code blocks and must be matched with a close brace
062 System.out.print(prompt);
063 System.out.print(" ");
064 returnreturn means to provide the result of the method and/or cease execution of the method immediately keyboard.nextLine();
065 }close braces end code blocks and must match an earlier open brace
066
067 /**
068 * The mainThe main method is the place where applications begin executing. program. At thisthis means the current object (the implicit parameter) level: make sure there is a file name on
069 * the command-line, construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link Pig}close braces end code blocks and must match an earlier open brace object, load and play
070 * the game.
071 *
072 * @paramthis is the Javadoc tag for documenting the purpose of parameters args command-line arguments; ignored by thisthis means the current object (the implicit parameter) program
073 */
074 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
075 Pig game =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Pig();
076 game.getPlayersNames();
077 ifif executes the next statement only if the condition in parenthesis evaluates to true (game.isPlayable()) {open braces start code blocks and must be matched with a close brace
078 game.play();
079 }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
080 System.err.println("Not enough players to make game playable.");
081 }close braces end code blocks and must match an earlier open brace
082 }close braces end code blocks and must match an earlier open brace
083
084 /**
085 * Announce the winner of the game. Method shows the sorted standings
086 * table and then announces the winner at the end.
087 *
088 * @paramthis is the Javadoc tag for documenting the purpose of parameters winner reference to the {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace who won the game
089 */
090 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value announceWinner(Player winner) {open braces start code blocks and must be matched with a close brace
091 sp_Sort(standings);
092 System.out.println("Final Standings:");
093 showStandings();
094 System.out.println(winner.getName() +adds two numbers together or concatenates Strings together " wins with " +adds two numbers together or concatenates Strings together
095 winner.getScore() +adds two numbers together or concatenates Strings together " points.");
096 }close braces end code blocks and must match an earlier open brace
097
098 /**
099 * Update the state of the game. Returns truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the game should
100 * continuecontinue terminates the current iteration of the loop and continues to the next iteration, falsefalse is a value for the boolean type and means not true otherwise.
101 *
102 * @paramthis is the Javadoc tag for documenting the purpose of parameters curr the index in standings of the current player
103 *
104 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true game should continuecontinue terminates the current iteration of the loop and continues to the next iteration; falsefalse is a value for the boolean type and means not true otherwise
105 */
106 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false continueGame(Player curr) {open braces start code blocks and must be matched with a close brace
107 booleanboolean is a type that is either true or false currHasWon =this assignment operator makes the left side equal to the right side curr.getScore() >=this evaluates to true if the left side is not less than the right side GAME_SCORE;
108 returnreturn means to provide the result of the method and/or cease execution of the method immediately !this is the not operator, which changes true to false and false to truecurrHasWon;
109 }close braces end code blocks and must match an earlier open brace
110
111 /**
112 * Get the n'th player in the list of players.
113 *
114 * @paramthis is the Javadoc tag for documenting the purpose of parameters n index of the player to fetch
115 *
116 * @returnnull reference to the player in position n of the standings
117 * list
118 */
119 privateprivate is used to restrict access to the current class only Player getPlayer(intint is the type for whole numbers and it is short for integer n) {open braces start code blocks and must be matched with a close brace
120 returnreturn means to provide the result of the method and/or cease execution of the method immediately standings.get(n);
121 }close braces end code blocks and must match an earlier open brace
122
123 /**
124 * Get the index of the {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace p in the {open braces start code blocks and must be matched with a close brace@link #standings}close braces end code blocks and must match an earlier open brace
125 * list.
126 *
127 * @paramthis is the Javadoc tag for documenting the purpose of parameters p the {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace to find
128 *
129 * @returnnull the index of p in the standings (ifif executes the next statement only if the condition in parenthesis evaluates to true it exists); -1
130 * otherwise
131 */
132 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer indexOf(Player p) {open braces start code blocks and must be matched with a close brace
133 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side -1;
134 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different standings.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
135 ifif executes the next statement only if the condition in parenthesis evaluates to true (standings.get(i) ==this is the comparison operator which evaluates to true if both sides are the same p) {open braces start code blocks and must be matched with a close brace
136 ndx =this assignment operator makes the left side equal to the right side i;
137 }close braces end code blocks and must match an earlier open brace
138 }close braces end code blocks and must match an earlier open brace
139 returnreturn means to provide the result of the method and/or cease execution of the method immediately ndx;
140 }close braces end code blocks and must match an earlier open brace
141
142 /**
143 * Get player's names from the user. A sentinel-controlled loop
144 * (sentinel: when user enters "done" as a player's name) uses {open braces start code blocks and must be matched with a close brace@link
145 * #getLine(String)}close braces end code blocks and must match an earlier open brace to prompt user forfor is a looping structure for repeatedly executing a block of code player's names. A {open braces start code blocks and must be matched with a close brace@link
146 * Player}close braces end code blocks and must match an earlier open brace object is then constructed forfor is a looping structure for repeatedly executing a block of code the newnew is used to create objects by calling the constructor player.<br />
147 * {open braces start code blocks and must be matched with a close brace@link #standings}close braces end code blocks and must match an earlier open brace is updated by thisthis means the current object (the implicit parameter) method.
148 */
149 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value getPlayersNames() {open braces start code blocks and must be matched with a close brace
150 String playerName =this assignment operator makes the left side equal to the right side "";
151 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to trueplayerName.equalsIgnoreCase("done")) {open braces start code blocks and must be matched with a close brace
152 playerName =this assignment operator makes the left side equal to the right side getLine("Next player's Name ('done' to exit):");
153 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 trueplayerName.equalsIgnoreCase("done")) {open braces start code blocks and must be matched with a close brace
154 standings.add(newnew is used to create objects by calling the constructor Player(playerName));
155 }close braces end code blocks and must match an earlier open brace
156 }close braces end code blocks and must match an earlier open brace
157 }close braces end code blocks and must match an earlier open brace
158
159 /**
160 * Get input from the user to complete their turn
161 *
162 * @paramthis is the Javadoc tag for documenting the purpose of parameters curr reference to the current {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace
163 */
164 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value handlePlayerTurn(Player curr) {open braces start code blocks and must be matched with a close brace
165 curr.takeTurn();
166 }close braces end code blocks and must match an earlier open brace
167
168 /**
169 * Is thisthis means the current object (the implicit parameter) game playable? Are there enough players?
170 *
171 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true there are enough players; falsefalse is a value for the boolean type and means not true otherwise
172 */
173 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isPlayable() {open braces start code blocks and must be matched with a close brace
174 booleanboolean is a type that is either true or false hasPlayerList =this assignment operator makes the left side equal to the right side standings !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object;
175 booleanboolean is a type that is either true or false hasEnoughPlayers =this assignment operator makes the left side equal to the right side hasPlayerList &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (standings.size() > 1);
176 returnreturn means to provide the result of the method and/or cease execution of the method immediately hasPlayerList &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) hasEnoughPlayers;
177 }close braces end code blocks and must match an earlier open brace
178
179 /**
180 * Increment the turn variable; returnreturn means to provide the result of the method and/or cease execution of the method immediately the next turn number.
181 *
182 * @paramthis is the Javadoc tag for documenting the purpose of parameters currentTurn the current turn
183 *
184 * @returnnull the index of the next player whose turn it should be
185 */
186 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer nextTurn(intint is the type for whole numbers and it is short for integer currentTurn) {open braces start code blocks and must be matched with a close brace
187 returnreturn means to provide the result of the method and/or cease execution of the method immediately (currentTurn +adds two numbers together or concatenates Strings together 1) %this divides the left side by the right side and evaluates to the remainder standings.size();
188 }close braces end code blocks and must match an earlier open brace
189
190 /**
191 * Play the game.<br />
192 * While the game is being played, one player takes their turn (a
193 * video game loop is in there). If player wins (can only happen at
194 * the end of their turn), announce it and end play If the player does
195 * not win, advance the turn index by 1 modulo the number of players
196 * (players numbered from 0 inside {open braces start code blocks and must be matched with a close brace@link #standings}close braces end code blocks and must match an earlier open brace). When we roll
197 * over to showing the first player print a standing's table in player
198 * number order.
199 */
200 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value play() {open braces start code blocks and must be matched with a close brace
201 intint is the type for whole numbers and it is short for integer turn =this assignment operator makes the left side equal to the right side 0;
202 Player curr =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
203 booleanboolean is a type that is either true or false playing =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
204 whilewhile is a looping structure for executing code repeatedly (playing) {open braces start code blocks and must be matched with a close brace
205 curr =this assignment operator makes the left side equal to the right side getPlayer(turn);
206 showState(curr);
207 handlePlayerTurn(curr);
208 playing =this assignment operator makes the left side equal to the right side continueGame(curr);
209 turn =this assignment operator makes the left side equal to the right side nextTurn(turn);
210 }close braces end code blocks and must match an earlier open brace
211 announceWinner(curr);
212 }close braces end code blocks and must match an earlier open brace
213
214 /**
215 * Print the standings (player's names and scores) in the order they
216 * are stored in {open braces start code blocks and must be matched with a close brace@link #standings}close braces end code blocks and must match an earlier open brace.
217 */
218 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value showStandings() {open braces start code blocks and must be matched with a close brace
219 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different standings.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
220 System.out.println(" " +adds two numbers together or concatenates Strings together standings.get(i));
221 }close braces end code blocks and must match an earlier open brace
222 }close braces end code blocks and must match an earlier open brace
223
224 /**
225 * Print the current state of the game. Show the standings (ifif executes the next statement only if the condition in parenthesis evaluates to true
226 * necessary) and show the current player's status.
227 *
228 * @paramthis is the Javadoc tag for documenting the purpose of parameters curr reference to the {open braces start code blocks and must be matched with a close brace@link Player}close braces end code blocks and must match an earlier open brace whose turn it is
229 */
230 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value showState(Player curr) {open braces start code blocks and must be matched with a close brace
231 booleanboolean is a type that is either true or false isFirstPlayersTurn =this assignment operator makes the left side equal to the right side (indexOf(curr) ==this is the comparison operator which evaluates to true if both sides are the same 0);
232 ifif executes the next statement only if the condition in parenthesis evaluates to true (isFirstPlayersTurn) {open braces start code blocks and must be matched with a close brace
233 System.out.println();// improve readability of output
234 System.out.println("Standings:");
235 showStandings();
236 }close braces end code blocks and must match an earlier open brace
237 System.out.println();// improve readability of output
238 System.out.println(curr);
239 }close braces end code blocks and must match an earlier open brace
240
241 /**
242 * Find the index of the largest score inside of standings at or after
243 * the given starting index
244 *
245 * @paramthis is the Javadoc tag for documenting the purpose of parameters aList reference to the list in which the index of
246 * the largest element is to be found
247 * @paramthis is the Javadoc tag for documenting the purpose of parameters startIndex start searching at thisthis means the current object (the implicit parameter) index in the list
248 *
249 * @returnnull a number >=this evaluates to true if the left side is not less than the right side to startIndex, an index into aList; ifif executes the next statement only if the condition in parenthesis evaluates to true
250 * startIndex is out of range, will returnreturn means to provide the result of the method and/or cease execution of the method immediately startIndex;
251 * otherwise will always returnreturn means to provide the result of the method and/or cease execution of the method immediately a valid index
252 */
253 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer sp_LargestIndex(ArrayList<Player> standings,
254 intint is the type for whole numbers and it is short for integer startIndex) {open braces start code blocks and must be matched with a close brace
255 intint is the type for whole numbers and it is short for integer largestNdx =this assignment operator makes the left side equal to the right side startIndex;
256 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer contenderNdx =this assignment operator makes the left side equal to the right side startIndex +adds two numbers together or concatenates Strings together 1;
257 contenderNdx !=this is the not equals operator which evaluates to true if both sides are different standings.size();
258 ++this is the increment operator, which increases the variable by 1contenderNdx) {open braces start code blocks and must be matched with a close brace
259 ifif executes the next statement only if the condition in parenthesis evaluates to true (standings.get(contenderNdx).getScore() >
260 standings.get(largestNdx).getScore()) {open braces start code blocks and must be matched with a close brace
261 largestNdx =this assignment operator makes the left side equal to the right side contenderNdx;
262 }close braces end code blocks and must match an earlier open brace
263 }close braces end code blocks and must match an earlier open brace
264 returnreturn means to provide the result of the method and/or cease execution of the method immediately largestNdx;
265 }close braces end code blocks and must match an earlier open brace
266
267 /**
268 * Sort the standings in descending order by score. firstUnsortedIndex
269 * indexes the first entry in the standings which is not yet sorted.
270 * The body of the outer loop moves the right element into the first
271 * unsorted position (making that position sorted), so
272 * firstUnsortedIndex can be incremented (by the forfor is a looping structure for repeatedly executing a block of code loop).<br />
273 */
274 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value sp_Sort(ArrayList<Player> standings) {open braces start code blocks and must be matched with a close brace
275 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer firstUnsortedIndex =this assignment operator makes the left side equal to the right side 0;
276 firstUnsortedIndex !=this is the not equals operator which evaluates to true if both sides are different standings.size();
277 ++this is the increment operator, which increases the variable by 1firstUnsortedIndex) {open braces start code blocks and must be matched with a close brace
278 intint is the type for whole numbers and it is short for integer largestUnsortedIndex =this assignment operator makes the left side equal to the right side
279 sp_LargestIndex(standings, firstUnsortedIndex);
280 sp_Swap(standings, firstUnsortedIndex, largestUnsortedIndex);
281 }close braces end code blocks and must match an earlier open brace
282 }close braces end code blocks and must match an earlier open brace
283
284 /**
285 * Swap elements standings[brackets are typically used to declare, initialize and index (indicate which element of) arraysa]brackets are typically used to declare, initialize and index (indicate which element of) arrays and standings[brackets are typically used to declare, initialize and index (indicate which element of) arraysb]brackets are typically used to declare, initialize and index (indicate which element of) arrays (using array notation).
286 * Works forfor is a looping structure for repeatedly executing a block of code all valid index value forfor is a looping structure for repeatedly executing a block of code a and b (even ifif executes the next statement only if the condition in parenthesis evaluates to true they are
287 * equal). Does not dodo is part of the do-while looping structure (post condition loop) anything crafty when they are equal.
288 *
289 * @paramthis is the Javadoc tag for documenting the purpose of parameters standings list in which elements should be changed
290 * @paramthis is the Javadoc tag for documenting the purpose of parameters a an index into aList
291 * @paramthis is the Javadoc tag for documenting the purpose of parameters b an index into aList
292 */
293 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value sp_Swap(ArrayList<Player> standings, intint is the type for whole numbers and it is short for integer a, intint is the type for whole numbers and it is short for integer b) {open braces start code blocks and must be matched with a close brace
294 Player temp =this assignment operator makes the left side equal to the right side standings.get(a);
295 standings.set(a, standings.get(b));
296 standings.set(b, temp);
297 }close braces end code blocks and must match an earlier open brace
298 }close braces end code blocks and must match an earlier open brace
299
300 //Uploaded on Mon Mar 29 21:39:50 EDT 2010
|