-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
146 lines (111 loc) · 4.33 KB
/
main.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
//two player object whose getSign method returns their sign.
const player = (sign) => {
const getSign = () => {
return sign;
}
return {getSign};
};
//module for the game board
const gameBoard = (
()=> {
//arr to store the player markings
let gameArr = ["", "", "",
"", "", "",
"", "", ""];
//create two players for each sign
let playerX = player("x");
let playerO = player("o");
//
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]
];
//selecting all 9 cells in the game board
let cells = document.querySelectorAll(".cell");
//selecting output element
let gameStatus = document.getElementById("gameStatus");
console.log(gameStatus);
//variable to check if game is over or not after each update
let isGameOver = false;
//variable to track the number of marking/update in the game board
let updateCount = 1;
//add event listeners for each cell
cells.forEach((cell, id) => {
cell.addEventListener(("click"), () => {
//when a cell is clicked get its id using getAtrribute
id = cell.getAttribute("id");
id=id.charAt(id.length-1);
//use id as array index for the gameArr, and update signs alternatively beginning with X sign
if (updateCount%2 == 1) {
updateGameBoard(id, playerX.getSign());
}
else {
updateGameBoard(id, playerO.getSign());
}
});
});
//function to draw the game board
const drawGameBoard = () => {
cells.forEach((cell, id) => {
id = cell.getAttribute("id");
id=id.charAt(id.length-1);
cell.innerHTML = `${gameArr[id-1]}`;
});
};
//function to update game for each markings
const updateGameBoard = (id, mark) => {
//if a cell is empty and if game is not over, only then update the cell.
if ((gameArr[id-1] == "") && (!isGameOver)) {
gameArr[id-1] = mark;
//draw the game board again to update the changes in UI
drawGameBoard();
updateCount++;
//after each update in the gameArr check if someone has won the game or not
checkForWin(playerX.getSign());
checkForWin(playerO.getSign());
}
if (updateCount > 9) {
gameStatus.innerHTML = `It's a draw, Reset and try again`;
}
};
//function to check if game is over or not
function checkForWin(player) {
// Loop through each winning combination
for (let i = 0; i < winningCombinations.length; i++) {
// Get the current winning combination
const combination = winningCombinations[i];
// Check if the player has all the positions in the combination
const hasWon = combination.every(index => gameArr[index] === player);
// If the player has won, return true
if (hasWon) {
gameStatus.innerHTML = `Congratulations! Game won by Player ${player}`;
isGameOver = true;
return;
}
}
}
//event listener to the reset button
let resetButton = document.querySelector(".resetButton");
resetButton.addEventListener("click", () => {
reset();
});
//whenever reset is called move all the variables to its initial state
const reset = () => {
updateCount = 1;
isGameOver = false;
gameArr = ["", "", "",
"", "", "",
"", "", ""];
gameStatus.innerHTML = '';
drawGameBoard();
};
return {updateGameBoard, drawGameBoard, reset, checkForWin, playerX,playerO};
}
) ();
gameBoard.drawGameBoard();