|
001 packagepackage is used to name the directory or folder a class is in scg.ch13.core;
002
003 importimport means to make the classes and/or packages available in this program java.awt.Color;
004 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
005
006 importimport means to make the classes and/or packages available in this program fang2.attributes.Location2D;
007 importimport means to make the classes and/or packages available in this program fang2.core.Game;
008 importimport means to make the classes and/or packages available in this program fang2.core.Sprite;
009 importimport means to make the classes and/or packages available in this program fang2.sprites.CompositeSprite;
010 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
011
012 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.Piece;
013
014 /**
015 * The board forfor is a looping structure for repeatedly executing a block of code playing a game of BlockDrop (aka Tetris). The code here
016 * was inspired by Javier LÜÜpez's C++this is the increment operator, which increases the variable by 1 Tetris Tutorial (one of the best
017 * simplifications I have seen)
018 * http://gametuto.com/tetris-tutorial-in-c-render-independent/<br />
019 * The board includes the background, the border, the visible squares,
020 * and an internal representation of the on-screen grid (forfor is a looping structure for repeatedly executing a block of code collision
021 * detection, line removal, and scoring). The board tracks its current
022 * piece. It is associated with a {open braces start code blocks and must be matched with a close brace@link ScoreSprite}close braces end code blocks and must match an earlier open brace (so that the score
023 * is automatically updated).<br />
024 * Assumption: rows > columns
025 */
026
027 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 Board
028 extendsextends means to customize or extend the functionality of a class CompositeSprite {open braces start code blocks and must be matched with a close brace
029 /** Constants forfor is a looping structure for repeatedly executing a block of code reporting whether a piece is in the wrong place */
030 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 CLEAR =this assignment operator makes the left side equal to the right side 0;
031 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 HIT =this assignment operator makes the left side equal to the right side 1;
032 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 HIGH =this assignment operator makes the left side equal to the right side 2;
033 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 LOW =this assignment operator makes the left side equal to the right side 3;
034 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 LEFT =this assignment operator makes the left side equal to the right side 4;
035 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 RIGHT =this assignment operator makes the left side equal to the right side 5;
036
037 /** the defaultdefault is what is executed when no cases are matched color of the mainThe main method is the place where applications begin executing. board */
038 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) Color DEFAULT_BACKGROUND_COLOR =this assignment operator makes the left side equal to the right side
039 Game.getColor("misty rose");
040
041 /** the defaultdefault is what is executed when no cases are matched color of the border around the board */
042 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) Color DEFAULT_EDGE_COLOR =this assignment operator makes the left side equal to the right side
043 Game.getColor("SCG Red");
044
045
046 /** display of the background */
047 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite background;
048
049 /** board grid; each location is either nullnull is the value used to refer to a non-existant object or filled with a block */
050 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<ArrayList<RectangleSprite>>this performs a bit-wise right shift blocks;
051
052 /** size in internal coordinates of one block edge */
053 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions blockSize;
054
055 /** width, in blocks, of the board */
056 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer columns;
057
058 /** the piece which is falling */
059 privateprivate is used to restrict access to the current class only Piece currentPiece;
060
061 /** display of 3-sided border around board */
062 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite edges;
063
064 /** height, in blocks, of the board */
065 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer rows;
066
067 /** where removed row count is recorded */
068 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ScoreSprite score;
069
070 /** internal coordinate point of upper-left corner of board */
071 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions ulcX;
072 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions ulcY;
073
074 /**
075 * 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 Board}close braces end code blocks and must match an earlier open brace object. The score is a {open braces start code blocks and must be matched with a close brace@link
076 * ScoreSprite}close braces end code blocks and must match an earlier open brace where thisthis means the current object (the implicit parameter) board will report cleared rows. The row and
077 * column count are the size of the board. It is assumed that row >
078 * columns.
079 *
080 * @paramthis is the Javadoc tag for documenting the purpose of parameters score {open braces start code blocks and must be matched with a close brace@link ScoreSprite}close braces end code blocks and must match an earlier open brace where cleared rows are reported.
081 * @paramthis is the Javadoc tag for documenting the purpose of parameters rows the number of rows high the {open braces start code blocks and must be matched with a close brace@link Board}close braces end code blocks and must match an earlier open brace should
082 * be; > columns
083 * @paramthis is the Javadoc tag for documenting the purpose of parameters columns the number of columns wide the {open braces start code blocks and must be matched with a close brace@link Board}close braces end code blocks and must match an earlier open brace
084 * should be; < rows
085 */
086 publicpublic is used to indicate unrestricted access (any other class can have access) Board(ScoreSprite score, intint is the type for whole numbers and it is short for integer rows, intint is the type for whole numbers and it is short for integer columns) {open braces start code blocks and must be matched with a close brace
087 thisthis means the current object (the implicit parameter).score =this assignment operator makes the left side equal to the right side score;
088 thisthis means the current object (the implicit parameter).rows =this assignment operator makes the left side equal to the right side rows;
089 thisthis means the current object (the implicit parameter).columns =this assignment operator makes the left side equal to the right side columns;
090
091 blockSize =this assignment operator makes the left side equal to the right side 1.0 / rows;
092
093 // Show one square width on each of 3 sides
094 edges =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite((columns +adds two numbers together or concatenates Strings together 2) * blockSize,
095 (rows +adds two numbers together or concatenates Strings together 1) * blockSize);
096 edges.setLocation(0.0, blockSize / 2);
097 setEdgeColor(DEFAULT_EDGE_COLOR);
098 addSprite(edges);
099
100 background =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(blockSize * columns, 1.0);
101 setColor(DEFAULT_BACKGROUND_COLOR);
102 addSprite(background);
103
104 ulcX =this assignment operator makes the left side equal to the right side -blockSize / 2 * (columns - 1);
105 ulcY =this assignment operator makes the left side equal to the right side -0.5 +adds two numbers together or concatenates Strings together (blockSize / 2);
106
107 blocks =this assignment operator makes the left side equal to the right side initBlocks();
108 }close braces end code blocks and must match an earlier open brace
109
110 /**
111 * Add the given piece to the board as the currentPiece. The piece is
112 * rescaled and positioned according to its initial position.<br />
113 * Should only be called when currentPiece is no longer needed.
114 *
115 * @paramthis is the Javadoc tag for documenting the purpose of parameters currentPiece the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace to make current.
116 */
117 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value add(Piece currentPiece) {open braces start code blocks and must be matched with a close brace
118 thisthis means the current object (the implicit parameter).currentPiece =this assignment operator makes the left side equal to the right side currentPiece;
119 currentPiece.setBoard(thisthis means the current object (the implicit parameter));
120 currentPiece.setScale(currentPiece.getRowSize() * blockSize);
121 currentPiece.setRow(currentPiece.getInitialRow());
122 currentPiece.setColumn(currentPiece.getInitialColumn() +adds two numbers together or concatenates Strings together
123 (columns / 2));
124 addSprite(currentPiece);
125 }close braces end code blocks and must match an earlier open brace
126
127 /**
128 * Can the given {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace, piece, occupy the given position (row,
129 * column) in the given rotation (facing) without colliding with
130 * existing squares (already frozen on the board) or the edges? The
131 * values row, column, and facing are provided so that any piece can
132 * be tested in any position.
133 *
134 * @paramthis is the Javadoc tag for documenting the purpose of parameters piece the piece to test
135 * @paramthis is the Javadoc tag for documenting the purpose of parameters row the row where the piece should be imagined
136 * @paramthis is the Javadoc tag for documenting the purpose of parameters column the column where the piece should be imagined
137 * @paramthis is the Javadoc tag for documenting the purpose of parameters facing the facing the piece is to be imagined to have
138 *
139 * @returnnull CLEAR ifif executes the next statement only if the condition in parenthesis evaluates to true safe to move; HIT, HIGH, LOW, LEFT, or RIGHT
140 * otherwise
141 */
142 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer canMoveTo(Piece piece, intint is the type for whole numbers and it is short for integer row, intint is the type for whole numbers and it is short for integer column, intint is the type for whole numbers and it is short for integer facing) {open braces start code blocks and must be matched with a close brace
143 intint is the type for whole numbers and it is short for integer canMove =this assignment operator makes the left side equal to the right side CLEAR;
144 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 r =this assignment operator makes the left side equal to the right side 0; (canMove ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) &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 &&) (r !=this is the not equals operator which evaluates to true if both sides are different piece.getRowSize());
145 ++this is the increment operator, which increases the variable by 1r) {open braces start code blocks and must be matched with a close brace
146 intint is the type for whole numbers and it is short for integer boardRow =this assignment operator makes the left side equal to the right side row +adds two numbers together or concatenates Strings together r;
147 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 c =this assignment operator makes the left side equal to the right side 0;
148 (canMove ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) &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 &&) (c !=this is the not equals operator which evaluates to true if both sides are different piece.getColumnSize()); ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
149 intint is the type for whole numbers and it is short for integer boardCol =this assignment operator makes the left side equal to the right side column +adds two numbers together or concatenates Strings together c;
150 ifif executes the next statement only if the condition in parenthesis evaluates to true (piece.hasBlock(r, c, facing)) {open braces start code blocks and must be matched with a close brace
151 canMove =this assignment operator makes the left side equal to the right side positionOnBoard(boardRow, boardCol);
152 ifif executes the next statement only if the condition in parenthesis evaluates to true (canMove ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) {open braces start code blocks and must be matched with a close brace
153 ifif executes the next statement only if the condition in parenthesis evaluates to true (hasBlock(boardRow, boardCol)) {open braces start code blocks and must be matched with a close brace
154 canMove =this assignment operator makes the left side equal to the right side HIT;
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 }close braces end code blocks and must match an earlier open brace
159 }close braces end code blocks and must match an earlier open brace
160 returnreturn means to provide the result of the method and/or cease execution of the method immediately canMove;
161 }close braces end code blocks and must match an earlier open brace
162
163 /**
164 * Move the piece down until it freezes.
165 */
166 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value drop() {open braces start code blocks and must be matched with a close brace
167 whilewhile is a looping structure for executing code repeatedly (moveDownIfPossible()) {open braces start code blocks and must be matched with a close brace
168 ;// don't do anything (empty loop)
169 }close braces end code blocks and must match an earlier open brace
170 }close braces end code blocks and must match an earlier open brace
171
172 /**
173 * Get the edge size used by the board.
174 *
175 * @returnnull the edge size
176 */
177 publicpublic is used to indicate unrestricted access (any other class can have access) doubledouble is the type for numbers that can contain decimal fractions getBlockSize() {open braces start code blocks and must be matched with a close brace
178 returnreturn means to provide the result of the method and/or cease execution of the method immediately blockSize;
179 }close braces end code blocks and must match an earlier open brace
180
181 /**
182 * Test whether or not the game is over.
183 *
184 * @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 the game is over, falsefalse is a value for the boolean type and means not true otherwise
185 */
186 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isGameOver() {open braces start code blocks and must be matched with a close brace
187 booleanboolean is a type that is either true or false retval =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
188 ArrayList<RectangleSprite> topRow =this assignment operator makes the left side equal to the right side blocks.get(0);
189
190 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 c =this assignment operator makes the left side equal to the right side 0; c !=this is the not equals operator which evaluates to true if both sides are different columns; ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
191 retval =this assignment operator makes the left side equal to the right side retval ||this is boolean or, meaning if either or both are true then the result is true (topRow.get(c) !=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);
192 }close braces end code blocks and must match an earlier open brace
193 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
194 }close braces end code blocks and must match an earlier open brace
195
196 /**
197 * Attempt to move the current piece down one grid location. If it
198 * cannot move down, freeze the piece in place, remove all filled
199 * rows, and update score.
200 *
201 * @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 the piece moved down one row; falsefalse is a value for the boolean type and means not true ifif executes the next statement only if the condition in parenthesis evaluates to true it was
202 * frozen
203 */
204 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false moveDownIfPossible() {open braces start code blocks and must be matched with a close brace
205 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canMoveDown() ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) {open braces start code blocks and must be matched with a close brace
206 currentPiece.moveDown();
207 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
208 }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 (contains(currentPiece)) {open braces start code blocks and must be matched with a close brace
209 freeze(currentPiece);
210 intint is the type for whole numbers and it is short for integer moveScore =this assignment operator makes the left side equal to the right side deleteAllFilledRows();
211 ifif executes the next statement only if the condition in parenthesis evaluates to true (score !=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) {open braces start code blocks and must be matched with a close brace
212 score.increment(moveScore);
213 }close braces end code blocks and must match an earlier open brace
214 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
215 }close braces end code blocks and must match an earlier open brace
216 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
217 }close braces end code blocks and must match an earlier open brace
218
219 /**
220 * Attempt to move the current piece left one column. Leave it
221 * unchanged ifif executes the next statement only if the condition in parenthesis evaluates to true it could not move.
222 *
223 * @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 the piece was moved; falsefalse is a value for the boolean type and means not true otherwise
224 */
225 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false moveLeftIfPossible() {open braces start code blocks and must be matched with a close brace
226 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canMoveLeft() ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) {open braces start code blocks and must be matched with a close brace
227 currentPiece.moveLeft();
228 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
229 }close braces end code blocks and must match an earlier open brace
230 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
231 }close braces end code blocks and must match an earlier open brace
232
233 /**
234 * Attempt to move the current piece right one column. Leave it
235 * unchanged ifif executes the next statement only if the condition in parenthesis evaluates to true it was blocked.
236 *
237 * @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 the piece was moved; falsefalse is a value for the boolean type and means not true otherwise
238 */
239 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false moveRightIfPossible() {open braces start code blocks and must be matched with a close brace
240 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canMoveRight() ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) {open braces start code blocks and must be matched with a close brace
241 currentPiece.moveRight();
242 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
243 }close braces end code blocks and must match an earlier open brace
244 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
245 }close braces end code blocks and must match an earlier open brace
246
247 /**
248 * Rotate the current piece anti-clockwise, ifif executes the next statement only if the condition in parenthesis evaluates to true possible. If rotation
249 * would put the piece into the edge of the board or an adjacent
250 * piece, "bump" the piece over to make it clear the edge. Note that
251 * that it might not be possible to clear the obstacle (think narrow
252 * piece in a narrow "alley"). If it cannot clear it will hit
253 * something on the other side. If that happens, reset everything and
254 * get out. Otherwise dodo is part of the do-while looping structure (post condition loop) the rotation.
255 *
256 * @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 the piece was rotated; falsefalse is a value for the boolean type and means not true otherwise
257 */
258 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false rotateCCWIfPossible() {open braces start code blocks and must be matched with a close brace
259 intint is the type for whole numbers and it is short for integer startColumn =this assignment operator makes the left side equal to the right side currentPiece.getColumn();
260 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canRotateCCW() ==this is the comparison operator which evaluates to true if both sides are the same LEFT) {open braces start code blocks and must be matched with a close brace
261 whilewhile is a looping structure for executing code repeatedly (currentPiece.canRotateCCW() ==this is the comparison operator which evaluates to true if both sides are the same LEFT) {open braces start code blocks and must be matched with a close brace
262 currentPiece.moveRight();
263 }close braces end code blocks and must match an earlier open brace
264 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canRotateCCW() !=this is the not equals operator which evaluates to true if both sides are different CLEAR) {open braces start code blocks and must be matched with a close brace
265 currentPiece.setColumn(startColumn);
266 }close braces end code blocks and must match an earlier open brace
267 }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 (currentPiece.canRotateCCW() ==this is the comparison operator which evaluates to true if both sides are the same RIGHT) {open braces start code blocks and must be matched with a close brace
268 whilewhile is a looping structure for executing code repeatedly (currentPiece.canRotateCCW() ==this is the comparison operator which evaluates to true if both sides are the same RIGHT) {open braces start code blocks and must be matched with a close brace
269 currentPiece.moveLeft();
270 }close braces end code blocks and must match an earlier open brace
271 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canRotateCCW() !=this is the not equals operator which evaluates to true if both sides are different CLEAR) {open braces start code blocks and must be matched with a close brace
272 currentPiece.setColumn(startColumn);
273 }close braces end code blocks and must match an earlier open brace
274 }close braces end code blocks and must match an earlier open brace
275
276 ifif executes the next statement only if the condition in parenthesis evaluates to true (currentPiece.canRotateCCW() ==this is the comparison operator which evaluates to true if both sides are the same CLEAR) {open braces start code blocks and must be matched with a close brace
277 currentPiece.rotateCCW();
278 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
279 }close braces end code blocks and must match an earlier open brace
280 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
281 }close braces end code blocks and must match an earlier open brace
282
283 /**
284 * Set the color (the background color) of thisthis means the current object (the implicit parameter) sprite. Caught here to
285 * set background, too.
286 *
287 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set
288 */
289 @Override
290 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setColor(Color color) {open braces start code blocks and must be matched with a close brace
291 super.setColor(color);
292 background.setColor(color);
293 }close braces end code blocks and must match an earlier open brace
294
295 /**
296 * Set the color of edge, the edge box around the piece box.
297 *
298 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set
299 */
300 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setEdgeColor(Color color) {open braces start code blocks and must be matched with a close brace
301 edges.setColor(color);
302 }close braces end code blocks and must match an earlier open brace
303
304 /**
305 * Set the given piece's location based on its current board location.
306 * Note that thisthis means the current object (the implicit parameter) takes into account the size of the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace (in
307 * rows and columns)
308 *
309 * @paramthis is the Javadoc tag for documenting the purpose of parameters piece the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace to position
310 */
311 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setPieceLocation(Piece piece) {open braces start code blocks and must be matched with a close brace
312 piece.setLocation((blockSize * (piece.getColumn() +adds two numbers together or concatenates Strings together 2)) +adds two numbers together or concatenates Strings together ulcX,
313 (blockSize * (piece.getRow() +adds two numbers together or concatenates Strings together 2)) +adds two numbers together or concatenates Strings together ulcY);
314 }close braces end code blocks and must match an earlier open brace
315
316 /**
317 * Steal a block forfor is a looping structure for repeatedly executing a block of code the given position. Modify the scale and location
318 * (since it will be in our space rather than in the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace's
319 * space) and returnreturn means to provide the result of the method and/or cease execution of the method immediately a reference to the block.
320 *
321 * @paramthis is the Javadoc tag for documenting the purpose of parameters block the block to steal from the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace
322 * @paramthis is the Javadoc tag for documenting the purpose of parameters blockRow board row of block
323 * @paramthis is the Javadoc tag for documenting the purpose of parameters blockColumn board column of block to create
324 *
325 * @returnnull reference to the newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link RectangleSprite}close braces end code blocks and must match an earlier open brace
326 */
327 privateprivate is used to restrict access to the current class only RectangleSprite acquireBlock(RectangleSprite block,
328 intint is the type for whole numbers and it is short for integer blockRow, intint is the type for whole numbers and it is short for integer blockColumn) {open braces start code blocks and must be matched with a close brace
329 block.setScale(blockSize);
330 block.setLocation(locationFromRowColumn(blockRow, blockColumn));
331 returnreturn means to provide the result of the method and/or cease execution of the method immediately block;
332 }close braces end code blocks and must match an earlier open brace
333
334 /**
335 * Traverse the board from top to bottom. When a full row is found,
336 * remove the row. This moves all rows down one. This is safe (we will
337 * still check all the rows) because immediately the row index is
338 * incremented to the NEXT ROW after the part that was changed by the
339 * delete operation. The method counts the number of rows removed (so
340 * that score can be kept).
341 *
342 * @returnnull the number of rows actually deleted. Should be on the
343 * range 0-4.
344 */
345 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer deleteAllFilledRows() {open braces start code blocks and must be matched with a close brace
346 intint is the type for whole numbers and it is short for integer rowsDeleted =this assignment operator makes the left side equal to the right side 0;
347 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 r =this assignment operator makes the left side equal to the right side 0; r !=this is the not equals operator which evaluates to true if both sides are different rows; ++this is the increment operator, which increases the variable by 1r) {open braces start code blocks and must be matched with a close brace
348 ifif executes the next statement only if the condition in parenthesis evaluates to true (isRowFilled(r)) {open braces start code blocks and must be matched with a close brace
349 deleteRow(r);
350 ++this is the increment operator, which increases the variable by 1rowsDeleted;
351 }close braces end code blocks and must match an earlier open brace
352 }close braces end code blocks and must match an earlier open brace
353 returnreturn means to provide the result of the method and/or cease execution of the method immediately rowsDeleted;
354 }close braces end code blocks and must match an earlier open brace
355
356 /**
357 * Move all rows above deadRowNdx down one row. Insert a newnew is used to create objects by calling the constructor row 0.
358 *
359 * @paramthis is the Javadoc tag for documenting the purpose of parameters deadRowNdx the index of the row to remove
360 */
361 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value deleteRow(intint is the type for whole numbers and it is short for integer deadRowNdx) {open braces start code blocks and must be matched with a close brace
362 removeRowOfSprites(blocks.get(deadRowNdx));
363 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 r =this assignment operator makes the left side equal to the right side deadRowNdx; r !=this is the not equals operator which evaluates to true if both sides are different 0; --this is the decrement operator, which decreases the variable by 1r) {open braces start code blocks and must be matched with a close brace// counts backwards!
364 blocks.set(r, blocks.get(r - 1));// (r - 1) >= 0
365 ArrayList<RectangleSprite> currRow =this assignment operator makes the left side equal to the right side blocks.get(r);
366 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 c =this assignment operator makes the left side equal to the right side 0; c !=this is the not equals operator which evaluates to true if both sides are different columns; ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
367 ifif executes the next statement only if the condition in parenthesis evaluates to true (currRow.get(c) !=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) {open braces start code blocks and must be matched with a close brace
368 currRow.get(c).setLocation(locationFromRowColumn(r, c));
369 }close braces end code blocks and must match an earlier open brace
370 }close braces end code blocks and must match an earlier open brace
371 }close braces end code blocks and must match an earlier open brace
372 // add a new, empty row at the top.
373 blocks.set(0, initRow());
374 }close braces end code blocks and must match an earlier open brace
375
376 /**
377 * Lock the piece down at its current location.
378 *
379 * @paramthis is the Javadoc tag for documenting the purpose of parameters piece typically the current piece; the piece to freeze
380 */
381 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value freeze(Piece piece) {open braces start code blocks and must be matched with a close brace
382 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 r =this assignment operator makes the left side equal to the right side 0; r !=this is the not equals operator which evaluates to true if both sides are different piece.getRowSize(); ++this is the increment operator, which increases the variable by 1r) {open braces start code blocks and must be matched with a close brace
383 intint is the type for whole numbers and it is short for integer boardRow =this assignment operator makes the left side equal to the right side piece.getRow() +adds two numbers together or concatenates Strings together r;
384 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 c =this assignment operator makes the left side equal to the right side 0; c !=this is the not equals operator which evaluates to true if both sides are different piece.getColumnSize(); ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
385 intint is the type for whole numbers and it is short for integer boardCol =this assignment operator makes the left side equal to the right side piece.getColumn() +adds two numbers together or concatenates Strings together c;
386 ifif executes the next statement only if the condition in parenthesis evaluates to true ((piece.hasBlock(r, c)) &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 &&)
387 (positionOnBoard(boardRow, boardCol) ==this is the comparison operator which evaluates to true if both sides are the same CLEAR)) {open braces start code blocks and must be matched with a close brace
388 RectangleSprite block =this assignment operator makes the left side equal to the right side acquireBlock(piece.blockAt(r, c),
389 boardRow, boardCol);
390 blocks.get(boardRow).set(boardCol, block);
391 addSprite(block);
392 }close braces end code blocks and must match an earlier open brace
393 }close braces end code blocks and must match an earlier open brace
394 }close braces end code blocks and must match an earlier open brace
395 removeSprite(piece);// remove the CompositeSprite
396 }close braces end code blocks and must match an earlier open brace
397
398 /**
399 * Is the given position on the board full?
400 *
401 * @paramthis is the Javadoc tag for documenting the purpose of parameters row row of position to check
402 * @paramthis is the Javadoc tag for documenting the purpose of parameters column column of position to check
403 *
404 * @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 position is full (!this is the not operator, which changes true to false and false to trueempty); falsefalse is a value for the boolean type and means not true otherwise
405 */
406 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false hasBlock(intint is the type for whole numbers and it is short for integer row, intint is the type for whole numbers and it is short for integer column) {open braces start code blocks and must be matched with a close brace
407 returnreturn means to provide the result of the method and/or cease execution of the method immediately (blocks.get(row).get(column) !=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);
408 }close braces end code blocks and must match an earlier open brace
409
410 /**
411 * Initialize the positions: all positions are initially empty; set to
412 * nullnull is the value used to refer to a non-existant object. When full they will hold a reference to the {open braces start code blocks and must be matched with a close brace@link
413 * RectangleSprite}close braces end code blocks and must match an earlier open brace in that location. The returned data structure is a
414 * 2D list of lists. Number of lists in thisthis means the current object (the implicit parameter) list is the number of
415 * rows. The
416 */
417 privateprivate is used to restrict access to the current class only ArrayList<ArrayList<RectangleSprite>>this performs a bit-wise right shift initBlocks() {open braces start code blocks and must be matched with a close brace
418 ArrayList<ArrayList<RectangleSprite>>this performs a bit-wise right shift allPositions =this assignment operator makes the left side equal to the right side
419 newnew is used to create objects by calling the constructor ArrayList<ArrayList<RectangleSprite>>this performs a bit-wise right shift();
420 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 r =this assignment operator makes the left side equal to the right side 0; r !=this is the not equals operator which evaluates to true if both sides are different rows; ++this is the increment operator, which increases the variable by 1r) {open braces start code blocks and must be matched with a close brace
421 allPositions.add(initRow());
422 }close braces end code blocks and must match an earlier open brace
423 returnreturn means to provide the result of the method and/or cease execution of the method immediately allPositions;
424 }close braces end code blocks and must match an earlier open brace
425
426 /**
427 * Generate one row of positions. One entry per column, each
428 * initialized to nullnull is the value used to refer to a non-existant object.
429 *
430 * @returnnull reference to the newnew is used to create objects by calling the constructor row of positions
431 */
432 privateprivate is used to restrict access to the current class only ArrayList<RectangleSprite> initRow() {open braces start code blocks and must be matched with a close brace
433 ArrayList<RectangleSprite> nextRow =this assignment operator makes the left side equal to the right side
434 newnew is used to create objects by calling the constructor ArrayList<RectangleSprite>();
435 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 c =this assignment operator makes the left side equal to the right side 0; c !=this is the not equals operator which evaluates to true if both sides are different columns; ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
436 nextRow.add(nullnull is the value used to refer to a non-existant object);
437 }close braces end code blocks and must match an earlier open brace
438 returnreturn means to provide the result of the method and/or cease execution of the method immediately nextRow;
439 }close braces end code blocks and must match an earlier open brace
440
441 /**
442 * Is the given row full of blocks?
443 *
444 * @paramthis is the Javadoc tag for documenting the purpose of parameters rowNdx the index into {open braces start code blocks and must be matched with a close brace@link #blocks}close braces end code blocks and must match an earlier open brace of the row to check
445 *
446 * @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 row is filled with blocks; falsefalse is a value for the boolean type and means not true otherwise
447 */
448 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false isRowFilled(intint is the type for whole numbers and it is short for integer rowNdx) {open braces start code blocks and must be matched with a close brace
449 ArrayList<RectangleSprite> currRow =this assignment operator makes the left side equal to the right side blocks.get(rowNdx);
450 intint is the type for whole numbers and it is short for integer c =this assignment operator makes the left side equal to the right side 0;
451 forfor is a looping structure for repeatedly executing a block of code (c =this assignment operator makes the left side equal to the right side 0; c !=this is the not equals operator which evaluates to true if both sides are different columns; ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
452 ifif executes the next statement only if the condition in parenthesis evaluates to true (currRow.get(c) ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
453 breakbreak terminates the loop immediately;// get out of the loop
454 }close braces end code blocks and must match an earlier open brace
455 }close braces end code blocks and must match an earlier open brace
456 returnreturn means to provide the result of the method and/or cease execution of the method immediately (c ==this is the comparison operator which evaluates to true if both sides are the same columns);// did we finish the loop?
457 }close braces end code blocks and must match an earlier open brace
458
459 /**
460 * Calculate the {open braces start code blocks and must be matched with a close brace@link Sprite}close braces end code blocks and must match an earlier open brace location forfor is a looping structure for repeatedly executing a block of code a {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace at the
461 * given board position Works on individual blocks rather than {open braces start code blocks and must be matched with a close brace@link
462 * Piece}close braces end code blocks and must match an earlier open brace objects
463 *
464 * @paramthis is the Javadoc tag for documenting the purpose of parameters row board row
465 * @paramthis is the Javadoc tag for documenting the purpose of parameters column board column
466 *
467 * @returnnull a {open braces start code blocks and must be matched with a close brace@link Location2D}close braces end code blocks and must match an earlier open brace where the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace must be
468 * positioned
469 */
470 privateprivate is used to restrict access to the current class only Location2D locationFromRowColumn(intint is the type for whole numbers and it is short for integer row, intint is the type for whole numbers and it is short for integer column) {open braces start code blocks and must be matched with a close brace
471 returnreturn means to provide the result of the method and/or cease execution of the method immediately newnew is used to create objects by calling the constructor Location2D(ulcX +adds two numbers together or concatenates Strings together (blockSize * column),
472 ulcY +adds two numbers together or concatenates Strings together (blockSize * row));
473 }close braces end code blocks and must match an earlier open brace
474
475 /**
476 * Is the given location on the board? If not, how did it miss?
477 *
478 * @paramthis is the Javadoc tag for documenting the purpose of parameters row position row
479 * @paramthis is the Javadoc tag for documenting the purpose of parameters column position column
480 *
481 * @returnnull CLEAR ifif executes the next statement only if the condition in parenthesis evaluates to true it is on the board; HIGH, LOW, LEFT, or RIGHT to
482 * indicate one way it missed otherwise
483 */
484 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer positionOnBoard(intint is the type for whole numbers and it is short for integer row, intint is the type for whole numbers and it is short for integer column) {open braces start code blocks and must be matched with a close brace
485 intint is the type for whole numbers and it is short for integer retval =this assignment operator makes the left side equal to the right side CLEAR;
486 ifif executes the next statement only if the condition in parenthesis evaluates to true (row < 0) {open braces start code blocks and must be matched with a close brace
487 retval =this assignment operator makes the left side equal to the right side HIGH;
488 }close braces end code blocks and must match an earlier open brace
489 ifif executes the next statement only if the condition in parenthesis evaluates to true (rows <=this evaluates to true if the left side is not more than the right side row) {open braces start code blocks and must be matched with a close brace
490 retval =this assignment operator makes the left side equal to the right side LOW;
491 }close braces end code blocks and must match an earlier open brace
492 ifif executes the next statement only if the condition in parenthesis evaluates to true (column < 0) {open braces start code blocks and must be matched with a close brace
493 retval =this assignment operator makes the left side equal to the right side LEFT;
494 }close braces end code blocks and must match an earlier open brace
495 ifif executes the next statement only if the condition in parenthesis evaluates to true (columns <=this evaluates to true if the left side is not more than the right side column) {open braces start code blocks and must be matched with a close brace
496 retval =this assignment operator makes the left side equal to the right side RIGHT;
497 }close braces end code blocks and must match an earlier open brace
498 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
499 }close braces end code blocks and must match an earlier open brace
500
501 /**
502 * Remove the sprites in the deadRow from the display of the {open braces start code blocks and must be matched with a close brace@link
503 * Board}close braces end code blocks and must match an earlier open brace
504 *
505 * @paramthis is the Javadoc tag for documenting the purpose of parameters deadRow the row of sprites (and, perhaps, nulls) to remove
506 */
507 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value removeRowOfSprites(ArrayList<RectangleSprite> deadRow) {open braces start code blocks and must be matched with a close brace
508 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 c =this assignment operator makes the left side equal to the right side 0; c !=this is the not equals operator which evaluates to true if both sides are different columns; ++this is the increment operator, which increases the variable by 1c) {open braces start code blocks and must be matched with a close brace
509 ifif executes the next statement only if the condition in parenthesis evaluates to true (deadRow.get(c) !=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) {open braces start code blocks and must be matched with a close brace
510 thisthis means the current object (the implicit parameter).removeSprite(deadRow.get(c));
511 }close braces end code blocks and must match an earlier open brace
512 }close braces end code blocks and must match an earlier open brace
513 }close braces end code blocks and must match an earlier open brace
514 }close braces end code blocks and must match an earlier open brace
515
516 //Uploaded on Mon Mar 29 21:39:46 EDT 2010
|