-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (90 loc) · 2.81 KB
/
app.js
File metadata and controls
99 lines (90 loc) · 2.81 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
let boxes = document.querySelectorAll('.box');
let tLeft = document.getElementById('top-left');
let tCenter = document.getElementById('top-center');
let tRight = document.getElementById('top-right');
let mLeft = document.getElementById('middle-left');
let mCenter = document.getElementById('middle-center');
let mRight = document.getElementById('middle-right');
let bLeft = document.getElementById('bottom-left');
let bCenter = document.getElementById('bottom-center');
let bRight = document.getElementById('bottom-right');
let winnerIs = document.getElementById("winnerCircle");
let anywhere = document.getElementById('board');
let x = 0;
let w = 0;
let gameOver = 0;
let c = 0;
function isEven(x) {
return x % 2 == 0;
};
function isOdd(x) {
return Math.abs(x % 2) == 1;
};
boxes.forEach(function(box) {
box.addEventListener('click', boxClicked, true);
});
anywhere.addEventListener('click', itOver, true);
function boxClicked(box) {
// if (c < 11) {
// itOver()
if (w) {
box.textContent += '';
} else if (isEven(x)) {
if (box.target.textContent != 'O') {
x++;
c++;
box.target.textContent = 'X';
checkForWinner();
}
} else {
if (box.target.textContent != 'X') {
x++;
c++;
box.target.textContent = 'O';
checkForWinner();
};
};
// };
};
function checkForWinner() {
let tRow = (tLeft.textContent + tCenter.textContent + tRight.textContent);
let mRow = (mLeft.textContent + mCenter.textContent + mRight.textContent);
let bRow = (bLeft.textContent + bCenter.textContent + bRight.textContent);
let lColumn = (tLeft.textContent + mLeft.textContent + bLeft.textContent);
let cColumn = (tCenter.textContent + mCenter.textContent + bCenter.textContent);
let rColumn = (tRight.textContent + mRight.textContent + bRight.textContent);
let fSlash = (tLeft.textContent + mCenter.textContent + bRight.textContent);
let bSlash = (tRight.textContent + mCenter.textContent + bLeft.textContent);
var pWins = [tRow, mRow, bRow, lColumn, cColumn, rColumn, fSlash, bSlash];
for (var i = 0; i < pWins.length; i++) {
// pWins.forEach(function(possibleWins) {
if (gameOver == 1) {
winnerIs.textContent += '';
boxes.textContent =+ '';
itOver();
gameOver++;
} else if (pWins[i] === 'XXX') {
winnerIs.textContent += 'And the winner is... X!';
gameOver++;
w++;
return true;
} else if (pWins[i] === 'OOO') {
winnerIs.textContent += 'And the winner is... O!';
gameOver++;
w++;
return true;
};
};
if (c == 9 && w !== true) {
winnerIs.textContent += 'Looks like we have a draw!';
gameOver = 3;
c++;
};
};
function itOver() {
if (gameOver === 1) {
window.location.reload();
} else if (gameOver === 3) {
window.location.reload();
};
};