-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnk.js
228 lines (198 loc) · 6.62 KB
/
mnk.js
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
class MNKAction {
constructor(playerSign, m, n) {
this.playerSign = playerSign;
this.m = m;
this.n = n;
}
toString() {
return `(${this.m}, ${this.n})`;
}
equals(other) {
return this.constructor === other.constructor && this.playerSign === other.playerSign && this.m === other.m && this.n === other.n;
}
hash() {
return JSON.stringify({ playerSign: this.playerSign, m: this.m, n: this.n });
}
}
class MNK {
constructor(m, n, k, playerSigns, emptySign = "-", aiPlayerSign) {
if (k > Math.max(m, n)) {
throw new Error("k has to be smaller or equal to max (m, n)");
}
if (playerSigns.includes(emptySign)) {
throw new Error("Use different signs for players and default empty cell");
}
this.m = m;
this.n = n;
this.k = k;
this.aiPlayerSign = aiPlayerSign;
this.playerSigns = [...playerSigns];
this.playerSignsRotation = [...playerSigns];
this.emptySign = emptySign;
this.lastAction = null;
this._isTerminal = false;
this.remainingMoves = m * n;
this.utility = Array(this.playerSigns.length).fill(0);
this.winningSequence = [];
this.bitboards = new Map();
playerSigns.forEach(sign => this.bitboards.set(sign, 0n));
this.winningMasks = this.generateWinningMasks();
this._cacheKey = null;
this._bitmask = 0n;
}
get board() {
const board = [];
for (let i = 0; i < this.m; i++) {
const row = [];
for (let j = 0; j < this.n; j++) {
const pos = i * this.n + j;
let sign = this.emptySign;
for (const [player, bitboard] of this.bitboards) {
if ((bitboard & (1n << BigInt(pos))) !== 0n) {
sign = player;
break;
}
}
row.push(sign);
}
board.push(row);
}
return board;
}
generateWinningMasks() {
const masks = [];
const addMasks = (startI, startJ, di, dj) => {
let mask = 0n;
const positions = [];
for (let d = 0; d < this.k; d++) {
const i = startI + di * d;
const j = startJ + dj * d;
const pos = i * this.n + j;
mask |= 1n << BigInt(pos);
positions.push(`${i},${j}`);
}
masks.push({ mask, positions });
};
// Horizontal
for (let i = 0; i < this.m; i++) {
for (let j = 0; j <= this.n - this.k; j++) {
addMasks(i, j, 0, 1);
}
}
// Vertical
for (let j = 0; j < this.n; j++) {
for (let i = 0; i <= this.m - this.k; i++) {
addMasks(i, j, 1, 0);
}
}
// Diagonal down-right
for (let i = 0; i <= this.m - this.k; i++) {
for (let j = 0; j <= this.n - this.k; j++) {
addMasks(i, j, 1, 1);
}
}
// Diagonal down-left
for (let i = 0; i <= this.m - this.k; i++) {
for (let j = this.k - 1; j < this.n; j++) {
addMasks(i, j, 1, -1);
}
}
return masks;
}
getCurrentPlayerSign() {
return this.playerSignsRotation[0];
}
getActions() {
const curPlayer = this.getCurrentPlayerSign();
const combined = Array.from(this.bitboards.values()).reduce((acc, bb) => acc | bb, 0n);
const actions = [];
for (let i = 0; i < this.m; i++) {
for (let j = 0; j < this.n; j++) {
const pos = i * this.n + j;
if ((combined & (1n << BigInt(pos))) === 0n) {
actions.push(new MNKAction(curPlayer, i, j));
}
}
}
return actions;
}
isLegalAction(action) {
if (!action) return false;
const pos = action.m * this.n + action.n;
const combined = Array.from(this.bitboards.values()).reduce((acc, bb) => acc | bb, 0n);
return action.m >= 0 && action.m < this.m &&
action.n >= 0 && action.n < this.n &&
action.playerSign === this.getCurrentPlayerSign() &&
(combined & (1n << BigInt(pos))) === 0n;
}
takeAction(action, preserveState = true) {
this._cacheKey = null;
if (this.isTerminal()) {
throw new Error("Cannot take actions in a terminal state.");
}
if (!this.isLegalAction(action)) {
throw new Error("Illegal Action.");
}
const stateCopy = preserveState ? new MNK(this.m, this.n, this.k, this.playerSigns, this.emptySign, this.aiPlayerSign) : this;
stateCopy.playerSignsRotation = [...this.playerSignsRotation];
stateCopy.remainingMoves = this.remainingMoves;
stateCopy.lastAction = this.lastAction;
stateCopy._isTerminal = this._isTerminal;
stateCopy.utility = [...this.utility];
stateCopy.winningSequence = [...this.winningSequence];
stateCopy.bitboards = new Map(this.bitboards);
const pos = action.m * this.n + action.n;
stateCopy.bitboards.set(action.playerSign, stateCopy.bitboards.get(action.playerSign) | (1n << BigInt(pos)));
stateCopy.playerSignsRotation.shift();
stateCopy.playerSignsRotation.push(action.playerSign);
stateCopy.remainingMoves--;
stateCopy.lastAction = action;
stateCopy.checkWinner();
return stateCopy;
}
checkWinner() {
if (this._isTerminal) return true;
if (!this.lastAction) return false;
const playerSign = this.lastAction.playerSign;
const bitboard = this.bitboards.get(playerSign);
for (const { mask, positions } of this.winningMasks) {
if ((bitboard & mask) === mask) {
this.utility = this.playerSigns.map(s => s === playerSign ? 1 : -1);
this._isTerminal = true;
this.winningSequence = positions;
return true;
}
}
if (this.remainingMoves === 0) {
this.utility.fill(0);
this._isTerminal = true;
return true;
}
return false;
}
isTerminal() {
return this._isTerminal;
}
getUtility() {
return this.utility;
}
toString() {
return this.board.map(row => row.join(' ')).join('\n');
}
getCacheKey() {
if (!this._cacheKey) {
this._bitmask = Array.from(this.bitboards.values())
.reduce((acc, bb) => acc | bb, 0n);
this._cacheKey = `${this.remainingMoves}|` +
`${this.playerSignsRotation.join(',')}|` +
this._bitmask.toString(36);
}
return this._cacheKey;
}
equals(other) {
return this.getCacheKey() === other.getCacheKey();
}
isMaximizerTurn() {
return this.getCurrentPlayerSign() === this.aiPlayerSign;
}
}