forked from exercism/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
68 lines (65 loc) · 1.68 KB
/
Copy pathexample.js
File metadata and controls
68 lines (65 loc) · 1.68 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
/**
* "Player O" plays from top to bottom, "Player X" plays from left to right.
* @param board
*/
export class Board {
constructor(board) {
this.board = board.map(b => [...b]);
}
winner() {
const players = ['X', 'O'];
for (const player of players) {
if (this.checkWin(player)) {
return player;
}
}
return '';
}
checkWin(player) {
const positions = this.startPositions(player);
for (const position of positions) {
if (this.search(position, player, [])) {
return true;
}
}
return false;
}
search(pos, XorO, checked) {
if (!this.matches(pos, XorO)) {
return false;
}
if (this.winningSpot(pos, XorO)) {
return true;
}
checked = checked.slice(0);
checked.push(pos);
const matches = this.neighbors(pos).filter(({ x, y }) => this.matches({ x, y }, XorO) && checked.filter(spot => spot.x === x && spot.y === y).length === 0);
if (matches.length === 0) {
return false;
}
return matches.filter(spot => this.search(spot, XorO, checked)).length > 0;
}
neighbors({ x, y }) {
return [
{ x, y: y + 2 },
{ x, y: y - 2 },
{ x: x + 1, y: y + 1 },
{ x: x - 1, y: y + 1 },
{ x: x + 1, y: y - 1 },
{ x: x - 1, y: y - 1 },
];
}
startPositions(XorO) {
return XorO === 'X' ?
this.board.map((pos, i) => ({ x: i, y: i })) :
this.board[0].map((pos, i) => ({ x: 0, y: i }));
}
winningSpot({ x, y }, XorO) {
return XorO === 'X' ?
y === this.board[0].length - 1 + x :
x === this.board.length - 1;
}
matches({ x, y }, XorO) {
return this.board[x] !== undefined && this.board[x][y] === XorO;
}
}