-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.js
187 lines (128 loc) · 5.77 KB
/
tic-tac-toe.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
"use strict";
window.onload = function(){
var board = document.getElementById("board")
var box = board.querySelectorAll('div')
var win = document.getElementById("status")
var winner = ""
let counter = 0;
let game_state = ["", "", "", "", "", "", "", "", ""];
box.forEach(function(item)
{
//Set each div's class to square
item.setAttribute("class", "square");
//sets inner html of each div to ""
item.innerHTML="";
//Add click event listener to each square where X & O can be placed.
item.addEventListener("click", function(){
//if counter is even it is player 1's turn and if the box is still empty ""
if(counter % 2 == 0 && this.innerHTML == "" )
{
//Place an X inside item
item.innerHTML = "X";
//set class of item to square X so it looks properly
item.setAttribute("class", "square X");
//Increment counter so that it becomes the other players turn
counter++;
game_state[Array.from(box).indexOf(item)] = "X";
}
else if(counter % 2 != 0 && this.innerHTML == "")
{
//Place an O inside item
item.innerHTML = "O";
//set class of item to square O so the css formats it properly
item.setAttribute("class", "square O");
counter++;
game_state[Array.from(box).indexOf(item)] = "O";
}
});
//Mouse event listener that changes div class to hover when mouse is over div
item.addEventListener("mouseover", function(){
item.setAttribute("class", "hover");
});
//MOuse event listener that returns div to it's previous class
item.addEventListener("mouseout", function(){
if(item.innerHTML == "X"){
item.setAttribute("class", "square X")
}
else if(item.innerHTML == "O"){
item.setAttribute("class", "square O")
}
else{
item.setAttribute("class", "square")
}
});
let row_win = false;
let col_win = false;
let diagonal_win = false;
board.onclick = function(){
if(counter>4){
//For loop to check each row
for(var row_check = 0; row_check <= 6; row_check+=3){
//checks if rows are equal and in correct order
if(game_state[row_check] == game_state[row_check+1] &&
game_state[row_check+1] == game_state[row_check+1] &&
game_state[row_check+1] == game_state[row_check+2])
{
//checks that no div has an innerHtml of ""
if(game_state[row_check]!="" || game_state[row_check+1]!="" || game_state[row_check+1]!=""){
winner = game_state[row_check];
row_win = true;
}
}
}
//For loop to check each column
for(var col_check = 0; col_check<=3; col_check++)
{
if(game_state[col_check] == game_state[col_check+3] &&
game_state[col_check] == game_state[col_check+6] &&
game_state[col_check+3] == game_state[col_check+6])
{
if(game_state[col_check]!="" || game_state[col_check+1]!="" || game_state[col_check+1]!=""){
winner = game_state[col_check];
col_win = true;
}
}
}
//check if player woon from bottom left to top right
if(game_state[2] == game_state[4] &&
game_state[2]== game_state[6] &&
game_state[4] == game_state[6])
{
winner = game_state[2];
diagonal_win = true;
}
//check if player won from top left to bottom right
if(game_state[0] == game_state[4] &&
game_state[0]== game_state[8] &&
game_state[4] == game_state[8])
{
winner = game_state[0];
diagonal_win = true;
}
}
//Checks if draw conditions have been met by ensuring
//all indexes of game_state is filled & no found winning combination
if(!row_win && !col_win && diagonal_win && !game_state.includes(""))
{
win.innerHTML = "Draw"
win.setAttribute("class", "you-won")
}
//Checks if there is a winning condition.
if(row_win || col_win || diagonal_win)
{
//change div with class status inner html
win.innerHTML = "Congratulations! "+ winner +" is the Winner!";
//set attribute of win
win.setAttribute("class", "you-won");
//anti cheat no longer able to click more boxes after a winner
//is declared
board.style.pointerEvents = "none";
}
}
//Get new game button through document
var newGameButton = document.getElementById("game").getElementsByClassName("btn");
newGameButton[0].addEventListener("clicl", function(){
window.location.reload(true);
});
});
}