-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (108 loc) · 3.43 KB
/
script.js
File metadata and controls
125 lines (108 loc) · 3.43 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
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
// Attach an event listener to the "cells"
let cells = document.querySelectorAll(".cell");
cells.forEach(function (cell) {
cell.addEventListener("click", game);
});
// Global variables that hold the game state
const numRows = 3;
const numCols = 3;
let numEmptyCells = numRows * numCols;
const board = new Array(numEmptyCells);
const markers = ["x", "o"];
let player = 0;
let gameIsOver = false;
// The main event listener and the controller for the game
function game(event) {
if (gameIsOver)
return window.alert(
"This game has ended. Refresh the page for a new game!"
);
let cell = event.target;
let index = cell.id;
if (board[index]) return window.alert("This cell has already been marked!");
// update the board
board[index] = markers[player];
cell.classList.add(markers[player]);
numEmptyCells--;
updateGameStatus();
switchPlayer();
}
// --- Helper Methods ---
// Return the linear index corresponding to the row and column subscripts
function toLinearIndex(row, col) {
return row * numRows + col;
}
// Switches the player value from 0 to 1 and vice versa
function switchPlayer() {
player = player === 0 ? 1 : 0;
}
// Updates gameIsOver to true if a player won the game
// or the game ended in a draw!
// Side effect: displays an alert with a message as to
// who won the game or that the game ended in a draw!
function updateGameStatus() {
if (checkRows() || checkColumns() || checkDiagonals()) {
window.alert(`${markers[player]} won!`);
gameIsOver = true;
} else if (numEmptyCells === 0) {
window.alert(`It's a draw!`);
gameIsOver = true;
}
}
// Returns true if all cells in a row
// are marked with the same marker, otherwise returns false
function checkRows() {
for (let row = 0; row < numRows; row++) {
if (checkRow(row)) return true;
}
return false;
}
// Returns true if all cells in the given row
// are marked with the same marker, otherwise returns false
function checkRow(row) {
for (let col = 0; col < numCols; col++) {
let index = toLinearIndex(row, col);
if (board[index] !== markers[player]) return false;
}
return true;
}
// Returns true if all cells in a column
// are marked with the same marker, otherwise returns false
function checkColumns() {
for (let col = 0; col < numCols; col++) {
if (checkColumn(col)) return true;
}
return false;
}
// Returns true if all cells in the given column
// are marked with the same marker, otherwise returns false
function checkColumn(col) {
for (let row = 0; row < numRows; row++) {
let index = toLinearIndex(row, col);
if (board[index] !== markers[player]) return false;
}
return true;
}
// Returns true if all cells in the major or minor diagonal
// are marked with the same marker, otherwise returns false
function checkDiagonals() {
return checkMajorDiagonal() || checkMinorDiagonal();
}
// Returns true if all cells in the major diagonal
// are marked with the same marker, otherwise returns false
function checkMajorDiagonal() {
for (let row = 0; row < numRows; row++) {
let index = toLinearIndex(row, row);
if (board[index] !== markers[player]) return false;
}
return true;
}
// Returns true if all cells in the minor diagonal
// are marked with the same marker, otherwise returns false
function checkMinorDiagonal() {
for (let row = 0; row < numRows; row++) {
let index = toLinearIndex(row, numCols - row - 1);
if (board[index] !== markers[player]) return false;
}
return true;
}