-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameModel.java
113 lines (91 loc) · 2.82 KB
/
GameModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package model;
/**
* This class represents the logic of a game where a
* board is updated on each step of the game animation.
* The board can also be updated by selecting a board cell.
*
*/
public abstract class GameModel {
protected BoardCell[][] board;
/**
* Creates a rectangular board of the specified size, filling it with
* BoardCell.EMPTY cells.
* @param rows number of rows on the board
* @param cols number of columns on the board
*/
public GameModel(int rows, int cols) {
board = new BoardCell[rows][cols];
//Traverses through entire board and sets each cell to empty
for(int row = 0; row < board.length; row++) {
for(int col = 0; col < board[0].length; col++) {
board[row][col] = BoardCell.EMPTY;
}
}
}
/** Returns the number of rows on the board
*
* @return number of rows
*/
public int getRows() {
return board.length;
}
/** Returns the number of columns on the board
*
* @return number of columns
*/
public int getCols() {
return board[0].length;
}
/**
* Returns a reference to the board without making any kind of copy.
*
* @return the board
*/
public BoardCell[][] getBoard() {
return board; // This is done for you. Just return the reference.
}
/** Sets the cell at the specified location to value provided.
*
* @param rowIndex row to set
* @param colIndex column to set
* @param boardCell value to assign to the board
*/
public void setBoardCell(int rowIndex, int colIndex, BoardCell boardCell) {
board[rowIndex][colIndex] = boardCell;
}
/** Returns the value at a given location of the board
*
* @param rowIndex row to access
* @param colIndex column to access
* @return value of board at the specified location
*/
public BoardCell getBoardCell(int rowIndex, int colIndex) {
return board[rowIndex][colIndex];
}
/** Provides a string representation of the board
* We have implemented this method for you.
*/
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Board(Rows: " + board.length + ", Cols: " + board[0].length + ")\n");
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++)
buffer.append(board[row][col].getName());
buffer.append("\n");
}
return buffer.toString();
}
public abstract boolean isGameOver();
public abstract int getScore();
/**
* Advances the animation one step.
*/
public abstract void nextAnimationStep();
/**
* Adjust the board state according to the current board
* state and the selected cell.
* @param rowIndex row that user has clicked
* @param colIndex column that user has clicked
*/
public abstract void processCell(int rowIndex, int colIndex);
}