-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
90 lines (70 loc) · 2.2 KB
/
game.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
import { symbolsToArray } from "./symbols.js";
const createBoard = () => {
const board = [];
for (let y = 0; y < 3; y++) {
const row = [];
for (let x = 0; x < 3; x++) row.push('');
board.push(row);
}
return board;
};
const findBoardY = (flatIndex) => Math.floor(flatIndex);
const findBoardX = (flatIndex) => Math.round((flatIndex - parseInt(`${flatIndex}`)) * 3);
const updateBoard = (board, symbol, flatIndex) => {
const mappedIndex = flatIndex / 3;
const y = findBoardY(mappedIndex);
const x = findBoardX(mappedIndex);
board[y][x] = symbol;
return board;
}
const isSymbolWinner = (board, symbol) =>{
let positions;
const fillArray = (value) => new Array(3).fill(value);
const xWin = board.some(y => {
const result = y.every(playedSymbol => playedSymbol === symbol);
if (result) {
const yPos = board.indexOf(y);
positions = [fillArray(yPos), [0,1,2]];
}
return result;
});
if (xWin)
return positions;
const checkYwin = (result, xs) => {
for (let i = 0; i < 3; i++)
result[i] = result[i] && xs[i] === symbol;
return result;
}
const yArr = board.reduce(checkYwin, fillArray(true));
const yWin = yArr.some(win => win);
if (yWin) {
const xPos = yArr.indexOf(true);
positions = [[0,1,2], fillArray(xPos)];
return positions;
}
const checkYxWin = (yxTuples) => yxTuples.every(([y, x]) => board[y][x] === symbol);
const parseYxPositions = (yxTuples) => yxTuples.reduce(([yPos, xPos] , [y, x]) => [[...yPos, y], [...xPos, x]],[[], []]);
const yxCombos1 = [[0, 0], [1, 1], [2, 2]];
const yxCombos2 = [[0, 2], [1, 1], [2, 0]];
if (checkYxWin(yxCombos1))
positions = parseYxPositions(yxCombos1);
if (checkYxWin(yxCombos2))
positions = parseYxPositions(yxCombos2);
return positions;
}
const checkBoardState = (board) => {
let positions, symbol;
for (symbol of symbolsToArray) {
positions = isSymbolWinner(board, symbol);
if (positions)
break;
}
const hasWinner = !!positions;
const isFinished = hasWinner || board.every(y => y.every(x => symbolsToArray.includes(x)))
return {isFinished, hasWinner, positions, symbol};
}
export const GAME = {
createBoard,
updateBoard,
checkBoardState,
};