-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt.js
73 lines (67 loc) · 3.14 KB
/
ttt.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
window.onload = function(){
// Collection of all squares on the board.
var squares = document.getElementsByTagName('td');
var moveCounter = 0;
function add_mark(){
if(moveCounter % 2 === 0 && this.innerHTML === "-"){
this.innerHTML = "X";
moveCounter++;
}
else if(moveCounter % 2 != 0 && this.innerHTML === "-"){
this.innerHTML = "O";
moveCounter++;
}
else{
console.log("error")
}
}
for(var i = 0; i < squares.length; i++) {
// When you click a square, runs the `add_mark` method.
squares[i].addEventListener("click", add_mark);
squares[i].addEventListener("click", isWinningGame);
squares[i].addEventListener("click", isCatsGame);
}
function isCatsGame(){
if( moveCounter === 9 && isWinningGame() === false){
alert("Cat's Game");
location.reload();
}
}
function isWinningGame(){
if( (squares[0].innerHTML != "-" && squares[1].innerHTML != "-" && squares[2].innerHTML != "-") && (squares[0].innerHTML === squares[1].innerHTML && squares[0].innerHTML === squares[2].innerHTML) ){
alert(squares[0].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[3].innerHTML != "-" && squares[4].innerHTML != "-" && squares[5].innerHTML != "-") && (squares[3].innerHTML === squares[4].innerHTML && squares[3].innerHTML === squares[5].innerHTML) ){
alert(squares[3].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[6].innerHTML != "-" && squares[7].innerHTML != "-" && squares[8].innerHTML != "-") && (squares[6].innerHTML === squares[7].innerHTML && squares[6].innerHTML === squares[8].innerHTML) ){
alert(squares[6].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[0].innerHTML != "-" && squares[3].innerHTML != "-" && squares[6].innerHTML != "-") && (squares[0].innerHTML === squares[3].innerHTML && squares[0].innerHTML === squares[6].innerHTML) ){
alert(squares[0].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[1].innerHTML != "-" && squares[4].innerHTML != "-" && squares[7].innerHTML != "-") && (squares[1].innerHTML === squares[4].innerHTML && squares[1].innerHTML === squares[7].innerHTML) ){
alert(squares[1].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[2].innerHTML != "-" && squares[5].innerHTML != "-" && squares[8].innerHTML != "-") && (squares[2].innerHTML === squares[5].innerHTML && squares[2].innerHTML === squares[8].innerHTML) ){
alert(squares[2].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[0].innerHTML != "-" && squares[4].innerHTML != "-" && squares[8].innerHTML != "-") && (squares[0].innerHTML === squares[4].innerHTML && squares[0].innerHTML === squares[8].innerHTML) ){
alert(squares[0].innerHTML + " is the winner!");
location.reload();
}
else if( (squares[2].innerHTML != "-" && squares[4].innerHTML != "-" && squares[6].innerHTML != "-") && (squares[2].innerHTML === squares[4].innerHTML && squares[2].innerHTML === squares[6].innerHTML) ){
alert(squares[2].innerHTML + " is the winner!");
location.reload();
}
else{
return false;
}
}
}