-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
60 lines (52 loc) · 1.39 KB
/
script.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
var turn = 0;
var board = ['', '', '', '', '', '', '', '', ''];
var win = false;
var winner = '';
function play(cell) {
if (cell.innerHTML == '') {
if (turn % 2 == 0) {
cell.innerHTML = 'X';
} else {
cell.innerHTML = 'O';
}
turn++;
checkWin(cell.innerHTML);
}
}
function checkWin(p) {
for (i = 1; i < 10; i++) {
board[i - 1] = document.getElementById('c' + i).innerHTML;
}
if ((board[0] == p && board[1] == p && board[2] == p) ||
(board[3] == p && board[4] == p && board[5] == p) ||
(board[6] == p && board[7] == p && board[8] == p) ||
(board[0] == p && board[3] == p && board[6] == p) ||
(board[1] == p && board[4] == p && board[7] == p) ||
(board[2] == p && board[5] == p && board[8] == p) ||
(board[0] == p && board[4] == p && board[8] == p) ||
(board[2] == p && board[4] == p && board[6] == p)) {
winner = 'Player ' + p + ' won!';
win = true;
gameOver();
}
if (win == false && turn == 9) {
winner = 'Draw';
gameOver();
}
console.log(turn);
}
function gameOver() {
document.getElementById('player').innerHTML = winner;
document.getElementById('panel').style.display = 'block';
}
function restart() {
document.getElementById('panel').style.display = 'none';
restore();
}
function restore() {
for (i = 1; i < 10; i++) {
document.getElementById('c' + i).innerHTML = '';
}
turn = 0;
win = false;
}