-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.html
More file actions
210 lines (176 loc) · 4.98 KB
/
Copy pathTicTacToe.html
File metadata and controls
210 lines (176 loc) · 4.98 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<!DOCTYPE html>
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="TicTacToe.css">
<script>
window.onload = function() {
displayPlayerTurn();
}
var playerSymbol = ['X', 'O'][Math.floor(Math.random()*2)];
const player1Symbol = "X";
const player2Symbol = "O";
var gameOver = false;
//get player symbol
function getSymbol(buttonId) {
document.getElementById(buttonId).innerHTML = playerSymbol;
}
//switch player turn
function switchPlayer() {
playerSymbol = (playerSymbol === 'X') ? 'O' : 'X';
displayPlayerTurn();
}
//place player symbol on the board
function placeSymbol(buttonId) {
var button = document.getElementById(buttonId);
if (!button.textContent) { // check if button is empty
getSymbol(buttonId);
if (checkWin(playerSymbol)){
playerTurn.innerHTML = "Player " + playerSymbol + " wins!";
lockBoard();
showResetButton();
} else if (checkTie()){
playerTurn.innerHTML = "Its a tie!";
lockBoard();
showResetButton();
} else {
switchPlayer();
}
}
}
function displayPlayerTurn(){
const playerTurn = document.getElementById("playerTurn");
if (playerSymbol === player1Symbol){
playerTurn.innerHTML = "Player X's Turn";
}
else {
playerTurn.innerHTML = "Player O's Turn";
}
}
function checkWin(symbol){
const board = [
document.getElementById('1').textContent,
document.getElementById('2').textContent,
document.getElementById('3').textContent,
document.getElementById('4').textContent,
document.getElementById('5').textContent,
document.getElementById('6').textContent,
document.getElementById('7').textContent,
document.getElementById('8').textContent,
document.getElementById('9').textContent
];
// Check rows
if ((board[0] === symbol && board[1] === symbol && board[2] === symbol) ||
(board[3] === symbol && board[4] === symbol && board[5] === symbol) ||
(board[6] === symbol && board[7] === symbol && board[8] === symbol)) {
return true;
}
// Check columns
if ((board[0] === symbol && board[3] === symbol && board[6] === symbol) ||
(board[1] === symbol && board[4] === symbol && board[7] === symbol) ||
(board[2] === symbol && board[5] === symbol && board[8] === symbol)) {
return true;
}
// Check diagonals
if ((board[0] === symbol && board[4] === symbol && board[8] === symbol) ||
(board[2] === symbol && board[4] === symbol && board[6] === symbol)) {
return true;
}
return false;
}
function checkTie(){
const board = [
document.getElementById('1').textContent,
document.getElementById('2').textContent,
document.getElementById('3').textContent,
document.getElementById('4').textContent,
document.getElementById('5').textContent,
document.getElementById('6').textContent,
document.getElementById('7').textContent,
document.getElementById('8').textContent,
document.getElementById('9').textContent
];
for (var i = 0; i < board.length; i++){
if (board[i] === ''){
return false;
}
}
return true;
}
//lock the board so no more moves can be made
function lockBoard(){
var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++){
buttons[i].disabled = true;
}
gameOver = true;
}
function showResetButton(){
const resetButton = document.createElement('button');
resetButton.id = 'resetButton';
resetButton.innerHTML = 'Reset Game';
resetButton.onclick = resetGame;
document.getElementById('resetButtonContainer').appendChild(resetButton);
}
function resetGame(){
const buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++) {
buttons[i].textContent = '';
buttons[i].disabled = false;
}
const resetButton = document.getElementById('resetButton');
resetButton.remove();
playerSymbol = ['X', 'O'][Math.floor(Math.random()*2)];
displayPlayerTurn();
}
</script>
</head>
<body>
<h2>Tic Tac Toe: Let's Go!</h2>
<div class="playerClass">
<p id="playerTurn" class="center"></p>
</div>
<div class="container">
<table id="tic-tac-toe" class="center">
<tbody>
<tr>
<td>
<button id="1" onclick="placeSymbol('1')"></button>
</td>
<td>
<button id="2" onclick="placeSymbol('2')"></button>
</td>
<td>
<button id="3" onclick="placeSymbol('3')"></button>
</td>
</tr>
<tr>
<td>
<button id="4" onclick="placeSymbol('4')"></button>
</td>
<td>
<button id="5" onclick="placeSymbol('5')"></button>
</td>
<td>
<button id="6" onclick="placeSymbol('6')"></button>
</td>
</tr>
<tr>
<td>
<button id="7" onclick="placeSymbol('7')"></button>
</td>
<td>
<button id="8" onclick="placeSymbol('8')"></button>
</td>
<td>
<button id="9" onclick="placeSymbol('9')"></button>
</td>
</tr>
</tbody>
</table>
</div>
<div class = "resetClass">
<div id="resetButtonContainer" class="center"></div>
</div>
</body>
</html>