-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
141 lines (120 loc) · 3.38 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
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
window.onload = () => {
// Variables
const squares = document.querySelectorAll(".game-square");
const resetButton = document.querySelector("#reset");
const winScreen = document.querySelector(".win-screen");
class Player {
constructor(turn) {
this.turn = turn;
}
}
const player = new Player(true);
// Functions
function resetGame() {
squares.forEach((square) => {
square.innerText = "";
square.classList.add("hover-state");
});
player.turn = true;
if (winScreen.classList.contains("place-win-middle")) {
winScreen.classList.remove("place-win-middle");
}
}
function displayWinner(winner) {
const winnerText = document.querySelector("#actual-winner-text");
if (winner == "x") {
winnerText.innerText = "You Win!";
} else {
winnerText.innerText = "You Lose!";
}
winScreen.classList.add("place-win-middle");
}
function setHoverState(condition) {
if (condition == true) {
squares.forEach((square) => {
if (square.innerText == "") {
square.classList.add("hover-state");
}
});
} else {
squares.forEach((square) => {
square.classList.remove("hover-state");
});
}
}
function checkWin() {
let sqOne, sqTwo, sqThree, sqFour, sqFive, sqSix, sqSeven, sqEight, sqNine;
let winState = false;
[sqOne, sqTwo, sqThree, sqFour, sqFive, sqSix, sqSeven, sqEight, sqNine] = [
...squares,
].map((square) => {
return square.innerText;
});
if (sqOne == sqTwo && sqOne == sqThree && sqOne != "") {
return sqOne;
}
if (sqOne == sqFive && sqOne == sqNine && sqOne != "") {
return sqOne;
}
if (sqOne == sqFour && sqOne == sqSeven && sqOne != "") {
return sqOne;
}
if (sqTwo == sqFive && sqTwo == sqEight && sqTwo != "") {
return sqTwo;
}
if (sqThree == sqSix && sqThree == sqNine && sqThree != "") {
return sqThree;
}
if (sqThree == sqFive && sqThree == sqSeven && sqThree != "") {
return sqThree;
}
if (sqFour == sqFive && sqFour == sqSix && sqFour != "") {
return sqFour;
}
if (sqSeven == sqEight && sqSeven == sqNine && sqSeven != "") {
return sqSeven;
}
}
function robotTurn() {
const emptySquares = [...squares].filter((square) => {
return square.innerText == "";
});
const randomNumber = Math.floor(Math.random() * emptySquares.length);
emptySquares[randomNumber].innerText = "o";
let winner = checkWin();
if (winner == "o") {
displayWinner(winner);
}
player.turn = true;
setHoverState(true);
}
function displayLetter(square) {
square.innerText = "x";
let winner = checkWin();
if (winner == "x") {
displayWinner(winner);
return true;
}
}
function checkTaken(e) {
// Check if square is empty
if (player.turn == true) {
// If select empty block
if (e.currentTarget.innerText == "") {
player.turn = false;
// displays letter and returns true if win
if (displayLetter(e.currentTarget)) {
} else {
setTimeout(robotTurn, 1000);
}
// Add check win here and remove from other
setHoverState(false);
}
}
}
// Event Listeners
squares.forEach((square) => {
square.addEventListener("click", checkTaken);
});
reset.addEventListener("click", resetGame);
};