This repository has been archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (137 loc) · 4.53 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
import { miniMaxMove } from './minimax.js';
let gameboard = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
const X_CLASS = 'x'
const O_CLASS = 'o'
let cellElements = document.querySelectorAll('[data-cell]')
let htmlBoard = document.getElementById('board')
let winningMessageElement = document.getElementById('winningMessage')
let restartButtonOne = document.getElementById('restartButtonOne')
let restartButtonTwo = document.getElementById('restartButtonTwo')
let winningMessageTextElement = document.querySelector('[data-winning-message-text]')
let oTurn = true
let computerTurn = 2
window.addEventListener('DOMContentLoaded', (event) => {
cellElements = document.querySelectorAll('[data-cell]')
htmlBoard = document.getElementById('board')
winningMessageElement = document.getElementById('winningMessage')
restartButtonOne = document.getElementById('restartButtonOne')
restartButtonTwo = document.getElementById('restartButtonTwo')
winningMessageTextElement = document.querySelector('[data-winning-message-text]')
restartButtonOne.addEventListener('click', startGameOne)
restartButtonTwo.addEventListener('click', startGameTwo)
});
function startGameOne() {
oTurn = Math.random() >= 0.5;
computerTurn = Math.round(Math.random())
restartGame()
if ((oTurn === true && computerTurn === 0) || (oTurn === false && computerTurn === 1)) {
processNewInput(miniMaxMove(gameboard, computerTurn))
}
}
function startGameTwo() {
oTurn = Math.random() >= 0.5;
computerTurn = 2
restartGame()
}
function restartGame() {
winningMessageElement.classList.remove('show')
gameboard = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
drawBoard(gameboard)
}
function drawBoard(board) {
// Draw Cells
for (let i = 0; i < cellElements.length; i++) {
let cell = cellElements[i]
cell.classList.remove(X_CLASS)
cell.classList.remove(O_CLASS)
if (getBoard(board, i) === 0) {
cell.classList.add(O_CLASS)
cell.removeEventListener('click', handleClick)
}
else if (getBoard(board, i) === 1) {
cell.classList.add(X_CLASS)
cell.removeEventListener('click', handleClick)
}
else {
cell.addEventListener('click', handleClick, { once: true })
}
}
// Set Hover State
if (oTurn) {
htmlBoard.classList.remove(X_CLASS)
htmlBoard.classList.add(O_CLASS)
}
else {
htmlBoard.classList.remove(O_CLASS)
htmlBoard.classList.add(X_CLASS)
}
}
function handleClick(e) {
const cell = e.target
const i = cell.id
processNewInput(i)
if (computerTurn !== 2 && checkWin(gameboard) < 0) {
processNewInput(miniMaxMove(gameboard, computerTurn))
}
}
function processNewInput(cellNumber) {
const newClass = oTurn ? 0 : 1
setBoard(gameboard, cellNumber, newClass)
oTurn = !oTurn
drawBoard(gameboard)
if (checkWin(gameboard) >= 0) {
endGame()
}
}
export function setBoard(board, i, val) {
board[Math.floor(i / 3)][i % 3] = val
return board
}
export function getBoard(board, i) {
return (board[Math.floor(i / 3)][i % 3])
}
export function checkWin(board) {
for (var i = 0; i < 3; i++) {
if (board[i][0] === board[i][1] && board[i][1] === board[i][2] && board[i][0] !== -1) { return board[i][0] }
if (board[0][i] === board[1][i] && board[1][i] === board[2][i] && board[0][i] !== -1) { return board[0][i] }
}
if (board[0][0] === board[1][1] && board[1][1] === board[2][2] && board[1][1] !== -1) { return board[1][1] }
if (board[2][0] === board[1][1] && board[1][1] === board[0][2] && board[1][1] !== -1) { return board[1][1] }
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
if (board[i][j] < 0) { return -1 }
}
}
return 2
}
function endGameMessage(winner) {
let text = ''
if (winner === 2) {
text = "Draw!"
}
else if (computerTurn !== 2) {
if (computerTurn === winner) {
text = "You lost!"
}
else {
text = "You won!"
}
}
else {
if (winner === 0) {
text = "O won!"
}
else {
text = "X won!"
}
}
return text
}
function endGame() {
for (let i = 0; i < cellElements.length; i++) {
let cell = cellElements[i]
cell.removeEventListener('click', handleClick)
}
winningMessageElement.classList.add('show')
winningMessageTextElement.innerText = endGameMessage(checkWin(gameboard))
}