-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.js
173 lines (168 loc) · 6.15 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
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
var start;
var firstPlayerIs;
var cycle = 0;
var win = 'null';
var clickCount = 0;
//when called, opens the board to interaction
function startGame() {
start = true;
document.getElementById('notify').innerHTML = 'Game started!';
firstPlayerIs = document.getElementById('firstPlayer').value;
if (firstPlayerIs == 'X' || firstPlayerIs == 'x') {
cycle = 0;
} else if (firstPlayerIs == 'O' || firstPlayerIs == 'o') {
cycle = 1;
} else {
cycle = 0;
console.log('invalid input, set to default X');
}
}
//calls when a tile is clicked
function onClick(tileID) {
if (win == 'null' && start == true) {
if (cycle == 0
&& document.getElementById(tileID).innerHTML != 'X'
&& document.getElementById(tileID).innerHTML != 'O') {
document.getElementById(tileID).innerHTML = 'X';
cycle = 1;
clickCount = clickCount + 1;
winState();
} else if (cycle == 1
&& document.getElementById(tileID).innerHTML != 'X'
&& document.getElementById(tileID).innerHTML != 'O') {
document.getElementById(tileID).innerHTML = 'O';
cycle = 0;
clickCount = clickCount + 1;
winState();
}
}
}
//when called, checks tiles for a row of 3
function winState() {
//check top row for x
if (document.getElementById('tile1').innerHTML == 'X'
&& document.getElementById('tile2').innerHTML == 'X'
&& document.getElementById('tile3').innerHTML == 'X') {
win = 'X';
}
//check middle row for x
if (document.getElementById('tile4').innerHTML == 'X'
&& document.getElementById('tile5').innerHTML == 'X'
&& document.getElementById('tile6').innerHTML == 'X') {
win = 'X';
}
//check bottom row for x
if (document.getElementById('tile7').innerHTML == 'X'
&& document.getElementById('tile8').innerHTML == 'X'
&& document.getElementById('tile9').innerHTML == 'X') {
win = 'X';
}
//check left column for x
if (document.getElementById('tile1').innerHTML == 'X'
&& document.getElementById('tile4').innerHTML == 'X'
&& document.getElementById('tile7').innerHTML == 'X') {
win = 'X';
}
//check middle column for x
if (document.getElementById('tile2').innerHTML == 'X'
&& document.getElementById('tile5').innerHTML == 'X'
&& document.getElementById('tile8').innerHTML == 'X') {
win = 'X';
}
//check right column for x
if (document.getElementById('tile3').innerHTML == 'X'
&& document.getElementById('tile6').innerHTML == 'X'
&& document.getElementById('tile9').innerHTML == 'X') {
win = 'X';
}
//check left down diagonal for x
if (document.getElementById('tile1').innerHTML == 'X'
&& document.getElementById('tile5').innerHTML == 'X'
&& document.getElementById('tile9').innerHTML == 'X') {
win = 'X';
}
//check right down diagonal for x
if (document.getElementById('tile3').innerHTML == 'X'
&& document.getElementById('tile5').innerHTML == 'X'
&& document.getElementById('tile7').innerHTML == 'X') {
win = 'X';
}
//check top row for o
if (document.getElementById('tile1').innerHTML == 'O'
&& document.getElementById('tile2').innerHTML == 'O'
&& document.getElementById('tile3').innerHTML == 'O') {
win = 'O';
}
//check middle row for o
if (document.getElementById('tile4').innerHTML == 'O'
&& document.getElementById('tile5').innerHTML == 'O'
&& document.getElementById('tile6').innerHTML == 'O') {
win = 'O';
}
//check bottom row for o
if (document.getElementById('tile7').innerHTML == 'O'
&& document.getElementById('tile8').innerHTML == 'O'
&& document.getElementById('tile9').innerHTML == 'O') {
win = 'O';
}
//check left column for o
if (document.getElementById('tile1').innerHTML == 'O'
&& document.getElementById('tile4').innerHTML == 'O'
&& document.getElementById('tile7').innerHTML == 'O') {
win = 'O';
}
//check middle column for o
if (document.getElementById('tile2').innerHTML == 'O'
&& document.getElementById('tile5').innerHTML == 'O'
&& document.getElementById('tile8').innerHTML == 'O') {
win = 'O';
}
//check right column for o
if (document.getElementById('tile3').innerHTML == 'O'
&& document.getElementById('tile6').innerHTML == 'O'
&& document.getElementById('tile9').innerHTML == 'O') {
win = 'O';
}
//check left down diagonal for o
if (document.getElementById('tile1').innerHTML == 'O'
&& document.getElementById('tile5').innerHTML == 'O'
&& document.getElementById('tile9').innerHTML == 'O') {
win = 'O';
}
//check right down diagonal for o
if (document.getElementById('tile3').innerHTML == 'O'
&& document.getElementById('tile5').innerHTML == 'O'
&& document.getElementById('tile7').innerHTML == 'O') {
win = 'O';
}
if (win != 'null') {
document.getElementById('notify').innerHTML = 'Player ' + win + ' wins!';
start = false;
}
catsGame();
}
//checks for a full board with no win
function catsGame() {
if (win == 'null' && clickCount == 9) {
start = false;
document.getElementById('notify').innerHTML = 'It\'s a Cat\'s game!'
}
}
//when called, reset the game variables
function reset() {
start = false;
firstPlayerIs = '';
cycle = 0;
win = 'null';
clickCount = 0;
document.getElementById('tile1').innerHTML = ' ';
document.getElementById('tile2').innerHTML = ' ';
document.getElementById('tile3').innerHTML = ' ';
document.getElementById('tile4').innerHTML = ' ';
document.getElementById('tile5').innerHTML = ' ';
document.getElementById('tile6').innerHTML = ' ';
document.getElementById('tile7').innerHTML = ' ';
document.getElementById('tile8').innerHTML = ' ';
document.getElementById('tile9').innerHTML = ' ';
document.getElementById('notify').innerHTML = 'This will notify you of events in the game.';
}