-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
196 lines (171 loc) · 5.97 KB
/
script.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
const State = class {
constructor(board) {
board ? this.board = board : this.board = [
'E', 'E', 'E',
'E', 'E', 'E',
'E', 'E', 'E'
];
this.oMovesCount = 0;
this.emptyCells = () => {
let indexes = [];
for (let i = 0; i < this.board.length; i++) {
if (this.board[i] =='E') {
indexes.push(i);
}
}
return indexes;
}
}
// isTerminal checks for win conditions and draws
isTerminal() {
// Check rows
for (let i = 0; i <= 6; i += 3) {
if (this.board[i] !== 'E' && this.board[i + 1] == this.board[i] && this.board[i] == this.board[i + 2]) {
game.gameOver('win', this.board[i]);
return this.board[i];
}
}
// Check columns
for (let i = 0; i <= 2; i++) {
if (this.board[i] !== 'E' && this.board[i + 3] == this.board[i] && this.board[i] == this.board[i + 6]) {
game.gameOver('win', this.board[i]);
return true;
}
}
// Check diagonals
for (let i = 0, j = 2; i <= 2; i++, j += 2) {
if (this.board[i] !== 'E' && this.board[i] == this.board[i + 4] && this.board[i] == this.board[i + 8]) {
game.gameOver('win', this.board[i]);
return true;
}
if (this.board[j] !== 'E' && this.board[j] == this.board[j + 2] && this.board[j] == this.board[j + 4]) {
game.gameOver('win', this.board[j]);
return true;
}
}
// Check for a draw
this.board.includes('E') ? console.log('still running') : game.gameOver('draw');
}
}
let state = new State();
const Game = class {
constructor(player) {
this.player = player;
}
// Change div text to player value
turn(target) {
if (player == 'X') {
target.innerText = 'X';
} else if (player == 'O') {
target.innerText = 'O';
}
// update that state
state.board[target.id] == 'E' ? state.board[target.id] = player : null;
// check win conditions
state.isTerminal() ? console.log(state.board[target.id] + " has won the game") : null;
// switch players
player == 'X'? player = 'O' : player = 'X';
// if playing w/ AI, make a move
if (player == 'O' && AIplayer && !state.isTerminal()) {
AIplayer.makeAMove();
state.isTerminal();
}
}
// Cover board w/ game-over div
gameOver(type, winner) {
document.querySelector('#game-over').classList.add('game-over');
if (type == 'win') {
document.querySelector('#game-over-text').innerHTML = "" +
`<h1 style="text-align: center">The winner is... ${winner}</h1>` +
`<div class='reset-button'>Reset board</div>`
} else {
document.querySelector('#game-over-text').innerHTML = "" +
`<h1 style='text-align: center'>A strange game. <br> <br> The only winning move is not to play.</h1>` +
`<div class='reset-button'>Play again</div>`
}
document.querySelector('.reset-button').addEventListener('click', () => this.reset());
}
reset() {
player = 'X';
game = new Game('X');
state = new State();
boxes.forEach((item) => {
item.innerHTML = "";
item.removeEventListener('click', (event) => {
event.target.innerText == "" ? game.turn(event.target) : null;
});
});
document.querySelector('#game-over').classList.remove('game-over');
document.querySelector('#game-over-text').innerHTML = "";
}
}
let player = 'X';
let game = new Game(player);
const AI = class {
constructor() {
this.state = state;
this.score = 0;
}
calculateNextMoves() {
let nextState = [...state.board];
for (let i = 0; i < nextState.length; i++) {
let newState = new State(nextState);
console.log(newState.board);
// if (newState.board[i] == 'E') {
newState.board[i] = 'O';
console.log(newState.board)
if (i !== 0) {
newState.board[i - 1] = 'E';
}
console.log(newState)
// this.isGameOver(nextState);
// newState.isGameOver();
// newState.isTerminal();
// } else { console.log(`can\'t move at position ${i}`)}
}
}
makeAMove() {
let available = state.emptyCells();
console.log(available)
let randomCell = Math.floor(Math.random() * available.length);
console.log(available[randomCell]);
console.log(state)
if (state.board[randomCell] == 'E') {
game.turn(boxes[available[randomCell]]);
state.board[available[randomCell]] = 'O';
} else {
this.makeAMove();
}
// boxes[randomCell].id;
}
isGameOver(board) {
if (board.isTerminal() == 'X') {
return -10;
} else if (board.isTerminal() == 'O') {
return 10;
} else {
return 5;
}
}
}
let prompt = window.prompt('Would you like to play with an AI? (y/n)');
if (prompt.toLowerCase() == 'y') {
AIplayer = new AI(game);
}
// AIplayer.calculateNextMoves();
// List of colors to apply to squares
const colors = [
'red', 'green', 'blue',
'darkred', 'darkgreen', 'darkblue',
'mediumvioletred', 'lawngreen', 'blueviolet'
];
// Grab each square
let boxes = document.querySelectorAll('.square');
// Add event listeners to each square
boxes.forEach((item) => {
item.addEventListener('click', (event) => {
event.target.innerText == "" ? game.turn(event.target) : null;
});
});
// Apply color to each square
colors.forEach((color, index) => boxes[index].style.backgroundColor = color);