Package model
Class ClearCellGameModel
- java.lang.Object
-
- model.GameModel
-
- model.ClearCellGameModel
-
public class ClearCellGameModel extends GameModel
This class extends GameModel and implements the logic of the clear cell game, specifically.- Author:
- Dept of Computer Science, UMCP
-
-
Constructor Summary
Constructors Constructor Description ClearCellGameModel(int rows, int cols, java.util.Random random)
Defines a board with empty cells.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getScore()
Returns the player's score.boolean
isGameOver()
The game is over when the last row (the one with index equal to board.length -1) contains at least one cell that is not empty.void
nextAnimationStep()
This method must do nothing in the case where the game is over.void
processCell(int rowIndex, int colIndex)
This method is called when the user clicks a cell on the board.-
Methods inherited from class model.GameModel
getBoard, getBoardCell, getCols, getRows, setBoardCell, toString
-
-
-
-
Constructor Detail
-
ClearCellGameModel
public ClearCellGameModel(int rows, int cols, java.util.Random random)
Defines a board with empty cells. It relies on the super class constructor to define the board.- Parameters:
rows
- number of rows in boardcols
- number of columns in boardrandom
- random number generator to be used during game when rows are randomly created
-
-
Method Detail
-
isGameOver
public boolean isGameOver()
The game is over when the last row (the one with index equal to board.length -1) contains at least one cell that is not empty.- Specified by:
isGameOver
in classGameModel
-
getScore
public int getScore()
Returns the player's score. The player should be awarded one point for each cell that is cleared.
-
nextAnimationStep
public void nextAnimationStep()
This method must do nothing in the case where the game is over. As long as the game is not over yet, this method will do the following: 1. Shift the existing rows down by one position. 2. Insert a row of random BoardCell objects at the top of the board. The row will be filled from left to right with cells obtained by calling BoardCell.getNonEmptyRandomBoardCell(). (The Random number generator passed to the constructor of this class should be passed as the argument to this method call.)- Specified by:
nextAnimationStep
in classGameModel
-
processCell
public void processCell(int rowIndex, int colIndex)
This method is called when the user clicks a cell on the board. If the selected cell is not empty, it will be set to BoardCell.EMPTY, along with any adjacent cells that are the same color as this one. (This includes the cells above, below, to the left, to the right, and all in all four diagonal directions.) If any rows on the board become empty as a result of the removal of cells then those rows will "collapse", meaning that all non-empty rows beneath the collapsing row will shift upward.- Specified by:
processCell
in classGameModel
- Parameters:
rowIndex
- row that user has clickedcolIndex
- column that user has clicked- Throws:
java.lang.IllegalArgumentException
- with message "Invalid row index" for invalid row or "Invalid column index" for invalid column. We check for row validity first.
-
-