-
Notifications
You must be signed in to change notification settings - Fork 2
/
Model.java
59 lines (53 loc) · 1.26 KB
/
Model.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
public class Model {
private int[][] board;
private boolean gameOver;
private boolean winnerCalled;
private int winner = Position.CONTINUE;
public Model() {
board = new int[3][3];
}
public void select(Location square) {
int row = square.getRow();
int col = square.getColumn();
if (!gameOver && board[row][col] == 0 &&
row >= 0 && row < 3 && col >= 0 && col < 3) {
board[row][col] = Position.X;
Position response = new Position(board, true, square);
Location move = response.getMove();
if (move != null) {
board[move.getRow()][move.getColumn()] = Position.O;
}
}
int evaluation = Position.eval(board);
if (evaluation != Position.CONTINUE) {
winner = evaluation;
gameOver = true;
}
}
public String getLabel(Location square) {
int row = square.getRow();
int col = square.getColumn();
int val = board[row][col];
switch (val) {
case 0:
return "";
case Position.X:
return "X";
case Position.O:
return "O";
default:
throw new IllegalStateException(
"The board has the invalid entry " + val + ".");
}
}
public boolean gameIsOver() {
return gameOver;
}
public boolean getWinnerCalled() {
return winnerCalled;
}
public int getWinner() {
winnerCalled = true;
return winner;
}
}