Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
87 lines (80 loc) · 1.76 KB
/
Copy pathgame.js
File metadata and controls
87 lines (80 loc) · 1.76 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
// game.js, a js file for all the game logic
console.log('game working');
var Cell = function(row, col) {
this.index = row + "-" + col;
this.value = " ";
};
/*var firstCell = new Cell(1,1);
console.log(firstCell);*/
var game = {
turn: "x",
over: false,
winner: null,
board: [],
buildBoard: function(){
for(var i=0; i<3; i++) {
this.board.push([]);
for(var j=0; j<3; j++){
var c = new Cell(i,j);
this.board[i].push(c);
}
}
},
count: 0,
changeTurn: function(){
if(this.count % 2 == 0){
this.turn = "o"; //for x
} else {
this.turn = "x"; //for o
}
console.log(this.count);
this.count++;
},
checkWin: function(){
var winningCombinations = [['0-0','0-1','0-2'], ['0-0','1-1','2-2'], ['0-0','1-0','2-0'], ['0-1','1-1','2-1'], ['0-2','1-2','2-2'], ['0-2','1-1','2-0'], ['1-0','1-1','1-2'], ['2-0','2-1','2-2']];
for(var i=0; i<8; i++){
var xs = 0;
var os = 0;
for(var j=0; j<3; j++){
var row = winningCombinations[i][j].charAt(0);
var col = winningCombinations[i][j].charAt(2);
var cel = this.board[row][col];
if(cel.value == "x")
xs++;
else if(cel.value == "o")
os++;
if(xs == 3){
this.over = true;
this.winner = "x";
break;
}
if(os == 3){
this.over = true;
this.winner = "o";
break;
}
}
}
},
updateCell: function(row, col){
if(this.board[row][col].value == " ")
this.board[row][col].value = this.turn;
},
reset: function(){
this.board = [];
this.buildBoard();
this.turn = "x";
this.count = 1;
this.winner = null;
this.over = false;
$("#0-0").text("");
$("#0-1").text("");
$("#0-2").text("");
$("#1-0").text("");
$("#1-1").text("");
$("#1-2").text("");
$("#2-0").text("");
$("#2-1").text("");
$("#2-2").text("");
}
}