Question: this homework is about threes game. api package api; /** * Indication of direction for game moves. */ public enum Direction { LEFT, RIGHT, UP,

this homework is about threes game.

api

package api;

/**

* Indication of direction for game moves.

*/

public enum Direction

{

LEFT, RIGHT, UP, DOWN

}

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;

/**

* 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;

}

}

The questions are

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;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!