-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameModule.js
69 lines (54 loc) · 1.96 KB
/
GameModule.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
"use strict";
const winCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[6, 4, 2]
];
const start = () => {
let randomNumber = Math.round(Math.random() * 1);
if (randomNumber < 0.5) return 'X';
else return 'O';
};
const second = () => {
if (startPlayer === 'X') return 'O';
else return 'X';
};
const startPlayer = start();
const secondPlayer = second();
exports.Game = function Game() {
const gameObject = {
emptyGameField: ["","","","","","","","",""],
gameField: ["","","","","","","","",""],
currentPlayer: startPlayer,
secondPlayer: secondPlayer,
result: "",
move: (symbol,field) => {
if (gameObject.gameField[field] === "") {
gameObject.gameField[field] = symbol;
const arrayOfXIndices = gameObject.gameField.reduce((a,e,i) => (e === 'X') ? a.concat(i) : a, []); //finds all fields in board that have already symbols
const arrayOfOIndices = gameObject.gameField.reduce((a,e,i) => (e === 'O') ? a.concat(i) : a, []); //finds all fields in board that have already symbols
for (let [index,win] of winCombos.entries()) {
if (win.every(elem => arrayOfXIndices.indexOf(elem) > -1) || win.every(elem => arrayOfOIndices.indexOf(elem) > -1)) {
return `Game Over: Player ${symbol} has won!`;
};
};
const numberOfSymbolsOnGameField = gameObject.gameField.filter(x => x != "").length;
numberOfSymbolsOnGameField === 9 ? gameObject.result = 'done' : gameObject.result = symbol;
if (gameObject.result === 'done') {
for (let [index,win] of winCombos.entries()) {
if (win.every(elem => arrayOfXIndices.indexOf(elem) > -1) || win.every(elem => arrayOfOIndices.indexOf(elem) > -1)) {
return `Game Over: Player ${symbol} has won!`
}
else return "It's a draw."
};
};
return "";
};
}};
return gameObject;
};