Untitled
unknown
plain_text
5 years ago
8.7 kB
9
Indexable
public ArrayList<ArrayList<Tile>> board;
/**
* 5 marks (Pass level)
*
* Constructor: create a board of size rows x columns
* containing Tiles of different colours using the
* random number generator as discussed in the assignment
* specification.
*
* Both rows and columns are going to be an integer > 0 and <= 1000.
* You can assume that rgen is going to be a valid Random object
* (rgen won't be null)
*
* You can look at the constructor for SimpleSameGame to get
* some ideas. To construct a Tile with a random colour, you
* can use.
*
* Tile random = new Tile(colors.get(rgen.nextInt(colors.size())),1);
*
* Remember that row 0 is the bottom-most row.
*
* @param rgen - the random number generator (non null)
* @param rows - the number of rows on the board, 0 < rows <= 1000
* @param columns - the number of columns on the board, 0 < columns <= 1000
*/
public SameGame(Random rgen, int rows, int columns) {
// do not modify the code involving the ArrayList colors
ArrayList<Color> colors = new ArrayList<Color>();
colors.add(Color.decode(ColorDef.BLUE));
colors.add(Color.decode(ColorDef.PURPLE));
colors.add(Color.decode(ColorDef.YELLOW));
colors.add(Color.decode(ColorDef.GREEN));
// start writing your code here
board = new ArrayList<ArrayList<Tile>>();
for(int i = 0; i < rows; i++) {
ArrayList<Tile> board2 = new ArrayList<>();
for(int k=0; k<columns; k++) {
int current = rgen.nextInt(colors.size());
board2.add(new Tile(colors.get(current),1));
}
board.add(board2);
}
}
/**
* 5 marks (Pass level)
*
* Computes the score obtained by the user at the end of the
* game. The score for the game is the number of tiles that
* the player manages to remove, so if the player removes 10
* tiles, then the player should receive a score of 10.
*
* The ideal way to complete this method is by completing the
* removeTile() method (e.g. by adding a point for each tile removed),
* but you should be able to obtain full marks even if you cannot
* implement removeTile() (see the JUnit test)
*
* @return the score obtained by the user
*/
public int calculateScore() {
// to be completed
return 0;
}
/**
* 5 marks (Pass level)
*
* Returns an instance copy of the board. Do not return a reference
* copy since the calling method may modify the returned ArrayList.
*
* @return an instance copy of the board
*/
public ArrayList<ArrayList<Tile>> getBoard() {
// to be completed
ArrayList<ArrayList<Tile>> instance = new ArrayList<ArrayList<Tile>>();
for(int i=0; i<board.size();i++) {
ArrayList<Tile> columns = new ArrayList<Tile>();
for(int j=0; j<board.get(i).size(); j++) {
columns.add(board.get(i).get(j));
}
instance.add(columns);
}
return instance;
}
/**
* 5 marks (Pass level)
*
* Checks if row i column j is a valid index, that is, if there is a
* tile on that location. Return false if i and j does not
* correspond to a valid index, or if a null value is found at
* that location.
*
* @param i - the row index
* @param j - the column index
* @return true if row i column j is a valid location that contains a Tile,
* false otherwise
*/
public boolean isValidIndex(int i, int j) {
if (i <0|| i>=board.size()) {
return false;
}else {
ArrayList<Tile> _board = board.get(i);
if(j <0|| j>=_board.size()) {
return false;
}
}
return true;
}
/**
* 10 marks (Pass level)
*
* Checks if it is legal to remove a tile on row i column j. A tile
* can be removed if there is an adjacent tile with the same colour
* (please see the assignment specification for more information).
* Return false if the location given is invalid.
*
* @param i - the row index
* @param j - the column index
* @return true if it is legal to remove the tile at row i column j,
* false otherwise
*/
public boolean isValidMove(int i, int j) {
if(!isValidIndex(i,j))
return false;
Tile c = board.get(i).get(j);
Tile up=null, down=null, next=null, prev=null;
if(i==0||i+1<board.size())
down = board.get(i+1).get(j);
if(i==board.size()-1||i-1>=0)
up = board.get(i-1).get(j);
if(j==0||j+1<board.get(i).size())
next = board.get(i).get(j+1);
if(j==board.get(i).size()-1||j-1>=0)
prev = board.get(i).get(j-1);
if (c==null) {
return false;
}
if (prev!=null) {
if (c.getColor()==prev.getColor())
return true;
}
if (next!=null) {
if (c.getColor() == next.getColor()) {
return true;
}
}
if (down!=null) {
if (c.getColor()==down.getColor())
return true;
}
if (up!=null) {
if (c.getColor() == up.getColor()) {
return true;
}
}
return false;
}
/**
* 5 marks (Pass level)
*
* Checks if the player has run out of moves, that is, if it is not possible
* to remove any more tiles from the board. Return true if there are no more
* tiles on the board.
*
* @return true if there are no more valid moves, false otherwise
*/
public boolean noMoreValidMoves() {
// to be completed
int count=0;
for(int i=0; i<board.size();i++) {
for(int j=0; j<board.get(i).size();j++) {
if(isValidMove(i,j))
count++;
}
}
return count==0;
}
/**
* 5 marks (Credit Level)
*
* Imagine two rows of Tiles, 'bottom' and 'top', with 'top' placed on top of 'bottom'.
*
* This method rearranges the two rows such that if there is an empty (i.e. null)
* slot in the 'bottom' row and there is a tile at the same index in the 'top' row,
* then it will move the tile down to 'bottom' row (effectively you are copying
* the tile into 'bottom', and set the index to null in 'top').
* You are basically moving tiles downward, but only between two rows.
*
* Don't do anything if the sizes of the two ArrayLists are not the same,
* or if one of them is null
*
* @param bottom - the bottom row of Tiles
* @param top - the top row of Tiles
*/
public void trickleDown(ArrayList<Tile> bottom, ArrayList<Tile> top) {
// to be completed
}
/** 5 marks (Credit Level)
*
* Rearranges the board so that all tiles are moved downwards and all empty columns are
* removed.
*
* You do not need to complete the deleteEmptyColumns() method to receive full
* marks for this method as it will not be tested in the JUnit test for this method,
* that is, the JUnit test for rearrangeBoard() will NOT test if your implementation
* of rearrangeBoard() removes the empty columns.
*
* However, a proper implementation of rearrangeBoard() SHOULD call deleteEmptyColumns()
* (after you move the tiles downwards), so if you manage to implement
* deleteEmptyColumns(), you need to call that method in rearrangeBoard().
*
* Hint: use the trickleDown() method (although you can also do it without
* calling trickleDown(), but calling trickleDown() will make the code simpler,
* albeit less efficient).
*
* @param board
*/
public void rearrangeBoard() {
// to be completed
}
/** 10 marks (Distinction Level)
*
* The following method removes all empty columns in the board.
* You MUST PASS testRearrangeBoard() before attempting deleteEmptyColumns,
* because the JUnit test will NOT directly test deleteEmptyColumns().
* The JUnit test will call rearrangeBoard(), assuming that you have
* implemented deleteEmptyColumns() properly and call it from rearrangeBoard().
*
*/
public void deleteEmptyColumns() {
// to be completed
}
/** 16 marks (High Distinction Level)
*
* The following method removes a tile at row i column j from the board
* and all adjacent tiles of the same colour, as per the game rules
* (as discussed in the assignment specification).
*
* The JUnit test suite will include one test that tries a large board
* (1000 x 1000) --- see testRemoveTileLarge. The removeTile method
* must finish within 10 seconds for you to pass this test (it is
* worth 5 marks)
*
* Do nothing if the given row and column index is not valid, or if it
* is not legal to move the tile.
*
* @param i - the row index of the tile to be removed
* @param j - the column index of the tile to be removed
*/
public void removeTile(int i, int j) {
// to be completed
}
}Editor is loading...