-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMnkBoard.java
More file actions
166 lines (145 loc) · 4.54 KB
/
MnkBoard.java
File metadata and controls
166 lines (145 loc) · 4.54 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
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
package ticTacToe;
import java.util.Arrays;
import java.util.Map;
public class MnkBoard implements Board, Position {
private static final Map<Cell, String> SYMBOLS = Map.of(
Cell.X, "\033[0;35mX\033[0m",
Cell.O, "\033[0;36mO\033[0m",
Cell.E, "\u00B7"
);
private final Cell[][] cells;
private Cell turn;
public int m, n, k;
private int totalEmpty;
private boolean doCompress;
public MnkBoard(int m, int n, int k, boolean doCompress) {
this.m = m;
this.n = n;
this.k = k;
this.totalEmpty = m * n;
this.cells = new Cell[m][n];
this.doCompress = doCompress;
for (Cell[] row : cells) {
Arrays.fill(row, Cell.E);
}
turn = Cell.X;
}
public MnkBoard() {
this(3,3,3, false);
}
public MnkBoard(MnkBoard otherBoard, int m, int n, int k) {
this.m = m;
this.n = n;
this.k = k;
this.doCompress = false;
turn = otherBoard.getCell();
cells = new Cell[m][n];
totalEmpty = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cells[i][j] = otherBoard.getCell(i, j);
totalEmpty += cells[i][j] == Cell.E ? 1 : 0;
}
}
}
public int getTotalEmpty() {
return totalEmpty;
}
@Override
public Position getPosition() {
return new BoardInterface(this);
}
@Override
public Cell getCell() {
return turn;
}
@Override
public Result makeMove(final Move move) {
if (!isValid(move)) {
return Result.LOSE;
}
cells[move.getRow()][move.getColumn()] = move.getValue();
totalEmpty--;
int colMax = 0;
int rowMax = 0;
int diag1Max = 0;
int diag2Max = 0;
for (int xDir = -1; xDir <= 1; xDir++) {
for (int yDir = -1; yDir <= 1; yDir++) {
if (xDir == 0 && yDir == 0) {
continue;
}
if (check(move.getRow(), move.getColumn(), xDir, yDir, move.getValue())) {
return Result.WIN;
}
}
}
totalEmpty--;
if (totalEmpty == 0) {
return Result.DRAW;
}
turn = turn == Cell.X ? Cell.O : Cell.X;
// turn = turn == Cell.X ? Cell.O : (turn == Cell.O ? Cell.A : (turn == Cell.A ? Cell.B : Cell.X));
return Result.UNKNOWN;
}
private boolean check(int startx, int starty, int xDir, int yDir, Cell val) {
int len = 0;
for (int diff = -k; diff <= k; diff++) {
if (checkPos(startx + xDir * diff, starty + yDir * diff, val)) {
len++;
if (len == k) {
return true;
}
} else {
len = 0;
}
}
return false;
}
private boolean checkPos(int x, int y, Cell val) {
if (x < 0 || y < 0 || x >= m || y >= n) {
return false;
}
return val == cells[x][y];
}
@Override
public boolean isValid(final Move move) {
return 0 <= move.getRow() && move.getRow() < m
&& 0 <= move.getColumn() && move.getColumn() < n
&& cells[move.getRow()][move.getColumn()] == Cell.E
&& turn == move.getValue();
}
@Override
public Cell getCell(final int r, final int c) {
return cells[r][c];
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder(" ");
String columnFormatString = buildFormattingString(n);
String rowFormatString = buildFormattingString(m);
sb.append(" ".repeat(String.valueOf(m).length()));
for (int c = 0; c < n; c++) {
if (doCompress) {
sb.append(c % 10 + " ");
} else {
sb.append(String.format(columnFormatString, String.valueOf(c)));
}
}
for (int r = 0; r < m; r++) {
sb.append("\n");
sb.append(String.format(rowFormatString, String.valueOf(r)));
for (int c = 0; c < n; c++) {
sb.append(SYMBOLS.get(cells[r][c]));
if (doCompress) {
sb.append(" ");
} else
sb.append(" ".repeat(String.valueOf(n).length()));
}
}
return sb.toString();
}
private String buildFormattingString(int val) {
return "%-" + (String.valueOf(val).length() + 1) + "s";
}
}