-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (118 loc) · 3.44 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
const displayController = (() => {
const renderMessage = (message) => {
document.querySelector("#message").innerHTML = message;
}
return {
renderMessage,
}
})();
const gameBoard = (() => {
let gameboard = ["", "", "", "", "", "", "", "", ""];
const render = () => {
let boardHTML = "";
gameboard.forEach((square, index) => {
boardHTML +=`<div class="square" id="square-${index}">${square}</div>`;
})
document.querySelector("#gameboard").innerHTML = boardHTML;
const squares = document.querySelectorAll(".square");
squares.forEach((square) => {
square.addEventListener("click",Game.handleClick);
})
}
const update = (index, value) => {
gameboard[index] = value;
render();
};
const getGameboard = () => gameboard;
return {
render,
update,
getGameboard
}
})();
const createPlayer = (name, mark) => {
return {
name,
mark
}
}
const Game = (() => {
let players = [];
let currentPlayerIndex;
let gameOver;
const start = () => {
players = [
createPlayer(document.querySelector("#player1").value, "X"),
createPlayer(document.querySelector("#player2").value, "O")
]
currentPlayerIndex = 0;
gameOver = false;
gameBoard.render();
const squares = document.querySelectorAll(".square");
squares.forEach((square) => {
square.addEventListener("click", handleClick);
})
}
const handleClick = (event) => {
if (gameOver) {
return;
}
let index = parseInt(event.target.id.split("-")[1]);
if (gameBoard.getGameboard()[index] !== "") {
displayController.renderMessage("This square is already occupied!");
return;
}
gameBoard.update(index, players[currentPlayerIndex].mark);
if(checkForWin(gameBoard.getGameboard(), players[currentPlayerIndex].mark)){
gameOver = true;
displayController.renderMessage(`${players[currentPlayerIndex].name} wins`)
}else if (checkForTie(gameBoard.getGameboard())){
gameOver = true;
displayController.renderMessage("It's a tie")
}
currentPlayerIndex = currentPlayerIndex === 0 ? 1 : 0;
}
const restart = () => {
for (let i = 0; i < 9; i++){
gameBoard.update(i, "");
}
gameBoard.render();
gameOver = false;
document.querySelector("#message").innerHTML = "";
}
return {
start,
restart,
handleClick
}
})();
function checkForWin(board) {
const winningCombinations = [
[0,1,2],
[3,4,5],
[6,7,8],
[0,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
]
for (let i = 0; i < winningCombinations.length; i++){
const [a, b, c] = winningCombinations[i];
if (board[a] && board[a] === board[b] && board[a] === board[c]){
return true;
}
}
return false;
}
function checkForTie(board) {
return board.every(cell => cell !== "")
}
const restartButton= document.querySelector("#restart-button");
restartButton.addEventListener("click", () => {
Game.restart();
})
const startButton = document.querySelector("#start-button");
startButton.addEventListener("click", () => {
Game.start();
})