-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
176 lines (165 loc) · 5.5 KB
/
app.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
(() => {
//contains the list of coordinates of the winning combinations, plus the characters used for the two players
const gameRules = {
winningConditions: [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
],
players: ["X", "O"],
};
//set up gameboard
let curPlayer;
let gameBoard = new Array(9);
/**
* resets the game's UI:
* re-enables all squares and hover effects
* clears the end-game message
*/
const resetUI = () => {
document.querySelectorAll(".ttt-square").forEach((item) => {
item.textContent = "";
item.classList.remove("square-x", "square-o", "winning-line");
item.disabled = false;
});
document.querySelector(".ttt-grid").classList.add("hover-active");
document.querySelector("#message").textContent = "";
updateUI();
};
/**
* updates the game's UI (to be run after each turn):
* current player indicator
* current hover effects
*/
const updateUI = () => {
document.querySelector("#cur-player").textContent =
gameRules.players[curPlayer];
document
.querySelector(".app-container")
.classList.remove("use-color-x", "use-color-o");
document
.querySelector(".app-container")
.classList.add(`use-color-${gameRules.players[curPlayer].toLowerCase()}`);
};
/**
* Restarts (or starts) the game:
* clears the board
* sets X as the current player
* resets all UI elements
*/
const restartGame = () => {
isGameRunning = true;
curPlayer = 0;
gameBoard.fill("");
resetUI();
};
/**
* Checks for a draw condition, which is met if every square is full
* @returns a boolean value.
*/
const checkForDraw = () => {
return gameBoard.every((sq) => {
return sq !== "";
});
};
/**
* Checks the game board for winning conditions,
* returns false if the game hasn't been won yet
* if the game has been won, returns the winning player
* @returns either the winning player, or false if the game hasn't been won yet
*/
const checkForWin = () => {
let hasWon = false;
//iterate through each possible winning condition
gameRules.winningConditions.forEach((winningCondition) => {
//create an array containing the values of the game board in the positions for this winning condition
const lineToCheck = winningCondition.map((sqIndex) => {
return gameBoard[sqIndex];
});
//check if 1) all squares on this line are equal AND 2) all squares in this line have a value
const lineIsWin = lineToCheck.every((value, _index, array) => {
return value === array[0] && value !== "";
});
//if the line is a winning condition, return the player who has won.
//add the 'winning line' around the squares that created the win condition
if (lineIsWin) {
hasWon = lineToCheck[0];
winningCondition.forEach((sqIndex) => {
document
.querySelector(`[data-sq-index="${sqIndex}"]`)
.classList.add("winning-line");
});
}
});
return hasWon;
};
/**
* Event handler for all the squares
* @param event - the event object that was triggered
*/
const handleSqClick = (event) => {
if (isGameRunning) {
//event handler for squares - start by getting the index of the square clicked
const sqIndex = event.target.getAttribute("data-sq-index");
//check if the square is empty - if so, add a mark to it
if (isEmpty(sqIndex)) {
gameBoard[sqIndex] = gameRules.players[curPlayer];
const curSquareElem = document.querySelector(
`[data-sq-index='${sqIndex}']`
);
//UI updates to mark the square:
//unfocus the square (to get rid of focus/hover styling)
curSquareElem.blur();
//add the player character (X or O) to the square
curSquareElem.textContent = gameRules.players[curPlayer];
//disable the square so it is not clickable/selectable
curSquareElem.disabled = true;
//add a class to the square to color it according to player
curSquareElem.classList.add(curPlayer === 0 ? "square-x" : "square-o");
//check to see if a win or draw condition has occurred
const isWin = checkForWin();
const isDraw = checkForDraw();
if (isWin) {
endGame(`Congrats! Player ${isWin} won!`);
} else if (isDraw) {
endGame(`It's a draw!`);
} else {
//switch players
curPlayer = curPlayer === 1 ? 0 : 1;
updateUI();
}
}
}
};
/**
* Ends the game:
* displays the message passed as a parameter, and disables hover effects
* @param message - The message to display to the user.
*/
const endGame = (message) => {
document.querySelector("#message").textContent = message;
isGameRunning = false;
document.querySelector(".ttt-grid").classList.remove("hover-active");
};
/**
* Check if a square is empty (legal to click)
* @param index - the index of the square that we want to check
* @returns The function isEmpty is returning a boolean value.
*/
const isEmpty = (index) => {
return gameBoard[index] === "";
};
//add event listener to squares
document.querySelectorAll(".ttt-square").forEach((el) => {
el.addEventListener("click", handleSqClick);
});
//add event listener for Reset button
document.getElementById("btn-restart").addEventListener("click", restartGame);
//start the game
restartGame();
})();