-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.js
70 lines (62 loc) · 1.68 KB
/
TicTacToe.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
class TicTacToe {
constructor(){
this.grid = ['','','','','','','','',''],
this.turn = 'X',
this.turnCount = 0,
// 1 is 'x' 2 is 'o'
this.state = 'blank',
this.winner = ''
}
update(location){
if (this.grid[location] === ''){
this.grid[location] = this.turn;
this.turnCount += 1;
} else {
throw "Cell Already Filled !";
}
}
changeTurn(){
if (this.turn === 'X'){
this.turn = 'O';
} else if (this.turn === 'O') {
this.turn = 'X';
}
}
checkWin(){
const winstates = [[0,1,2],[3,4,5],[6,7,8],[0,4,8],[1,2,7],[2,4,6],[0,3,6],[1,4,7],[2,5,8]];
let roundwin = false;
for (let i=0;i < winstates.length ; i++){
const condition = winstates[i];
//console.log(condition);
let a = this.grid[condition[0]];
let b = this.grid[condition[1]];
let c = this.grid[condition[2]];
console.log(condition);
if (a === '' || b === '' || c === ''){
continue;
}
if (a === b && b === c){
this.winner = a;
roundwin = true;
break;
}
}
if (roundwin){
return this.winner;
}
if (this.isGridFull()){
this.winner = 'Draw';
return this.winner;
}
}
isGridFull(){
return !(this.grid.includes(''));
}
isCellFull(location){
if (this.grid[location]){
return true;
} else {
return false;
}
}
}