Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to write a java program that plays a 'game of Threes': The game consists of a grid of moveable tiles. Each tile contains

I need to write a java program that plays a 'game of Threes': The game consists of a grid of moveable tiles. Each tile contains a number 1, 2, 3, or a value that results from starting with a 3 and doubling a bunch of times. We will think of the grid as a 2D array of integer values, where the value 0 means there is no tile in that cell. There are only four possible operations: to "shift" the grid up, down, left, or right. What this means is that the tiles are shifted in the indicated direction, and certain combinations of tiles may be merged if "pushed" together against one of the boundaries. The exact rules for merging are discussed in a later section. The screenshot on the right shows the result of shifting in the "up" direction. The two 12's are "pushed" against the top and merge to make 24. In addition, the 1 and 2 in the second column are merged to make a 3. All other tiles that can move (the two 2's in this case) are shifted up as well. Whenever the grid is shifted in some direction, a new tile appears on the opposite side. The game ends when the grid can't be shifted in any direction because there are no empty cells and no merges are possible. The object of the game is to get the highest possible score. The score is the sum, for all tiles on the grid, of a predetermined number associated with each individual tile value.

I need help with the class Game.java, these are the methods, along with the descriptions of what they are supposed to do:

package hw3;

import java.util.ArrayList;

import java.util.Random;

import api.Direction;

import api.Move;

import api.TilePosition;

/**

* The Game class contains the state and logic for an implementation of a video

* game such as "Threes". The basic underlying state is an n by n grid of tiles,

* represented by integer values. A zero in a cell is considered to be *

* "empty". To play the game, a client calls the method

* shiftGrid(), selecting one of the four directions (LEFT, RIGHT,

* UP, DOWN). Each row or column is then shifted according to the rules

* encapsulated in the associated GameUtil object. The move is

* completed by calling newTile, which makes a new tile appear in

* the grid in preparation for the next move.

*

* In between the call to shiftGrid() and the call to

* newTile, the client may also call undo(), which

* reverts the grid to its state before the shift.

*

* The game uses an instance of java.util.Random to generate new tile values and

* to select the location for a new tile to appear. The new values are generated

* by the associated GameUtil's

* generateRandomTileValue method, and the new positions are

* generated by the GameUtil's

* generateRandomTilePosition method. The values are always

* generated one move ahead and stored, in order to support the ability for a UI

* to provide a preview of the next tile value.

*

* The score is the sum over all cells of the individual scores returned by the

* GameUtil's getScoreForValue() method.

*/

public class Game {

/**

* Constructs a game with a grid of the given size, using a default random

* number generator. The initial grid is produced by the

* initializeNewGrid method of the given GameUtil

* object.

*

* @param givenSize

* size of the grid for this game

* @param givenConfig

* given instance of GameUtil

*/

public Game(int givenSize, GameUtil givenConfig) {

// just call the other constructor

this(givenSize, givenConfig, new Random());

}

/**

* Constructs a game with a grid of the given size, using the given instance of

* Random for the random number generator. The initial grid is

* produced by the initializeNewGrid method of the given

* GameUtil object.

*

* @param givenSize

* size of the grid for this game

* @param givenConfig

* given instance of GameUtil

* @param givenRandom

* given instance of Random

*/

public Game(int givenSize, GameUtil givenConfig, Random givenRandom) {

// TODO

}

/**

* Returns the value in the cell at the given row and column.

*

* @param row

* given row

* @param col

* given column

* @return value in the cell at the given row and column

*/

public int getCell(int row, int col) {

// TODO

return 0;

}

/**

* Sets the value of the cell at the given row and column. NOTE: This method

* should not be used by clients outside of a testing environment.

*

* @param row

* given row

* @param col

* given col

* @param value

* value to be set

*/

public void setCell(int row, int col, int value) {

// TODO

}

/**

* Returns the size of this game's grid.

*

* @return size of the grid

*/

public int getSize() {

// TODO

return 0;

}

/**

* Returns the current score.

*

* @return score for this game

*/

public int getScore() {

// TODO

return 0;

}

/**

* Copy a row or column from the grid into a new one-dimensional array. There

* are four possible actions depending on the given direction:

*

*

LEFT - the row indicated by the index rowOrColumn is copied

* into the new array from left to right

*

RIGHT - the row indicated by the index rowOrColumn is copied

* into the new array in reverse (from right to left)

*

UP - the column indicated by the index rowOrColumn is copied

* into the new array from top to bottom

*

DOWN - the row indicated by the index rowOrColumn is copied

* into the new array in reverse (from bottom to top)

*

*

* @param rowOrColumn

* index of the row or column

* @param dir

* direction from which to begin copying

* @return array containing the row or column

*/

public int[] copyRowOrColumn(int rowOrColumn, Direction dir) {

// TODO

return null;

}

/**

* Updates the grid by copying the given one-dimensional array into a row or

* column of the grid. There are four possible actions depending on the given

* direction:

*

*

LEFT - the given array is copied into the the row indicated by the index

* rowOrColumn from left to right

*

RIGHT - the given array is copied into the the row indicated by the index

* rowOrColumn in reverse (from right to left)

*

UP - the given array is copied into the column indicated by the index

* rowOrColumn from top to bottom

*

DOWN - the given array is copied into the column indicated by the index

* rowOrColumn in reverse (from bottom to top)

*

*

* @param arr

* the array from which to copy

* @param rowOrColumn

* index of the row or column

* @param dir

* direction from which to begin copying

*/

public void updateRowOrColumn(int[] arr, int rowOrColumn, Direction dir) {

// TODO

}

/**

* Plays one step of the game by shifting the grid in the given direction.

* Returns a list of Move objects describing all moves performed. All Move

* objects must include a valid value for getRowOrColumn() and

* getDirection(). If no cells are moved, the method returns an

* empty list.

*

* The shift of an individual row or column is performed by the method

* shiftArray of GameUtil.

*

* The score is not updated.

*

* @param dir

* direction in which to shift the grid

* @return list of moved or merged tiles

*/

public ArrayList shiftGrid(Direction dir) {

// TODO

return null;

}

/**

* Reverts the shift performed in a previous call to shiftGrid(),

* provided that neither newTile() nor undo() has been

* called. If there was no previous call to shiftGrid() without a

* newTile() or undo(), this method does nothing and

* returns false; otherwise returns true.

*

* @return true if the previous shift was undone, false otherwise

*/

public boolean undo() {

// TODO

return false;

}

/**

* Generates a new tile and places its value in the grid, provided that there

* was a previous call to shiftGrid without a corresponding call to

* undo or newTile. The tile's position is determined

* according to the generateRandomTilePosition of this game's

* associated GameUtil object. If there was no previous call to

* shiftGrid without an undo or newTile,

* this method does nothing and returns null; otherwise returns a

* TilePosition object with the new tiles's position and value.

* Note that the returned tile's value should match the current value

* returned by getNextTileValue, and if this method returns a

* non-null value the upcoming tile value should be updated according to

* generateRandomTileValue(). This method should update the total

* score and the score should include the newly generated tile.

*

* @return TilePosition containing the new tile's position and value, or null if

* no new tile is created

*/

public TilePosition newTile() {

// TODO

return null;

}

/**

* Returns the value that will appear on the next tile generated in a call to

* newTile. This is an accessor method that does not modify the

* game state.

*

* @return value to appear on the next generated tile

*/

public int getNextTileValue() {

// TODO

return 0;

}

}

package api;

/**

* Data container for describing the position and value of a

* new "tile" in a Threes game.

*/

public class TilePosition

{

/**

* Row index.

*/

private final int row;

/**

* Column index.

*/

private final int col;

/**

* Number on the tile.

*/

private int value;

/**

* Constructs a new TilePosition with the given row, column, and value.

* @param givenRow

* row index for this

* @param givenCol

* column index for this tile

* @param givenValue

* value for this tile

*/

public TilePosition(int givenRow, int givenCol, int givenValue)

{

row = givenRow;

col = givenCol;

value = givenValue;

}

/**

* Returns the row index for this tile position.

* @return

* row index

*/

public int getRow()

{

return row;

}

/**

* Returns the column index for this tile position.

* @return

* column index

*/

public int getCol()

{

return col;

}

/**

* Returns the value for this tile position.

* @return

* value on this tile

*/

public int getValue()

{

return value;

}

/**

* Sets the value for this tile position.

* @param

* value to be set

*/

public void setValue(int value)

{

this.value = value;

}

/**

* Determines whether this object is equal to the given object.

* @return

* true if the given object is a TilePosition with the same

* attributes as this one

*/

public boolean equals(Object obj)

{

if (obj == null || obj.getClass() != this.getClass())

{

return false;

}

TilePosition other = (TilePosition) obj;

return row == other.row && col == other.col && value == other.value;

}

/**

* Returns a string description of this object.

* @return

* a string description of this object

*/

public String toString()

{

return "Position (" + row + ", " + col + ") value " + value;

}

}

package api;

/**

* A Move object describes the motion of one tile (or two tiles in the case

* of a merge move) in the Threes game. A move is represented

* by the old and new indices of the tile or tiles within a one-dimensional array.

* In addition, it is possible to also specify a Direction and a row or column

* index, so that the move could be interpreted as taking place in a two-dimensional

* grid. For example, given a Move with startIndex 0, newIndex 1,

* rowOrColumn 2 and direction UP, in a 5 x 5 grid it would

* correspond to a move from (4, 2) to (3, 2).

*/

public class Move

{

/**

* Starting index of the tile being moved, or, in the case of a merge,

* starting index of the right tile of the pair being merged.

*/

private final int startIndex;

/**

* In case of a merge, represents the starting index of the left

* tile of the pair being merged.

*/

private final int startIndex2;

/**

* Ending index of the tile or tiles.

*/

private final int endIndex;

/**

* Indicates whether this is a merge move or not.

*/

private final boolean merged;

/**

* Current value in the tile to be moved, or, in the case of a merge,

* current value of the right tile of the pair being merged.

*/

private final int value;

/**

* In case of a merge, represents the current value of the left

* tile of the pair being merged.

*/

private final int value2;

/**

* Value on the tile after the move.

*/

private final int newValue;

/**

* Row or column in the grid (represents a row if direction

* is LEFT or RIGHT, represents a column if direction is UP or DOWN).

*/

private int rowOrColumn;

/**

* Direction of the move in the grid.

*/

private Direction dir;

/**

* Constructs a single-tile move from oldPos to newPos. Caller should

* ensure that newPos is strictly less than oldPos.

* @param oldIndex

* old index of the tile within an array

* @param newIndex

* new index of the tile within an array

* @param value

* current value of the tile

*/

public Move(int oldIndex, int newIndex, int value)

{

this.startIndex = oldIndex;

this.startIndex2 = -1; // ignored

this.endIndex = newIndex;

this.newValue = value;

this.value = value;

this.value2 = -1; // ignored

merged = false;

dir = null;

rowOrColumn = -1;

}

/**

* Constructs a merge move from startIndex to endIndex, assuming

* that the left tile of the pair being merged is at startIndex - 1.

* @param startIndex

* old index of the right tile of the pair being merged

* @param endIndex

* new index of the right tile of the pair being merged

* @param currentValue

* current value of the right tile of the pair

* @param currentValue2

* current value of the left tile

* @param newValue

* new value on the tile after merging

*/

public Move(int startIndex, int endIndex, int currentValue, int currentValue2, int newValue)

{

this.startIndex = startIndex;

this.startIndex2 = startIndex - 1;

this.endIndex = endIndex;

this.value = currentValue;

this.value2 = currentValue2;

this.newValue = newValue;

merged = true;

dir = null;

rowOrColumn = -1;

}

/**

* Constructs a merge move from tiles at startIndex and startIndex2 to

* endIndex, where the left tile of the pair being merged is at startIndex2.

* @param startIndex

* old index of the right tile of the pair being merged

* @param startIndex2

* old index of the left tile of the pair being merged

* @param endIndex

* new index of the right tile of the pair being merged

* @param currentValue

* current value of the right tile of the pair

* @param currentValue2

* current value of the left tile

* @param newValue

* new value on the tile after merging

*/

public Move(int startIndex, int startIndex2, int endIndex, int currentValue, int currentValue2, int newValue)

{

this.startIndex = startIndex;

this.startIndex2 = startIndex2;

this.endIndex = endIndex;

this.value = currentValue;

this.value2 = currentValue2;

this.newValue = newValue;

merged = true;

dir = null;

rowOrColumn = -1;

}

/**

* Sets a direction and row/column index for interpreting this move within a grid.

* @param givenRowOrColumn

* row or column index

* @param givenDirection

* direction to set

*/

public void setDirection(int givenRowOrColumn, Direction givenDirection)

{

dir = givenDirection;

rowOrColumn = givenRowOrColumn;

}

/**

* Returns the old index of the first (or only) tile represented by this move.

* @return

* index of first tile

*/

public int getOldIndex()

{

return startIndex;

}

/**

* Returns the new index of the tile or tiles represented by this move.

* @return

* new index for move

*/

public int getNewIndex()

{

return endIndex;

}

/**

* Determines whether this is a merge move or a single tile move.

* @return

* true if this is a merge move, false otherwise

*/

public boolean isMerged()

{

return merged;

}

/**

* Returns a direction for interpreting this move in a 2D grid.

* @return

* direction for this move, or null if none has been set

*/

public Direction getDirection()

{

return dir;

}

/**

* Returns a row or column index for interpreting this move in a 2D grid.

* @return

* row or column index, or -1 if none has been set

*/

public int getRowOrColumn()

{

return rowOrColumn;

}

/**

* Returns the current (old) value of the tile or tiles represented by this move.

* @return

* value of tiles in this move

*/

public int getValue()

{

return value;

}

public int getNewValue()

{

return newValue;

}

/**

* Determines whether this Move object is equal to the given object.

* @return

* true if the given object is a Move and all attributes are the same as those

* in this Move

*/

public boolean equals(Object obj)

{

if (obj == null || !(obj instanceof Move))

{

return false;

}

Move other = (Move) obj;

return (startIndex == other.startIndex &&

endIndex == other.endIndex &&

value == other.value &&

rowOrColumn == other.rowOrColumn &&

dir == other.dir &&

merged == other.merged);

}

/**

* Returns a string description of this move.

* @return

* a string description of this move

*/

public String toString()

{

String rowAndDirection = "";

if (rowOrColumn >= 0 && dir != null)

{

if (dir == Direction.UP || dir == Direction.DOWN)

{

rowAndDirection = " (column " + rowOrColumn + " " + dir + ")";

}

else if (dir == Direction.LEFT || dir == Direction.RIGHT)

{

rowAndDirection = " (row " + rowOrColumn + " " + dir + ")";

}

}

if (merged)

{

return "Merge " + startIndex + " to " + endIndex + rowAndDirection;

}

else

{

return "Move " + startIndex + " to " + endIndex + rowAndDirection;

}

}

}

package api;

/**

* Indication of direction for game moves.

*/

public enum Direction

{

LEFT, RIGHT, UP, DOWN

}

I ALREADY HAVE THE GAMEUTIL.JAVA CLASS, I NEED HELP WITH GAME.JAVA

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

Group Size and Communication

Answered: 1 week ago

Question

Understanding Group Roles

Answered: 1 week ago