-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
264 lines (212 loc) · 7.99 KB
/
script.js
File metadata and controls
264 lines (212 loc) · 7.99 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
// tic tac toe instructions
// Store the gameboard as an array inside of a gameboard object
// your players are going to be stored in objects and you're probably going to want an object to control the flow of the game itself
// have as little global code as possible, tuck as much as you can in factories
// if you only need a single instance of something (eg. the gameboard, the displayController etc.), then wrap the factory inside an IFFE so it cannot be used to create additional instances
// think carefully about where each bit of logic should reside, each piece of functionality should be able to fit in the game, player or gameboard objects. Spend some time brainstorming here
// focus on getting a working game in the console first..
// ___________Game Board______________
const gameBoard = (function() {
// This variable should be private so that the user cannot manipulate it directly
const rows = 3;
const columns = 3;
const board = [];
for (let i = 0; i < rows; i++) {
board[i] = [];
for (let j = 0; j < columns; j++) {
board[i].push("");
}
}
let moveValid = true;
const markBoard = (marker, arrayRow, arrayColumn) => {
if (board[arrayRow][arrayColumn] === "") {
board[arrayRow][arrayColumn] = `${marker}`;
moveValid = true;
} else {
// do nothing
moveValid = false;
}
};
const checkValidMove = () => moveValid;
let allBoardCombos = [];
const getAllBoardCombos = () => {
for (let row of board) {
allBoardCombos.push(row)
}
for (let c = 0; c < board[0].length; c++) {
let col = board.map(function(value) { return value[c];});
allBoardCombos.push(col);
}
allBoardCombos.push([board[0][0], board[1][1], board[2][2]]);
allBoardCombos.push([board[2][0], board[1][1], board[0][2]]);
return allBoardCombos;
}
const resetAllBoardCombos = () => {
allBoardCombos = [];
}
const checkBoard = () => {
// checkAvailableSpaces();
getAllBoardCombos();
let match;
for (combo of allBoardCombos) {
if (combo.every(val => val === combo[0]) && (combo[0] !== "")) {
match = true
break
} else {
match = false
}
}
resetAllBoardCombos();
return match
}
const checkAvailableSpaces = () => {
let boardFull = board.every(row => row.every(cell => cell === "X" || cell === "O")) ? true : false;
return boardFull;
}
const clearBoard = () => {
for (let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
board[i][j] = "";
}
}
}
const getBoard = () => board;
return {
markBoard, checkValidMove, checkBoard, clearBoard, getBoard, checkAvailableSpaces
}
})();
// ____________Players________________
const Players = function() {
const players = [
{
name: "Player One",
marker: "X"
},
{
name: "",
marker: "O"
}
]
function addPlayerNames(input1, input2) {
obj1 = players.findIndex(obj => obj.marker === "X")
players[obj1].name = input1;
obj2 = players.findIndex(obj => obj.marker === "O")
players[obj2].name = input2;
}
const getPlayers = () => players;
return {getPlayers, addPlayerNames};
}();
// __________Game Flow______________
function gameController(){
let activePlayer = Players.getPlayers()[0];
const switchPlayerTurn = () => {
activePlayer = activePlayer === Players.getPlayers()[0] ? Players.getPlayers()[1] : Players.getPlayers()[0];
}
const setToPlayerOne = () => {
activePlayer = Players.getPlayers()[0];
}
const getActivePlayer = () => activePlayer;
function placeMarker(inputRow, inputColumn){
gameBoard.markBoard(getActivePlayer().marker, inputRow, inputColumn)
}
function playRound(inputRow, inputColumn) {
placeMarker(inputRow, inputColumn);
if (gameBoard.checkValidMove() == true) {
// gameBoard.checkBoard();
checkForWinner();
} else {
// do nothing, player has not marked a valid square
}
}
const checkForWinner = () => {
if (gameBoard.checkBoard() === true || gameBoard.checkAvailableSpaces() === true) {
// do nothing, the game is ended
} else {
switchPlayerTurn();
}
}
return {getActivePlayer, playRound, getBoard: gameBoard.getBoard, setToPlayerOne}
};
// __________Display Control______________
function screenController() {
const game = gameController();
const playerDiv = document.querySelector('.player-div');
const boardDiv = document.querySelector('.board-div');
const resetButton = document.querySelector('.reset-banner > button');
const formPopup = document.querySelector('.player-name-form');
const form = document.querySelector('.player-name-form > form')
const playerOneInput = document.querySelector('#player-one');
const playerTwoInput = document.querySelector('#player-two');
formPopup.showModal();
form.addEventListener('submit', function(e) {
e.preventDefault();
Players.addPlayerNames(playerOneInput.value, playerTwoInput.value);
form.reset();
formPopup.close();
updateScreen();
})
const updateScreen = () => {
// clear the board
boardDiv.textContent = "";
let match = gameBoard.checkBoard();
let boardFull = gameBoard.checkAvailableSpaces()
// get the newest version of board and player turn
const board = game.getBoard();
const activePlayer = game.getActivePlayer();
const playerDisplay = () => {
// let match = gameBoard.checkBoard();
if (match === true) {
playerDiv.textContent = `${activePlayer.name} wins!`;
} else if (match === false && boardFull === true) {
playerDiv.textContent = "Tie game!"
} else if (match === false && boardFull === false) {
playerDiv.textContent = `${activePlayer.name}'s turn...`;
}
}
playerDisplay();
const toggleBoard = () => {
if (match === true || boardFull === true) {
boardDiv.classList.add('stop-btn-effects');
} else {
boardDiv.classList.remove('stop-btn-effects');
};
};
toggleBoard();
board.forEach(row => {
row.forEach((cell, index) => {
//anything clickable should be a button
const cellButton = document.createElement("button");
cellButton.classList.add("cell");
// create data attribute to identify the column - this makes it easier to pass into our playRound function
cellButton.dataset.row = board.indexOf(row);
cellButton.dataset.column = index;
cellButton.textContent = cell;
boardDiv.appendChild(cellButton);
})
})
};
function clickHandlerBoard(e) {
const selectedCellRow = e.target.dataset.row;
const selectedCellCol = e.target.dataset.column;
// make sure the columnn is clicked and not the gaps in between
if (!selectedCellCol) return;
game.playRound(selectedCellRow, selectedCellCol);
updateScreen();
}
boardDiv.addEventListener("click", function(e) {
if (gameBoard.checkBoard() === true) {
// do nothing, game has ended
} else {
clickHandlerBoard(e)
}
});
resetButton.addEventListener("click", () => {
gameBoard.clearBoard();
game.setToPlayerOne()
updateScreen();
formPopup.showModal();
});
// initial render
updateScreen();
};
screenController();