-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (175 loc) · 5.04 KB
/
index.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//Variables
const textO = "O";
const textX = "X";
let currentPlayer = textX;
let random;
let spaces = [null, null, null, null, null, null, null, null, null];
let currentUsername;
let vsComputer = false;
//Pop up window for name
// window.onload = function () {
// let username = window.prompt("Enter your name");
// document.getElementById("playText").innerHTML = username + " vs. Computer";
// currentUsername = username;
// document.getElementById("playerOne").innerHTML = "playerOne" + username;
// if (username = 'null') {
// document.getElementById("playText").innerHTML = ('You' + " vs. Computer")
// } else {
// return;
// }
// };
//vs button - toggles vs computer to be true
//have a form for names and display names- also two player
//add remove event listener -takes types and function- cant be inside of current event
//score counter
let restartButton = document.getElementById("restartButton");
let cells = Array.from(document.getElementsByClassName("cell"));
let playText = document.getElementById("playText");
function computerPlayer() {
random = Math.floor(Math.random() * cells.length);
//Accessing random cells for computer player
if (cells[random].innerText === "") {
spaces[random] = currentPlayer;
console.log(spaces);
cells[random].innerText = currentPlayer;
//for every random cell that has an empty string, put value of currentPlayer
if (checkForWin()) {
playText.innerText = `${currentPlayer} WINS!`;
return;
}
switchPlayers();
//recursion
} else {
let counter = 0; //how many open spots are currently on the board
for (let i = 0; i < cells.length; i++) {
if (cells[i].innerText === "") {
counter++;
}
}
if (counter) {
//if counter is true, (1 or greater) then we know we have empty spots on board and we re-run computerPlayer to try to find new number for empty cell
computerPlayer();
}
}
}
function switchPlayers() {
if (currentPlayer == textX) {
currentPlayer = textO;
} else {
currentPlayer = textX;
}
}
// const board = document.getElementById("board");
// board.removeEventListener("click", );
//Click event for clicked cells
const board = document.getElementById("board");
board.addEventListener("click", function (event) {
//passing an event so has "event parameter"
if (!event.target.innerText) {
//if the targeted event is falsy and has no text
// console.log(event.target.innerText)
event.target.innerText = currentPlayer;
spaces[event.target.id] = currentPlayer;
if (checkForWin()) {
playText.innerText = `${currentPlayer} WINS!`;
return;
}
if (vsComputer) {
computerPlayer();
} else {
if (currentPlayer == textX) {
//alternating between players
currentPlayer = textO;
} else {
currentPlayer = textX;
}
}
}
});
const winningCombos = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
// Winning function
function checkForWin() {
for (let i = 0; i < winningCombos.length; i++) {
let arr = winningCombos[i];
let boardIndexA = arr[0];
let boardIndexB = arr[1];
let boardIndexC = arr[2];
if (spaces[boardIndexA] !== null) {
if (
spaces[boardIndexA] === spaces[boardIndexB] &&
spaces[boardIndexA] === spaces[boardIndexC]
) {
return true;
}
}
}
checkDraw();
return false;
}
function checkForWin() {
for (let i = 0; i < winningCombos.length; i++) {
let arr = winningCombos[i];
let boardIndexA = arr[0];
let boardIndexB = arr[1];
let boardIndexC = arr[2];
if (spaces[boardIndexA] !== null) {
if (
spaces[boardIndexA] === spaces[boardIndexB] &&
spaces[boardIndexA] === spaces[boardIndexC]
) {
board.removeEventListener("click", handleBoardClick); // remove event listener from the board
playText.innerText = `${currentPlayer} WINS!`;
return true;
}
}
}
if (checkDraw()) {
board.removeEventListener("click", handleBoardClick); // remove event listener from the board
return true;
}
return false;
}
function handleBoardClick(event) {
if (!event.target.innerText) {
event.target.innerText = currentPlayer;
spaces[event.target.id] = currentPlayer;
if (checkForWin()) {
return;
}
switchPlayers();
}
}
//Draw function
function checkDraw() {
for (let i = 0; i < spaces.length; i++) {
const element = spaces[i];
if (element === null) {
return false;
}
}
playText.innerText = "It's a draw!";
}
//Click event for restart button
restartButton.addEventListener("click", function () {
for (let i = 0; i < spaces.length; i++) {
document.getElementById(`${i}`).innerHTML = "";
spaces[i] = null;
}
playText.innerText = "Let's Play!";
currentPlayer = textX;
//pass currentUsername
});
//Accessing random cells once game
const randomCell = Math.floor(Math.random() * cells.length);
// cells[randomCell].innerText='O'
//display hidden CSS for input element
// one player vs multi player