model
Class ClearCellGameModel

java.lang.Object
  extended by model.GameModel
      extended by model.ClearCellGameModel

public class ClearCellGameModel
extends GameModel

This class extends GameModel and implements the logic of the clear cell game. We define an empty cell as BoardCell.EMPTY. An empty row is defined as one where every cell corresponds to BoardCell.EMPTY.

Author:
Dept of Computer Science, UMCP

Constructor Summary
ClearCellGameModel(int maxRows, int maxCols, java.util.Random random)
          Defines a board with empty cells.
 
Method Summary
 int getScore()
          Returns the game score.
 boolean isGameOver()
          The game is over when the last board row (row with index board.length -1) is different from empty row.
 void nextAnimationStep()
          This method will attempt to insert a row of random BoardCell objects at the top of the board (row with index 0) if the last board row (row with index board.length -1) corresponds to the empty row; Otherwise no operation will take place.
 void processCell(int rowIndex, int colIndex)
          This method will turn to BoardCell.EMPTY any cells adjacent (surrounding) the cell at position [rowIndex][colIndex] if and only if those adjacent cells have the same color as the selected cell.
 
Methods inherited from class model.GameModel
getBoardCell, getMaxCols, getMaxRows, setBoardCell, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClearCellGameModel

public ClearCellGameModel(int maxRows,
                          int maxCols,
                          java.util.Random random)
Defines a board with empty cells. It relies on the super class constructor to define the board.

Parameters:
maxRows -
maxCols -
random -
Method Detail

isGameOver

public boolean isGameOver()
The game is over when the last board row (row with index board.length -1) is different from empty row.

Specified by:
isGameOver in class GameModel

getScore

public int getScore()
Returns the game score.

Specified by:
getScore in class GameModel

nextAnimationStep

public void nextAnimationStep()
This method will attempt to insert a row of random BoardCell objects at the top of the board (row with index 0) if the last board row (row with index board.length -1) corresponds to the empty row; Otherwise no operation will take place. The random row of cells will be generated using the getNonEmptyRandomBoardCell. This is the method that is called by a timer in order to create the animated game.

Specified by:
nextAnimationStep in class GameModel

processCell

public void processCell(int rowIndex,
                        int colIndex)
This method will turn to BoardCell.EMPTY any cells adjacent (surrounding) the cell at position [rowIndex][colIndex] if and only if those adjacent cells have the same color as the selected cell. The selected cell will also be turned into a BoardCell.EMPTY. Notice that the maximum number of adjacent cells is eight. If the cell at [rowIndex][colIndex] corresponds to an empty cell no action will take place. Clearing a cell adds one point to the game's score.

If after processing the adjacent cells any rows in the board are empty then those rows will collapse, moving non-empty rows upward. For example, if we have the following board (an * represents an empty cell):

RRR
GGG
YYY
* * *

then processing each cell of the second row will generate the following board

RRR
YYY
* * *
* * *

Specified by:
processCell in class GameModel
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.


Web Accessibility