-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
94 lines (84 loc) · 2.26 KB
/
Copy pathgame.js
File metadata and controls
94 lines (84 loc) · 2.26 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
const EventEmitter = require('events');
const winConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
class Game extends EventEmitter {
constructor(player) {
super();
this.board = Array(9)
.fill()
.map((_, i) => `(${i + 1})`);
player.on('move', this.playerMove);
}
init = () => {
console.log('let us play');
this.emit('render', this.board);
};
playerMove = (move) => {
if (['#', '!'].includes(this.board[move - 1])) {
console.log('space already used :(');
this.emit('render', this.board);
} else if (!this.board[move - 1]) {
console.log('not a valid move');
this.emit('render', this.board);
} else {
this.board[move - 1] = '!';
if (
winConditions.some((condition) =>
condition.every((position) => this.board[position] === '!')
)
) {
this.emit('render', this.board);
this.emit('resolve', 'player');
} else {
const threatConditions = winConditions.filter((condition) => {
let counts = {
'!': 0,
'#': 0,
};
for (let position of condition) {
counts[this.board[position]]++;
}
return counts['!'] === 2 && counts['#'] === 0;
});
const freeIndexes = (threatConditions.length
? threatConditions[
Math.floor(Math.random() * threatConditions.length)
]
: Array(9)
.fill()
.map((_, i) => i)
).reduce(
(freeIndexes, position) =>
['#', '!'].includes(this.board[position])
? freeIndexes
: [...freeIndexes, position],
[]
);
const aiPlayIndex =
freeIndexes[Math.floor(Math.random() * freeIndexes.length)];
this.board[aiPlayIndex] = '#';
if (
winConditions.some((condition) =>
condition.every((position) => this.board[position] === '#')
)
) {
this.emit('render', this.board);
this.emit('resolve', 'ai');
} else {
this.emit('render', this.board);
}
}
}
};
}
module.exports = (player) => {
return new Game(player);
};