-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic.js
More file actions
227 lines (188 loc) ยท 7.85 KB
/
Copy pathLogic.js
File metadata and controls
227 lines (188 loc) ยท 7.85 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
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// Logic.js โ Tic-Tac-Toe Game Logic
// Handles: Navigation, Board toggling, Player vs Computer,
// Player vs Friend, Win detection, and Board reset
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// โโ Navigation โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
function Secondpage() {
window.location.href = "Second.html";
}
function HomePage() {
window.location.href = "index.html";
}
// โโ Board Toggling (Mode Selection) โโโโโโโโโโโโโโโโโโโโโโโโโ
// Only run board-related code on the game page (Second.html)
let computerModeBtn = document.querySelector('.computer-mode');
let duoModeBtn = document.querySelector('.duo-mode');
if (computerModeBtn) {
computerModeBtn.addEventListener('click', function () {
document.querySelector('.Board1').style.display = "grid";
document.querySelector('.box').style.display = "flex";
document.querySelector('.Board2').style.display = "none";
document.querySelector('.box2').style.display = "none";
resetGame(); // Reset when switching modes
});
}
if (duoModeBtn) {
duoModeBtn.addEventListener('click', function () {
document.querySelector('.Board1').style.display = "none";
document.querySelector('.box').style.display = "none";
document.querySelector('.Board2').style.display = "grid";
document.querySelector('.box2').style.display = "flex";
resetFriendGame(); // Reset when switching modes
});
}
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// MODE 1: Player vs Computer
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let board = ["", "", "", "", "", "", "", "", ""];
let player = "X";
let computer = "O";
let gameActive = true; // Prevents moves after a win/draw
let empty_spaces = [];
// All possible winning combinations
let Possibilities = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
[0, 4, 8], [2, 4, 6] // Diagonals
];
// Player clicks on a cell (Board1 โ vs Computer)
let board1Cells = document.querySelectorAll('.Board1 .cell');
board1Cells.forEach(cell => {
cell.addEventListener('click', function () {
let position = this.id;
// Only allow move if cell is empty and game is active
if (board[position] === "" && gameActive) {
moveCheck(position, player);
// Computer responds after a short delay
if (gameActive) {
setTimeout(computerMove, 500);
}
}
});
});
// Place a symbol on the board (shared helper for Mode 1)
function moveCheck(position, symbol) {
if (board[position] === "") {
board[position] = symbol;
// Update the correct cell in Board1
document.querySelectorAll('.Board1 .cell')[position].innerText = symbol;
updateEmptySpaces();
let winner = checkWinner();
if (winner) {
gameActive = false;
}
}
}
// Track remaining empty cells
function updateEmptySpaces() {
empty_spaces = [];
for (let i = 0; i < board.length; i++) {
if (board[i] === "") empty_spaces.push(i);
}
}
// Computer picks a random empty cell
function computerMove() {
updateEmptySpaces();
if (empty_spaces.length > 0 && gameActive) {
let randomIndex = Math.floor(Math.random() * empty_spaces.length);
let position = empty_spaces[randomIndex];
moveCheck(position, computer);
}
}
// Check for a winner (Mode 1: vs Computer)
function checkWinner() {
for (let i = 0; i < Possibilities.length; i++) {
let [a, b, c] = Possibilities[i];
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
// Highlight winning cells
let cells = document.querySelectorAll('.Board1 .cell');
[a, b, c].forEach(index => {
cells[index].style.textDecoration = "line-through";
cells[index].style.color = "rgb(10, 255, 226)";
});
document.querySelector(".box").innerText = `${board[a]} is the Winner ๐ฅณ`;
return board[a];
}
}
// Check for draw
if (!board.includes("")) {
document.querySelector(".box").innerText = `It's a Draw! ๐ญ`;
return "Draw";
}
return null;
}
// Reset Mode 1 game state
function resetGame() {
board = ["", "", "", "", "", "", "", "", ""];
gameActive = true;
empty_spaces = [];
document.querySelectorAll('.Board1 .cell').forEach(cell => {
cell.innerText = "";
cell.style.textDecoration = "none";
cell.style.color = "white";
cell.style.pointerEvents = "auto";
});
document.querySelector(".box").innerText = "";
}
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// MODE 2: Player vs Friend
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
let board2 = ["", "", "", "", "", "", "", "", ""];
let currentPlayer = "X";
let friendGameActive = true;
let FriendCells = document.querySelectorAll(".Board2 .cell");
FriendCells.forEach((cell) => {
cell.addEventListener('click', function () {
let cellIndex = this.id;
// Only allow move if cell is empty and game is active
if (board2[cellIndex] !== "" || !friendGameActive)
return;
board2[cellIndex] = currentPlayer;
this.innerText = currentPlayer;
// Check for winner
let winner = checkWinnerFriend();
if (winner) {
document.querySelector(".box2 .result2").innerText = `${winner} is the Winner ๐ฅณ`;
friendGameActive = false;
return;
}
// Check for draw
if (!board2.includes("")) {
document.querySelector(".box2 .result2").innerText = `It's a Draw ๐ญ`;
friendGameActive = false;
return;
}
// Switch player turn
currentPlayer = currentPlayer === "X" ? "O" : "X";
});
});
// Check for a winner (Mode 2: vs Friend)
function checkWinnerFriend() {
for (let i = 0; i < Possibilities.length; i++) {
let [a, b, c] = Possibilities[i];
if (board2[a] && board2[a] === board2[b] && board2[a] === board2[c]) {
// Highlight winning cells
let cells = document.querySelectorAll(".Board2 .cell");
[a, b, c].forEach(index => {
cells[index].style.textDecoration = "line-through";
cells[index].style.color = "rgb(10, 255, 226)";
});
return board2[a];
}
}
return null;
}
// Reset Mode 2 game state
function resetFriendGame() {
board2 = ["", "", "", "", "", "", "", "", ""];
currentPlayer = "X";
friendGameActive = true;
document.querySelectorAll('.Board2 .cell').forEach(cell => {
cell.innerText = "";
cell.style.textDecoration = "none";
cell.style.color = "white";
cell.style.pointerEvents = "auto";
});
document.querySelector(".box2 .result2").innerText = "";
}