-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeBoard.java
More file actions
139 lines (119 loc) · 3.95 KB
/
Copy pathTicTacToeBoard.java
File metadata and controls
139 lines (119 loc) · 3.95 KB
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
package boards;
import api.Rule;
import api.RuleSet;
import game.*;
import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
public class TicTacToeBoard implements CellBoard, Cloneable {
String[][] cells = new String[3][3];
History history = new History();
public TicTacToeBoard() {
}
private TicTacToeBoard(TicTacToeBoard board) {
for (int i = 0; i < 3; i++) {
this.cells[i] = Arrays.copyOf(board.cells[i], 3); // Deep copy of each row
}
}
public History getHistory() {
return history;
}
public String getSymbol(int i, int j){
return this.cells[i][j];
}
public void setCell(Cell cell, String symbol) {
if(cells[cell.getRow()][cell.getCol()] == null){
cells[cell.getRow()][cell.getCol()] = symbol;
} else{
throw new IllegalArgumentException();
}
}
public static RuleSet getRules(){
RuleSet rules = new RuleSet();
rules.add(new Rule((board)->outerTraversal(board::getSymbol)));
rules.add(new Rule ((board)->outerTraversal((i,j)-> board.getSymbol(j,i))));
rules.add(new Rule ((board)->traverse((i)-> board.getSymbol(i,i))));
rules.add(new Rule ((board)->traverse((i)-> board.getSymbol(i,2-i))));
rules.add(new Rule (TicTacToeBoard::countMoves));
return rules;
}
@Override
public TicTacToeBoard copy() {
TicTacToeBoard ticTacToeBoard = new TicTacToeBoard();
for (int i = 0; i < 3; i++) {
System.arraycopy(cells[i], 0, ticTacToeBoard.cells[i], 0, 3);
}
ticTacToeBoard.history = history;
return ticTacToeBoard;
}
@Override
public TicTacToeBoard move(Move move){
TicTacToeBoard boardCopy = copy();
history.add(boardCopy);
setCell(move.getCell(),move.getPlayer().symbol());
return this;
}
public TicTacToeBoard dummyMove(Move move){
TicTacToeBoard boardCopy = copy();
boardCopy.setCell(move.getCell(),move.getPlayer().symbol());
return boardCopy;
}
@Override
public String toString(){
StringBuilder result = new StringBuilder();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(cells[i][j]==null) result.append("-");
else result.append(cells[i][j]);
}
result.append("\n");
}
return result.toString();
}
@Override
public TicTacToeBoard clone() {
return new TicTacToeBoard(this);
}
public static GameState countMoves(CellBoard board1){
int count=0;
GameState gameState = new GameState(false, "-");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board1.getSymbol(i,j)!=null){
count ++;
}
}
}
if(count == 9){
gameState = new GameState(true,"-");
}
return gameState;
}
public static GameState outerTraversal(BiFunction<Integer, Integer, String> next){
GameState result = new GameState(false, "-");
for (int i = 0; i < 3; i++) {
int finalI = i;
Function<Integer,String> nextValue = (j) -> next.apply(finalI,j);
GameState traversal = traverse(nextValue);
if(traversal.isOver()) {
result = traversal;
break;
}
}
return result;
}
public static GameState traverse(Function<Integer, String> next){
GameState result = new GameState(false, "-");
boolean possibleStreak = true;
for (int i = 0; i < 3; i++) {
if (next.apply(i)== null || !next.apply(0).equals(next.apply(i))) {
possibleStreak = false;
break;
}
}
if (possibleStreak) {
result = new GameState(true, next.apply(0));
}
return result;
}
}