-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
172 lines (166 loc) · 5.9 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
167
168
169
170
171
172
const gameBoard = function () {
const buttonList = document.querySelectorAll(".playButton");
const rows = 3;
const columns = 3;
const board = [];
let count = 0;
for (let i = 0; i < rows; i++) {
board[i] = [];
for (let j = 0; j < columns; j++) {
board[i][j] = buttonList[count];
count++;
}
}
return board;
};
function gameController(playerOneName, playerTwoName, screen) {
let count = 0;
const board = gameBoard;
const players = [
{
name: playerOneName,
symbol: "X"
},
{
name: playerTwoName,
symbol: "O"
}
];
let activePlayer = players[0];
const switchPlayerTurn = () => {
activePlayer = activePlayer === players[0] ? players[1] : players[0];
}
const getActivePlayer = () => activePlayer;
function resetActivePlayer() {
activePlayer = players[0];
}
function checkScore(board, playerSymbol, playerName, screen) {
let winSquares = [];
for (let i = 0; i < 3; i++) {
if (board[i][0].textContent == playerSymbol && board[i][1].textContent == playerSymbol && board[i][2].textContent == playerSymbol) {
for (let j = 0; j < 3; j++) {
winSquares.push(board[i][j]);
}
return winSquares;
}
}
for (let j = 0; j < 3; j++) {
if (board[0][j].textContent == playerSymbol && board[1][j].textContent == playerSymbol && board[2][j].textContent == playerSymbol) {
for (let i = 0; i < 3; i++) {
winSquares.push(board[i][j]);
}
return winSquares;
}
}
if (board[0][0].textContent === playerSymbol && board[1][1].textContent === playerSymbol && board[2][2].textContent === playerSymbol) {
winSquares.push(board[0][0], board[1][1], board[2][2]);
return winSquares;
}
if (board[0][2].textContent === playerSymbol && board[1][1].textContent === playerSymbol && board[2][0].textContent === playerSymbol) {
winSquares.push(board[0][2], board[1][1], board[2][0]);
return winSquares;
}
}
function checkForDraw(board) {
let count = 0;
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j].textContent != "") count++;
}
}
if (count >= 9) return true;
}
function playRound(square, screen) {
square.textContent = activePlayer.symbol;
if (checkScore(board(), activePlayer.symbol, activePlayer.name, screen)) {
return checkScore(board(), activePlayer.symbol, activePlayer.name, screen);
}
else if (checkForDraw(board())) {
return 2;
}
else {
switchPlayerTurn();
return 3;
}
}
function resetGame(board) {
resetActivePlayer();
console.log("resetting....")
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
board[i][j].textContent = "";
}
}
}
return {
getActivePlayer,
playRound,
checkScore,
resetGame
}
};
const displayController = (function () {
const messageScreen = document.createElement("p");
const startButton = document.createElement("button");
startButton.classList.add("startBtn");
startButton.textContent = "Start Game";
const gridContainer = document.querySelector(".gridContainer");
for (let i = 0; i < 9; i++) {
const playButton = document.createElement("button");
playButton.classList.add("playButton");
playButton.id = i + 1;
gridContainer.appendChild(playButton);
playButton.addEventListener("click", playClickHandler);
}
document.body.insertBefore(messageScreen, gridContainer);
document.body.insertBefore(startButton, gridContainer);
const board = gameBoard;
const game = gameController("Player One", "Player Two", messageScreen);
playButtons = document.querySelectorAll(".playButton");
messageScreen.textContent = `This is Tic-Tac-Toe! ${game.getActivePlayer().name}, you go first!`;
startButton.addEventListener("click", startClickHandler);
function winningSquares(list) {
list.forEach(element => {
element.classList.add("winningSquare");
});
playButtons.forEach(element => {
element.style.pointerEvents = "none";
})
}
function resetSquares() {
playButtons.forEach(element => {
element.classList.remove("winningSquare");
element.style.pointerEvents = "auto";
})
}
function gameRoundUI(gameState) {
switch (gameState) {
case 2:
messageScreen.textContent = "It's a draw!! Press Start to play again!";
playButtons.forEach(element => {
element.style.pointerEvents = "none";
})
break;
case 3:
messageScreen.textContent = `${game.getActivePlayer().name} it's your turn!`;
break;
default:
messageScreen.textContent = `${game.getActivePlayer().name} wins! Press Start to play again!`;
winningSquares(gameState);
break;
}
}
function startClickHandler() {
game.resetGame(board());
resetSquares();
messageScreen.textContent = `${game.getActivePlayer().name} it's your turn!`;
}
function playClickHandler(e) {
const selectedSquare = e.target;
if (selectedSquare.textContent != "") {
messageScreen.textContent = `This square is taken! ${game.getActivePlayer().name} choose another one!!`
return;
}
gameRoundUI(game.playRound(selectedSquare, messageScreen));
}
})();