-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardModel.java
239 lines (211 loc) · 6.37 KB
/
BoardModel.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package distsys;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
/**
* This class contains the board data represented as a two dimensional
* array of Cell. Each Cell can contain a single char (e.g. X or O).
*/
final class BoardModel implements TableModel
{
private final Cell boardCells[][];
private final List<TableModelListener> listeners = new ArrayList<TableModelListener>();
BoardModel(int boardSize)
{
boardCells = new Cell[boardSize][];
for (int x = 0; x < boardSize; x++) {
boardCells[x] = new Cell[boardSize];
for (int y = 0; y < boardSize; y++)
boardCells[x][y] = new Cell();
}
}
/**
* Does (x,y) have a mark already?
*
* @param x X coordinate of cell to check
* @param y Y coordinate of cell to check
* @return true if the cell is empty, otherwise false.
*/
boolean isEmpty(int x, int y)
{
return boardCells[x][y].isEmpty();
}
/**
* Set a cell to contain a given mark and return true of this gives 5 in a row.
*
* @param x X coordinate of cell to update
* @param y Y coordinate of cell to update
* @param mark Mark to assign to (x,y)
* @return true if the new mark resulted in 5 in a row, false otherwise.
*/
boolean setCell(int x, int y, char mark)
{
boardCells[x][y].setContents(mark);
TableModelEvent event = new TableModelEvent(this, y, y, x);
for (TableModelListener listener : listeners)
listener.tableChanged(event);
// Check if this new mark gave 5 in a row
return checkHorizontal(x, y) || checkVertical(x, y) || checkDiagonalUp(x, y) || checkDiagonalDown(x, y);
}
/**
* Check if (x,y) is part of a horizontal 5 in a row.
*
* @param x X coordinate of cell to check
* @param y Y coordinate of cell to check
* @return true if there are 5 in a row including (x,y)
*/
private boolean checkHorizontal(int x, int y)
{
// Find the minimum and maximum x values for this y which have the same mark
int minX = x;
while (minX > 0 && boardCells[minX - 1][y].equals(boardCells[x][y]))
minX--;
int maxX = x;
while ((maxX + 1) < boardCells.length && boardCells[maxX + 1][y].equals(boardCells[x][y]))
maxX++;
// If the difference is larger than 4, we have 5 in a row
return maxX - minX >= 4;
}
/**
* Check if (x,y) is part of a vertical 5 in a row.
*
* @param x X coordinate of cell to check
* @param y Y coordinate of cell to check
* @return true if there are 5 in a row including (x,y)
*/
private boolean checkVertical(int x, int y)
{
// Find the minimum and maximum y values for this x which have the same mark
int minY = y;
while (minY > 0 && boardCells[x][minY - 1].equals(boardCells[x][y]))
minY--;
int maxY = y;
while ((maxY + 1) < boardCells.length && boardCells[x][maxY + 1].equals(boardCells[x][y]))
maxY++;
// If the difference is larger than 4, we have 5 in a row
return maxY - minY >= 4;
}
/**
* Check if (x,y) is part of a diagonal 5 in a row. Only checks
* diagonals going from bottom left to upper right.
*
* @param x X coordinate of cell to check
* @param y Y coordinate of cell to check
* @return true if there are 5 in a row including (x,y)
*/
private boolean checkDiagonalUp(int x, int y)
{
// Find the minimum and maximum x and y values which have the same mark
int minX = x;
int minY = y;
while (minX > 0 && minY > 0 && boardCells[minX - 1][minY - 1].equals(boardCells[x][y])) {
minX--;
minY--;
}
int maxX = x;
int maxY = y;
while ((maxX + 1) < boardCells.length && (maxY + 1) < boardCells.length &&
boardCells[maxX + 1][maxY + 1].equals(boardCells[x][y])) {
maxX++;
maxY++;
}
// If the difference is larger than 4, we have 5 in a row
// We only have to check X (or Y) as the difference is the same for both
return maxX - minX >= 4;
}
/**
* Check if (x,y) is part of a diagonal 5 in a row. Only checks
* diagonals going from upper left to bottom right.
*
* @param x X coordinate of cell to check
* @param y Y coordinate of cell to check
* @return true if there are 5 in a row including (x,y)
*/
private boolean checkDiagonalDown(int x, int y)
{
// Find the minimum and maximum x and y values which have the same mark
int minX = x;
int maxY = y;
while (minX > 0 && (maxY + 1) < boardCells.length &&
boardCells[minX - 1][maxY + 1].equals(boardCells[x][y])) {
minX--;
maxY++;
}
int maxX = x;
int minY = y;
while ((maxX + 1) < boardCells.length && minY < 0 &&
boardCells[maxX + 1][minY - 1].equals(boardCells[x][y])) {
maxX++;
minY--;
}
// If the difference is larger than 4, we have 5 in a row
// We only have to check X (or Y) as the difference is the same for both
return maxX - minX >= 4;
}
// Below is the implementation of the TableModel interface.
public int getRowCount()
{
return boardCells.length;
}
public int getColumnCount()
{
return boardCells.length;
}
public String getColumnName(int columnIndex)
{
return String.valueOf(columnIndex + 1);
}
public Class<?> getColumnClass(int columnIndex)
{
return Object.class;
}
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false;
}
public Object getValueAt(int rowIndex, int columnIndex)
{
return boardCells[columnIndex][rowIndex];
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex)
{
}
public void addTableModelListener(TableModelListener l)
{
listeners.add(l);
}
public void removeTableModelListener(TableModelListener l)
{
listeners.remove(l);
}
public String doMove() throws RemoteException {
return null;
}
/**
* Class representing a single cell in the board.
*/
private final class Cell
{
private char contents = ' ';
void setContents(char contents)
{
this.contents = contents;
}
boolean isEmpty()
{
return contents == ' ';
}
public String toString()
{
return String.valueOf(contents);
}
public boolean equals(Object obj)
{
return obj instanceof Cell &&
contents == ((Cell)obj).contents;
}
}
}