This problem is intended as practice with traversal of 2D arrays and with String
manipulation. The AP CS Magpie Lab demonstrates String
manipulation. The AP CS Pictures Lab demonstrates 2D array traversal.
Please don’t use this problem to practice your timing for the AP CS A Free Response section. Use only actual released AP CS FR for timing practice. The approach for this problem is identical to a real AP CS FR.
Problem description
Droppy is a single player game that is played on a rectangular board. During each turn all items on the board drop one row toward the bottom. Items that are already at the bottom row drop through the bottom of the board and the player can catch them. The player is positioned below a specific column.
The Droppy
class is shown below. You will write 2 methods of the Droppy
class.
public class Droppy
{
/* board[r][c] represents the item in row r and column c on the board
* Empty spots are represented by null. Guaranteed to contain at least 1 spot */
private String[][] board;
// the column under which the player is positioned
private int playerCol;
// the number of points earned by the player
private int playerPoints;
// the number of turns remaining before the game ends
private int turnsLeft;
/**
* Updates the game state based on the player catching the specified item.
* Prints the phrase: "Player caught " followed by the item's description.
* @param item the item caught by the player
* Precondition: item != null
*/
private void handleCaughtItem(String item)
{ /* to be implemented in part (a) */ }
/**
* Moves each item on the board 1 row below its previous location.
* Items on the last row are removed from the board. If an item in
* column playerCol is removed from the board it is caught by the player.
* Updates the game state if an item is caught by the player.
* Postcondition: the value of playerCol is unchanged
*/
public void shiftAndCatch()
{ /* to be implemented in part (b) */ }
// There may be instance variables, constructors and methods that are not shown.
}
Part (a)
Write the handleCaughtItem
method, which updates the game state based on the item caught and prints a message with the caught item’s description.
Each item is represented by a String
. The String
contains a single word description (without spaces) followed by a space followed by the item’s type. All possible item types are shown below.
"food"
increases the number of turns remaining by 10"prize"
increases the player’s score by 1"penalty"
decreases the player’s score by 1
Examples of items and the effect of the player catching each item are shown below.
Item | Change in game state | Output |
---|---|---|
"eggs food" |
The value of turnsLeft is increased by 10. |
Player caught eggs |
"diamond prize" |
The value of playerPoints is increased by 1. |
Player caught diamond |
"glue penalty" |
The value of playerPoints is decreased by 1. |
Player caught glue |
Complete method handleCaughtItem
.
/**
* Updates the game state based on the player catching the specified item.
* Prints the phrase: "Player caught " followed by the item's description.
* @param item the item caught by the player
* Precondition: item != null
*/
private void handleCaughtItem(String item)
Part (b)
Write the shiftAndCatch
method. The method moves each item to the same column 1 row below its original position. Items on the last row are removed from the board. If an item in column playerCol
is removed from the board the player catches that item and method shiftAndCatch
updates the game state.
The diagram below shows a possible game state immediately prior to the execution of method shiftAndCatch
.
playerCol: 3
playerPoints: 5
turnsLeft: 50
board:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | null |
"eggs food" |
null |
"diamond prize" |
null |
1 | null |
"glue penalty" |
null |
null |
"apple food" |
2 | "rice food" |
null |
null |
"bacon food" |
null |
The diagram below shows the state of the same game immediately after the execution of method shiftAndCatch
.
playerCol: 3
playerPoints: 5
turnsLeft: 60
board:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | null |
null |
null |
null |
null |
1 | null |
"eggs food" |
null |
"diamond prize" |
null |
2 | null |
"glue penalty" |
null |
null |
"apple food" |
The player was under column 3 so the player caught the item "bacon food"
. This caused the number of turns remaining to increase by 10. The item "rice food"
dropped through the bottom of the board and was not caught.
In writing your solution you must use the handleCaughtItem
method. Assume that handleCaughtItem
works as specified, regardless of what you wrote for part (a).
Complete method shiftAndCatch
.
/**
* Moves each item on the board 1 row below its previous location.
* Items on the last row are removed from the board. If an item in
* column playerCol is removed from the board it is caught by the player.
* Updates the game state if an item is caught by the player.
* Postcondition: the value of playerCol is unchanged
*/
public void shiftAndCatch()
Solution & comments
See the Droppy solution or review it with AP CS Tutor Brandon Horn.
Comment on Droppy free response