-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
166 lines (125 loc) · 4.65 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
const playerFactory = (symbol) => {
return { symbol };
};
const boardFactory = () => {
const board = []
return { board };
}
const game = () => {
const playerOne = playerFactory("X");
const playerTwo = playerFactory("O");
let currentTurn = playerOne;
let turnCount = 1;
let gameOver = false;
let tieGame = false;
let aiOn = false;
const board = boardFactory();
const aitogglebutton = document.getElementById("toggleai");
const aiStatus = document.querySelector("p");
aitogglebutton.addEventListener("click", () => {
aiOn = !aiOn;
if (aiOn) aiStatus.style.display = "block";
else aiStatus.style.display = "none";
resetGame();
});
const resetButton = document.getElementById("reset");
resetButton.addEventListener("click", resetGame);
function addClickEvents() {
const cells = document.querySelectorAll("div.cell")
cells.forEach(e => e.addEventListener("click", gameLoop, { once: true }))
};
function gameLoop(event) {
if (!gameOver) {
event.target.textContent = currentTurn.symbol;
turnCount++;
updateArray()
if (checkWin()) {
gameOver = true;
updateWinText();
}
else {
if (currentTurn == playerOne) {
currentTurn = playerTwo;
if (aiOn) computerTurn();
}
else currentTurn = playerOne;
}
}
};
function computerTurn() {
let randomInt;
while (currentTurn == playerTwo) {
randomInt = Math.floor(Math.random() * 8) + 1;
if (board[randomInt] == "") {
board[randomInt] = "O";
const cells = document.querySelectorAll("div.cell");
cells[randomInt].removeEventListener("click", gameLoop, { once: true })
for (let i = 0; i < cells.length; i++) {
cells[i].textContent = board[i];
}
if (checkWin()) {
gameOver = true;
updateWinText();
}
currentTurn = playerOne;
turnCount++;
}
}
}
//Update Array
function updateArray() {
const cells = document.querySelectorAll("div.cell");
for (let i = 0; i < cells.length; i++) {
board[i] = cells[i].textContent;
}
};
//Check Win
function checkWin() {
let topRow = board[0] + board[1] + board[2];
let middleRow = board[3] + board[4] + board[5];
let bottomRow = board[6] + board[7] + board[8];
let leftColumn = board[0] + board[3] + board[6];
let middleColumn = board[1] + board[4] + board[7];
let rightColumn = board[2] + board[5] + board[8];
let rightDiagonal = board[0] + board[4] + board[8];
let leftDiagonal = board[2] + board[4] + board[6];
if (topRow == "XXX" || topRow == "OOO") return true;
else if (middleRow == "XXX" || middleRow == "OOO") return true;
else if (bottomRow == "XXX" || bottomRow == "OOO") return true;
else if (rightDiagonal == "XXX" || rightDiagonal == "OOO") return true;
else if (leftDiagonal == "XXX" || leftDiagonal == "OOO") return true;
else if (leftColumn == "XXX" || leftColumn == "OOO") return true;
else if (middleColumn == "XXX" || middleColumn == "OOO") return true;
else if (rightColumn == "XXX" || rightColumn == "OOO") return true;
else if (turnCount > 9) {
tieGame = true;
return true;
}
else return false;
}
function updateWinText() {
const winnerText = document.getElementById("winner");
const winnerHeader = document.querySelector("h3");
winnerHeader.style.display = "block";
if (tieGame) winnerText.textContent = "Its a tie!";
else winnerText.textContent = `Player ${currentTurn.symbol} Wins!`;
}
function resetGame() {
const cells = document.querySelectorAll("div.cell");
const winnerHeader = document.querySelector("h3");
cells.forEach(e => {
e.textContent = "";
e.removeEventListener("click", gameLoop, { once: true });
e.addEventListener("click", gameLoop, { once: true });
});
updateArray();
winnerHeader.style.display = "none";
gameOver = false;
turnCount = 1;
tieGame = false;
currentTurn = playerOne;
}
addClickEvents();
return { updateArray, board, checkWin, gameOver, resetGame }
}
let ticTacToe = game();