Need help with computer science: Arrays

User Generated

glcbeg

Computer Science

Description

Have a look at the following images and please post the solution

Unformatted Attachment Preview

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. 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 handleCaught Item below. /** 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 turns Left: 50 board: 0 1 N 3 4 0 null "eggs food" null "diamond prize" null 1 null "glue penalty" null null "apple food" N "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 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 handleCaught Item works as specified, regardless of what you wrote for part (a). Complete method shiftAndCatch below. /** 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()
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer


Anonymous
Very useful material for studying!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags