-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic-tac-toe.js
169 lines (143 loc) · 3.6 KB
/
tic-tac-toe.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
window.onload = function () {
// set variables for dom elements
var board = document.getElementById("board");
var newGameBtn = document.getElementById("newGameBtn");
var currentPlayerBox = document.getElementById("currentPlayerBox");
var boxes = document.getElementsByClassName("box");
var winnerBox = document.getElementById("winnerBox");
// set variables for game state
var boardState = [
["", "", ""],
["", "", ""],
["", "", ""],
];
var winner = null;
var currentPlayer = {
name: "Player 1",
value: 1,
};
// set listeners
newGameBtn.onclick = newGame;
function initBoxes() {
for (let i = 0; i < boxes.length; i++) {
boxes[i].onclick = function () {
const position = this.dataset.position.split(",");
placeMarker(currentPlayer.value, position);
};
}
}
function init() {
initBoxes();
render();
}
function render() {
renderBoard();
renderCurrentWinner();
renderCurrentPlayer();
}
function renderCurrentPlayer() {
currentPlayerBox.innerHTML = currentPlayer.name;
}
function renderCurrentWinner() {
winnerBox.innerHTML = winner ? "Player " + winner : "No winner yet";
winner
? winnerBox.classList.add("text-success")
: winnerBox.classList.remove("text-success");
}
function renderBoard() {
board.innerHTML = boardState
.map(function (row, i) {
return row
.map(
(col, n) =>
'<div class="box" data-position="' +
[i, n] +
'">' +
boardState[i][n] +
"</div>"
)
.join("");
})
.join("");
initBoxes();
}
function setWinner(value) {
winner = value;
render();
}
function checkValsForWin(vals) {
if (vals.every((val) => val === "X")) {
setWinner(1);
} else if (vals.every((val) => val === "O")) {
setWinner(2);
}
}
function checkForWinner() {
let column1 = boardState.map((row) => row[0]);
let column2 = boardState.map((row) => row[1]);
let column3 = boardState.map((row) => row[2]);
let columns = [column1, column2, column3];
columns.map((col) => checkValsForWin(col));
let rows = boardState.map((row) => row);
rows.map((row) => checkValsForWin(row));
let count = 0;
let diagonal1 = boardState.map((row, i) => {
if (i > 0) count++;
return row[count];
});
let d2Count = 2;
let diagonal2 = boardState.map((row, i) => {
if (i > 0) count--;
return row[count];
});
let diagonals = [diagonal1, diagonal2];
diagonals.map((diagonal) => checkValsForWin(diagonal));
render();
}
function newGame() {
resetBoard();
clearWinner();
init();
}
function clearWinner() {
setWinner(null);
render();
}
function resetBoard() {
const newBoard = boardState.map((row, i) =>
row.map((col, n) => (boardState[i][n] = ""))
);
}
function getMarker(player) {
return {
1: "X",
2: "O",
}[player];
}
function updateBoard(row, col, value) {
if (!boardState[row][col]) {
boardState[row][col] = value;
togglePlayer();
render();
}
}
function placeMarker(player, position) {
if (!winner) {
const marker = getMarker(player);
updateBoard(position[0], position[1], marker);
checkForWinner();
}
}
function togglePlayer() {
currentPlayer =
currentPlayer.value === 1
? { name: "Player 2", value: 2 }
: { name: "Player 1", value: 1 };
render();
}
function newGame() {
resetBoard();
render();
}
init();
};