-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
211 lines (175 loc) · 5.21 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
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
// DOM ELEMENTS
const squareOne = document.getElementById("grid__square-1");
const squareTwo = document.getElementById("grid__square-2");
const squareThree = document.getElementById("grid__square-3");
const squareFour = document.getElementById("grid__square-4");
const squareFive = document.getElementById("grid__square-5");
const squareSix = document.getElementById("grid__square-6");
const squareSeven = document.getElementById("grid__square-7");
const squareEight = document.getElementById("grid__square-8");
const squareNine = document.getElementById("grid__square-9");
const allSquares = document.querySelectorAll(".grid__square");
const playerOneScore = document.getElementById("info__player__score1");
const playerTwoScore = document.getElementById("info__player__score2");
const infoText = document.getElementById("instructions__text");
const startGameBtn = document.getElementById("instructions__btn");
const modal = document.getElementById("modal");
// VARIABLES
const players = {
playerOne: { name: "Jack", wins: 0 },
playerTwo: { name: "Jill", wins: 0 },
};
let move = 1;
let nextPlayer = players.playerOne.name;
let pastPlayer;
let currentImage = "cross";
let playerHasWon = false;
// SQUARE CLICKING
function addSquareClick() {
allSquares.forEach((square) => {
square.addEventListener("click", squareClick);
});
}
function removeSquareClick() {
allSquares.forEach((square) => {
square.removeEventListener("click", squareClick);
});
}
function squareClick() {
if (!this.classList.contains("cross") && !this.classList.contains("circle")) {
this.classList.add(`${currentImage}`);
incrementMove();
}
}
// INCREMENT MOVE
function incrementMove() {
move += 1;
if (move % 2 !== 0) {
nextPlayer = players.playerOne.name;
pastPlayer = players.playerTwo.name;
currentImage = "cross";
infoText.innerHTML = `${players.playerOne.name}'s turn`;
} else {
nextPlayer = players.playerTwo.name;
pastPlayer = players.playerOne.name;
currentImage = "circle";
infoText.innerHTML = `${players.playerTwo.name}'s turn`;
}
checkForWin();
checkForTie();
}
// CHECK FOR WIN
function checkForWin() {
const lines = [
[squareOne, squareTwo, squareThree],
[squareFour, squareFive, squareSix],
[squareSeven, squareEight, squareNine],
[squareOne, squareFour, squareSeven],
[squareTwo, squareFive, squareEight],
[squareThree, squareSix, squareNine],
[squareOne, squareFive, squareNine],
[squareThree, squareFive, squareSeven],
];
for (const line of lines) {
const hasCross = line.every((square) => square.classList.contains("cross"));
const hasCircle = line.every((square) =>
square.classList.contains("circle")
);
if (hasCross || hasCircle) {
const winner = hasCross ? players.playerOne : players.playerTwo;
winner.wins += 1;
updateScores();
playerWon();
return;
}
}
}
function updateScores() {
playerOneScore.innerHTML = players.playerOne.wins;
playerTwoScore.innerHTML = players.playerTwo.wins;
}
function playerWon() {
infoText.innerHTML = `${pastPlayer} won!`;
playerHasWon = true;
continueGame();
}
// CHECK FOR TIE
function checkForTie() {
const squares = [
squareOne,
squareTwo,
squareThree,
squareFour,
squareFive,
squareSix,
squareSeven,
squareEight,
squareNine,
];
const allSquaresFilled = squares.every((square) => {
return (
square.classList.contains("cross") || square.classList.contains("circle")
);
});
if (allSquaresFilled && !playerHasWon) {
infoText.innerHTML = "It's a tie!";
continueGame();
}
}
// CONTINUE / RESTART / RESET
function continueGame() {
removeSquareClick();
setTimeout(() => {
reset();
}, 2000);
}
function restartGame() {
removeSquareClick();
reset();
}
function reset() {
allSquares.forEach((square) => {
square.classList = "grid__square";
});
addSquareClick();
playerHasWon = false;
infoText.innerHTML = `${nextPlayer}'s turn to start`;
}
// START GAME
function startGame() {
startGameBtn.addEventListener("click", () => {
modal.style.display = "flex";
});
const form = document.querySelector("form");
form.addEventListener("submit", (event) => {
event.preventDefault();
const player1Input = document
.getElementById("player1")
.value.trim()
.toLowerCase();
const player2Input = document
.getElementById("player2")
.value.trim()
.toLowerCase();
const player1InputCap =
player1Input.charAt(0).toUpperCase() + player1Input.slice(1);
const player2InputCap =
player2Input.charAt(0).toUpperCase() + player2Input.slice(1);
players.playerOne.name = player1InputCap;
players.playerTwo.name = player2InputCap;
nextPlayer = player1InputCap;
document.getElementById("info__player__name1").innerHTML =
players.playerOne.name;
document.getElementById("info__player__name2").innerHTML =
players.playerTwo.name;
players.playerOne.wins = 0;
players.playerTwo.wins = 0;
updateScores();
infoText.innerHTML = `${players.playerOne.name}'s turn to start`;
modal.style.display = "none";
startGameBtn.innerHTML = "Restart Game";
addSquareClick();
restartGame();
});
}
startGame();