-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
253 lines (208 loc) · 8.24 KB
/
app.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import loadBoard from "./initializeBoard.js";
export const gameBoard = (()=> {
const board = [];
const columns = 7;
const rows = 6;
for (let i = 0; i < rows; i++){
board[i] = [];
for(let j = 0; j < columns; j++){
board[i].push(cell())
}
}
const dropToken = (column, token) =>{
const filteredBoard = board.filter((rows) => rows[column].getValue() === "").map((row) => row[column]);
if(!filteredBoard.length) return;
const lowestRow = filteredBoard.length - 1
board[lowestRow][column].addToken(token);
}
return {
dropToken,
board
}
})()
function cell (){
let value = "";
const addToken = (token) => value = token;
const getValue = () => value;
return {
getValue,
addToken
}
};
const Player = (token) =>{
const getToken = () => token;
return {
getToken
}
}
// initial board render
const gameController = (() =>{
let isOver = false;
const player1 = Player('red');
const player2 = Player('yellow');
const players = [player1, player2];
const randomPlayer = Math.floor(Math.random() * players.length);
let currentPlayer = players[randomPlayer].getToken();
const playRound = (column) => {
gameBoard.dropToken(column, currentPlayer);
// check win
if (checkWin()){
displayController.setResultMessage(currentPlayer);
isOver = true;
return;
};
// check draw
const checkEmptySpaces = gameBoard.board.map(rows => rows.filter(columns => columns.getValue() === "")).reduce((rows, columns) => rows.concat(columns))
if(!checkEmptySpaces.length){
displayController.setResultMessage("draw");
isOver = true;
return;
}
switchPlayer();
displayController.setDisplayMessage(`${currentPlayer[0].toUpperCase() + currentPlayer.substr(1)}'s Turn`);
}
const switchPlayer = () => {
currentPlayer = (currentPlayer === player1.getToken()) ? player2.getToken() : player1.getToken();
}
const checkWin = () => {
// get gameboard
const board = gameBoard.board
let rows = 6;
let columns = 7;
//horizontal check
for(let i = 0; i< rows; i++){
for(let j = 0; j< columns - 3; j++){
if(board[i][j].getValue() === currentPlayer && board[i][j+1].getValue() === currentPlayer && board[i][j+2].getValue() === currentPlayer && board[i][j+3].getValue() === currentPlayer){
return true;
}
}
}
//vertical check
for(let i = 0; i < rows - 3; i++){
for(let j = 0; j< columns; j++){
if(board[i][j].getValue() === currentPlayer && board[i+1][j].getValue() === currentPlayer && board[i+2][j].getValue() === currentPlayer && board[i+3][j].getValue() === currentPlayer){
return true;
}
}
}
// diagonal check -- descending
for(let i = 0; i < rows - 3; i++){
for(let j = 0; j< columns - 3; j++){
if(board[i][j].getValue() === currentPlayer && board[i+1][j+1].getValue() === currentPlayer && board[i+2][j+2].getValue() === currentPlayer && board[i+3][j+3].getValue() === currentPlayer){
return true;
}
}
}
// diagonal check -- ascending
for(let i = rows - 3; i < rows; i++){
for(let j = 0; j< columns - 3; j++){
if(board[i][j].getValue() === currentPlayer && board[i-1][j+1].getValue() === currentPlayer && board[i-2][j+2].getValue() === currentPlayer && board[i-3][j+3].getValue() === currentPlayer){
return true;
}
}
}
}
const getCurrentPlayer = () => currentPlayer;
const getIsOver = () => isOver;
return{
playRound,
getCurrentPlayer,
getIsOver
}
})();
const displayController = (() => {
loadBoard();
const boardCells = document.querySelectorAll('.cell');
const allCells = document.querySelectorAll('button');
const hoverCells = document.querySelectorAll(".hover-cell");
const messageEl = document.querySelector(".message");
const firstPlayer = gameController.getCurrentPlayer();
document.addEventListener("DOMContentLoaded", ()=> {
document.querySelectorAll('button').forEach(button => {
button.classList.add(firstPlayer);
});
setDisplayMessage(`${firstPlayer[0].toUpperCase() + firstPlayer.substr(1)}'s Turn`);
})
boardCells.forEach(cells => cells.addEventListener('click', (e) => {
if(e.target.classList.contains("played") || gameController.getIsOver()) return;
gameController.playRound(parseInt(e.target.dataset.index));
toggleClass(gameController.getCurrentPlayer());
updateScreen();
removeBoardHover();
}))
const toggleClass = (currentPlayer) => {
allCells.forEach(button => {
button.classList.remove('red');
button.classList.remove('yellow');
if (currentPlayer === 'red'){
button.classList.toggle('red');
} else if (currentPlayer === 'yellow'){
button.classList.toggle('yellow');
}
})
}
const updateScreen = () => {
const board = gameBoard.board;
const boardContents = board.reduce((a,b) => a.concat(b));
boardCells.forEach((cells, index) => {
if(boardContents[index].getValue() === 'red'){
cells.classList.add("show-red");
cells.classList.add("played");
}else if(boardContents[index].getValue() === 'yellow'){
cells.classList.add("show-yellow");
cells.classList.add("played");
}
})
}
const handleHoverDiv = (() => {
const column1 = document.querySelectorAll('[data-index="0"]');
const column2 = document.querySelectorAll('[data-index="1"]');
const column3 = document.querySelectorAll('[data-index="2"]');
const column4 = document.querySelectorAll('[data-index="3"]');
const column5 = document.querySelectorAll('[data-index="4"]');
const column6 = document.querySelectorAll('[data-index="5"]');
const column7 = document.querySelectorAll('[data-index="6"]');
const columns = [column1, column2, column3, column4, column5, column6, column7];
columns.forEach((column, index) => {
column.forEach(cells =>{
cells.addEventListener("mouseover", () =>{
if(cells.classList.contains("played")) return;
if(cells.classList.contains("yellow")){
hoverCells[index].classList.add("show-yellow");
} else if(cells.classList.contains("red")){
hoverCells[index].classList.add("show-red");
}
})
cells.addEventListener("mouseout", () =>{
hoverCells[index].classList.remove("show-yellow");
hoverCells[index].classList.remove("show-red");
})
cells.addEventListener("click", () =>{
if(cells.classList.contains("played") || gameController.getIsOver()) return;
hoverCells[index].classList.toggle("show-yellow");
hoverCells[index].classList.toggle("show-red");
})
})
})
})()
const removeBoardHover = () => {
boardCells.forEach(cells => {
if(cells.classList.contains("played")){
cells.classList.remove("red");
cells.classList.remove("yellow");
}
})
}
const setResultMessage = (winner) => {
if(winner === "draw"){
setDisplayMessage("It's a Draw!⛔");
return;
}
setDisplayMessage(`${winner[0].toUpperCase() + winner.substr(1)} wins!!💥`)
}
const setDisplayMessage = (message) => messageEl.textContent = message
return {
setDisplayMessage,
setResultMessage
}
})()