/
Board.java
121 lines (98 loc) · 2.93 KB
/
Board.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
package usantatecla.tictactoe.models;
import usantatecla.tictactoe.types.Token;
import usantatecla.utils.Direction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Board {
private Token[][] tokens;
Board() {
this.tokens = new Token[Coordinate.DIMENSION][Coordinate.DIMENSION];
for (int i = 0; i < Coordinate.DIMENSION; i++) {
for (int j = 0; j < Coordinate.DIMENSION; j++) {
this.tokens[i][j] = Token.NULL;
}
}
}
private Board(Board board) {
this();
assert board != null;
for (int i = 0; i < Coordinate.DIMENSION; i++) {
for (int j = 0; j < Coordinate.DIMENSION; j++) {
this.tokens[i][j] = board.tokens[i][j];
}
}
}
Board copy() {
return new Board(this);
}
Token getToken(Coordinate coordinate) {
assert coordinate != null && !coordinate.isNull();
return this.tokens[coordinate.getRow()][coordinate.getColumn()];
}
void put(Coordinate coordinate, Token token) {
this.tokens[coordinate.getRow()][coordinate.getColumn()] = token;
}
void move(Coordinate origin, Coordinate target) {
Token token = this.getToken(origin);
this.tokens[origin.getRow()][origin.getColumn()] = Token.NULL;
this.put(target, token);
}
boolean isCompleted() {
int tokensCount = 0;
for (int i = 0; i < Coordinate.DIMENSION; i++) {
for (int j = 0; j < Coordinate.DIMENSION; j++) {
if (!this.tokens[i][j].isNull()) {
tokensCount++;
}
}
}
return tokensCount == Coordinate.DIMENSION * 2;
}
boolean isOccupied(Coordinate coordinate, Token token) {
assert coordinate != null && !coordinate.isNull();
return this.getToken(coordinate) == token;
}
boolean isEmpty(Coordinate coordinate) {
assert coordinate != null && !coordinate.isNull();
return this.isOccupied(coordinate, Token.NULL);
}
boolean isTicTacToe(Token token) {
assert token != null && !token.isNull();
List<Coordinate> coordinates = this.getCoordinates(token);
if (coordinates.size() < Coordinate.DIMENSION) {
return false;
}
Direction[] directions = new Direction[Coordinate.DIMENSION - 1];
for (int i = 0; i < Coordinate.DIMENSION - 1; i++) {
directions[i] = coordinates.get(i).getDirection(coordinates.get(i + 1));
if (directions[i] == Direction.NULL || i > 0 && directions[i - 1] != directions[i]) {
return false;
}
}
return true;
}
private List<Coordinate> getCoordinates(Token token) {
assert token != null && !token.isNull();
List<Coordinate> coordinates = new ArrayList<Coordinate>();
for (int i = 0; i < Coordinate.DIMENSION; i++) {
for (int j = 0; j < Coordinate.DIMENSION; j++) {
if (this.tokens[i][j] == token) {
coordinates.add(new Coordinate(i, j));
}
}
}
return coordinates;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Board other = (Board) obj;
return Arrays.deepEquals(tokens, other.tokens);
}
}