-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.html
144 lines (131 loc) · 5.01 KB
/
TicTacToe.html
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
<!DOCTYPE html>
<html>
<head>
<title>TicTacToe</title>
<link rel="stylesheet" href="index.css" />
</head>
<style>
#game-box {
display: none;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
grid-gap: 10px;
}
.square {
background-color: blue;
color: white;
font-size: xx-large;
text-align: center;
}
</style>
<body>
<a href="index.html">back</a>
<h1>Tic Tac Toe</h1>
<button id="new-game">New Game</button>
<div id="game-box">
<div id="top-left" class="square"></div>
<div id="top-centre" class="square"></div>
<div id="top-right" class="square"></div>
<div id="mid-left" class="square"></div>
<div id="mid-centre" class="square"></div>
<div id="mid-right" class="square"></div>
<div id="bottom-left" class="square"></div>
<div id="bottom-centre" class="square"></div>
<div id="bottom-right" class="square"></div>
</div>
</body>
<script>
// global variables
// the counter keeps track of the number of squares clicked
let COUNTER = 0;
// squares
const topLeft = document.getElementById('top-left');
const topCentre = document.getElementById('top-centre');
const topRight = document.getElementById('top-right');
const midLeft = document.getElementById('mid-left');
const midCentre = document.getElementById('mid-centre');
const midRight = document.getElementById('mid-right');
const bottomLeft = document.getElementById('bottom-left');
const bottomCentre = document.getElementById('bottom-centre');
const bottomRight = document.getElementById('bottom-right');
//----------------------------------------------------------------
// functions
gameOver = function () {
if (
topLeft.innerHTML != '' &&
topLeft.innerHTML == topCentre.innerHTML &&
topCentre.innerHTML == topRight.innerHTML
)
return true;
else if (
midLeft.innerHTML != '' &&
midLeft.innerHTML == midCentre.innerHTML &&
midCentre.innerHTML == midRight.innerHTML
)
return true;
else if (
bottomLeft.innerHTML != '' &&
bottomLeft.innerHTML == bottomCentre.innerHTML &&
bottomCentre.innerHTML == bottomRight.innerHTML
)
return true;
else if (
topLeft.innerHTML != '' &&
topLeft.innerHTML == midLeft.innerHTML &&
midLeft.innerHTML == bottomLeft.innerHTML
)
return true;
else if (
topCentre.innerHTML != '' &&
topCentre.innerHTML == midCentre.innerHTML &&
midCentre.innerHTML == bottomCentre.innerHTML
)
return true;
else if (
topRight.innerHTML != '' &&
topRight.innerHTML == midRight.innerHTML &&
midRight.innerHTML == bottomRight.innerHTML
)
return true;
else if (
topLeft.innerHTML != '' &&
topLeft.innerHTML == midCentre.innerHTML &&
midCentre.innerHTML == bottomRight.innerHTML
)
return true;
else if (
topRight.innerHTML != '' &&
topRight.innerHTML == midCentre.innerHTML &&
midCentre.innerHTML == bottomLeft.innerHTML
)
return true;
else return false;
};
newGame = function () {
document.getElementById('game-box').style = 'display: grid;';
console.log('new game');
COUNTER = 0;
for (let square of document.getElementsByClassName('square'))
square.innerHTML = '';
};
//----------------------------------------------------------------
// Event Listeners
// when a square is clicked
for (let square of document.getElementsByClassName('square'))
square.addEventListener('click', function () {
// if the square has not been clicked
if (this.innerHTML == '') {
console.log(this.id);
this.innerHTML = COUNTER % 2 == 0 ? 'X' : 'O';
COUNTER++;
if (gameOver() == true) {
console.log('game over!');
window.alert(this.innerHTML + ' wins!!!');
newGame();
}
}
});
// when the new game button is clicked
document.getElementById('new-game').addEventListener('click', newGame);
</script>
</html>