-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictac.html
309 lines (285 loc) · 8.52 KB
/
tictac.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE HTML>
<html>
<head>
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
#outer {
min-height: 100%;
position: relative;
}
#header {
padding: 10px;
text-align: center;
font-size: 48pt;
font-weight: bold;
}
#content {
padding: 10px 10px 80px;
}
#footer {
width: 100%;
position: absolute;
bottom: 0;
height: 80px;
left: 0;
text-align: center;
}
div.message {
text-align: center;
font-size: 36pt;
padding: 36px;
}
div.restart {
text-align: center;
}
p.choices {
text-align: center;
}
table.tttable {
border-collapse: collapse;
border: 0;
}
table.tttable td.row2 {
border-bottom: 2px solid black;
border-top: 2px solid black;
}
table.tttable td.col2 {
border-left: 2px solid black;
border-right: 2px solid black;
}
button.ttbutton {
background-color: transparent;
border: 0;
color: black;
width: 72px;
height: 72px;
font-family: sans-serif;
font-size: 48pt;
font-weight: bold;
}
button.button {
width: 108px;
height: 36px;
font-size: 16px;
font-weight: bold;
}
</style>
<script>
let playerNumber = 0;
let computerNumber = 0;
let playerLetter = '';
let computerLetter = '';
let winner = 0;
let board = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
function setPlayerNumber(pn) {
if (pn === 1) {
playerNumber = 1;
computerNumber = 2;
switchVis("NewGame", "ExOh")
}
if (pn === 2) {
playerNumber = 2;
computerNumber = 1;
switchVis('NewGame', 'ExOh');
}
}
function setPlayerLetter(pl) {
if (pl === "X") {
playerLetter = 'X';
computerLetter = 'O';
startGame();
}
if (pl === "O") {
playerLetter = 'O';
computerLetter = 'X';
startGame();
}
}
function restart() {
playerNumber = 0;
computerNumber = 0;
playerLetter = '';
computerLetter = '';
winner = 0;
board = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
setboard(board);
let rd = document.getElementById('Restart');
rd.style.display = 'none';
switchVis('TicTac', 'NewGame');
}
function setboard(board) {
for (let i = 0; i < 9; i++) {
let bt = document.getElementById('tictac' + i);
bt.innerHTML = '';
bt.disabled = true;
}
}
function startGame() {
switchVis('ExOh', 'TicTac');
if (playerNumber === 1) {
playersMove()
} else {
computersMove()
}
}
function setMessage(message) {
let md = document.getElementById('message');
md.innerHTML = message;
}
function playersMove() {
for (let i = 0; i < 9; i++) {
let bt = document.getElementById('tictac' + i);
bt.disabled = board[i] !== 0;
}
setMessage("Your Move...")
}
function computersMove() {
for (let i = 0; i < 9; i++) {
let bt = document.getElementById('tictac' + i);
bt.disabled = true;
}
setMessage("Computer is thinking...");
callBackend();
}
function processResponse(message) {
let payload = JSON.parse(message);
if (payload.move !== undefined) {
let move = payload.move;
board[move] = payload.player;
let bt = document.getElementById('tictac' + move);
bt.innerHTML = computerLetter;
}
if (payload.winner !== undefined) {
if (payload.winner === 3) {
setMessage("A Draw.");
}
if (payload.winner === computerNumber) {
setMessage("Computer Wins.");
}
if (payload.winner === playerNumber) {
setMessage("Human Wins.");
}
let rd = document.getElementById('Restart');
rd.style.display = 'block';
} else {
playersMove();
}
}
function ttbutton(bn) {
if (board[bn] !== 0) {
alert("that space is already taken; something bad has happened.");
return
}
board[bn] = playerNumber;
let bt = document.getElementById('tictac' + bn);
bt.innerHTML = playerLetter;
computersMove();
}
function switchVis(hideDiv, showDiv) {
let hd = document.getElementById(hideDiv);
let sd = document.getElementById(showDiv);
hd.style.display = 'none';
sd.style.display = 'block';
}
function callBackend() {
let xmlHttp = new XMLHttpRequest();
if (xmlHttp == null) {
alert("get a better browser!");
return;
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
processResponse(xmlHttp.responseText);
}
};
let s = '';
for (let i = 0; i < 9; i++) {
s = s + board[i];
}
let payload = 'computer=' + computerNumber + '&board=' + s;
xmlHttp.open("GET", "/cgi-bin/tictacservice?" + payload, true);
xmlHttp.send();
}
</script>
<title>Tic Tac Toe</title>
</head>
<body>
<div id="outer">
<div id="header">Tic Tac Toe</div>
<div id="content">
<div id="NewGame" style="display:block">
<p class="choices">Let's play a game of Tic Tac Toe.</p>
<p class="choices">Do you want to play first or second?</p>
<p class="choices">
<button type="button" class="button" onclick="setPlayerNumber(1)">First</button>
<button type="button" class="button" onclick="setPlayerNumber(2)">Second</button>
</p>
</div>
<div id="ExOh" style="display:none">
<p class="choices">Do you want to be X or O?</p>
<p class="choices">
<button type="button" class="button" onclick="setPlayerLetter('X')">X</button>
<button type="button" class="button" onclick="setPlayerLetter('O')">O</button>
</p>
</div>
<div id="TicTac" style="display:none">
<table class="tttable" align="center">
<tr>
<td>
<button type="button" class="ttbutton" id="tictac0" onclick="ttbutton(0)" disabled> 
</button>
</td>
<td class="col2">
<button type="button" class="ttbutton" id="tictac1" onclick="ttbutton(1)" disabled> 
</button>
</td>
<td>
<button type="button" class="ttbutton" id="tictac2" onclick="ttbutton(2)" disabled> 
</button>
</td>
</tr>
<tr>
<td class="row2">
<button type="button" class="ttbutton" id="tictac3" onclick="ttbutton(3)" disabled> 
</button>
</td>
<td class="col2 row2">
<button type="button" class="ttbutton" id="tictac4" onclick="ttbutton(4)" disabled> 
</button>
</td>
<td class="row2">
<button type="button" class="ttbutton" id="tictac5" onclick="ttbutton(5)" disabled> 
</button>
</td>
</tr>
<tr>
<td>
<button type="button" class="ttbutton" id="tictac6" onclick="ttbutton(6)" disabled> 
</button>
</td>
<td class="col2">
<button type="button" class="ttbutton" id="tictac7" onclick="ttbutton(7)" disabled> 
</button>
</td>
<td>
<button type="button" class="ttbutton" id="tictac8" onclick="ttbutton(8)" disabled> 
</button>
</td>
</tr>
</table>
<div class="message" id="message"></div>
</div>
<div id="Restart" class="restart" style="display:none">
<button type="button" class="button" onclick="restart()">Play Again</button>
</div>
</div>
<div id="footer">Otterson Tic-Tac-Toe Demo<br>
Your browser's "View-Source" function will show you the front end.<br>
<a href="tictacserver.html">See the back-end code</a></div>
</div>
</body>
</html>