-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameutils.js
65 lines (54 loc) · 1.32 KB
/
gameutils.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
const GridType = {
EMPTY: 0,
O: 1,
X: 2,
};
const GameState = {
WIN: 0,
LOSE: 1,
DRAW: 2,
ONGOING: 3,
};
class GameUtils{
constructor(){
};
static IsWinningMove(board, player){
let col=0, row=0, diag=0, rdiag=0;
for(let i=0; i<3; i++){
for(let j=0; j<3;j++){
if(board[i][j] == player) row ++;
if(board[j][i] == player) col ++;
}
if(board[i][i] == player) diag ++;
if(board[i][2-i] == player) rdiag ++;
if(row != 3) row = 0;
if(col != 3) col = 0;
}
return (col == 3) || (row == 3) || (diag == 3) || (rdiag == 3);
}
static AllValidMoves(board){
let moves = [];
if(board){
for(let i=0; i<3; i++){
for(let j=0; j<3;j++){
if(board[i][j] == GridType.EMPTY) moves.push({row:j, col:i});
}
}
}
return moves;
}
static RenderGrid(gridType){
switch(gridType){
case GridType.EMPTY:
return '';
break;
case GridType.O:
return 'O';
break;
case GridType.X:
return 'X';
break;
}
return;
}
}