-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
150 lines (132 loc) · 4.59 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
// Declare game variables
let board = [];
let currentPlayer = "X";
let gridSize = 3;
let winStreak = 3;
let movesMade = 0;
let gameOver = false;
let playerXIcon = "X";
let playerOIcon = "O";
function initializeGame() {
// Get user-defined grid size and win streak
gridSize = parseInt(document.getElementById("grid-size").value);
winStreak = parseInt(document.getElementById("win-streak").value);
// Get selected icons for players
playerXIcon = document.getElementById("player-x-icon").value;
playerOIcon = document.getElementById("player-o-icon").value;
// Validation: Grid size must be between 3 and 10
if (gridSize < 3 || gridSize > 10) {
alert("Grid size must be between 3 and 10.");
return;
}
// Validation: Win streak must be between 3 and gridSize
if (winStreak < 3 || winStreak > gridSize) {
alert(`Win streak must be between 3 and ${gridSize}.`);
return;
}
// Reset moves and game state
movesMade = 0;
gameOver = false;
currentPlayer = "X";
board = Array.from({ length: gridSize }, () => Array(gridSize).fill(""));
// Clear and setup the game board dynamically
const gameDiv = document.getElementById("game");
gameDiv.innerHTML = ""; // Clear previous board
gameDiv.classList.add("board");
gameDiv.style.gridTemplateColumns = `repeat(${gridSize}, 60px)`; // Set grid size
// Show the game board and reset button
gameDiv.removeAttribute("hidden");
document.getElementById("reset").removeAttribute("hidden");
// Create the board cells based on grid size
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
const cell = document.createElement("div");
cell.classList.add("cell");
cell.setAttribute("data-row", i);
cell.setAttribute("data-col", j);
cell.addEventListener("click", handleCellClick); // Add click event listener
gameDiv.appendChild(cell); // Add cell to the game board
}
}
// Update status to show current player's turn
document.getElementById(
"status"
).textContent = `Player ${currentPlayer}'s turn (${
currentPlayer === "X" ? playerXIcon : playerOIcon
})`;
}
function handleCellClick(event) {
if (gameOver) return; // Do nothing if game is over
// Get the clicked cell's row and column
const row = event.target.getAttribute("data-row");
const col = event.target.getAttribute("data-col");
// Check if the cell is empty before marking
if (board[row][col] === "") {
board[row][col] = currentPlayer; // Mark the board
event.target.textContent =
currentPlayer === "X" ? playerXIcon : playerOIcon; // Update UI
movesMade++; // Increment move count
// Check if the current move results in a win or a draw
if (checkWin(parseInt(row), parseInt(col))) {
document.getElementById(
"status"
).textContent = `Player ${currentPlayer} wins!`;
gameOver = true; // End game if there's a winner
} else if (movesMade === gridSize * gridSize) {
document.getElementById("status").textContent = `It's a draw!`;
gameOver = true; // Declare draw if all cells are filled
} else {
// Switch player for next turn
currentPlayer = currentPlayer === "X" ? "O" : "X";
document.getElementById(
"status"
).textContent = `Player ${currentPlayer}'s turn (${
currentPlayer === "X" ? playerXIcon : playerOIcon
})`;
}
}
}
function checkWin(row, col) {
return (
checkDirection(row, col, 0, 1) || // Horizontal check
checkDirection(row, col, 1, 0) || // Vertical check
checkDirection(row, col, 1, 1) || // Diagonal (down-right) check
checkDirection(row, col, 1, -1)
); // Diagonal (up-right) check
}
function checkDirection(row, col, rowIncrement, colIncrement) {
let count = 1; // Count the current cell
// Check in the positive direction (e.g., right, down-right)
for (let i = 1; i < winStreak; i++) {
const newRow = row + i * rowIncrement;
const newCol = col + i * colIncrement;
if (
newRow >= 0 &&
newRow < gridSize &&
newCol >= 0 &&
newCol < gridSize &&
board[newRow][newCol] === currentPlayer
) {
count++;
} else {
break; // Stop
}
}
// Check in the negative direction (e.g., left, up-left)
for (let i = 1; i < winStreak; i++) {
const newRow = row - i * rowIncrement;
const newCol = col - i * colIncrement;
if (
newRow >= 0 &&
newRow < gridSize &&
newCol >= 0 &&
newCol < gridSize &&
board[newRow][newCol] === currentPlayer
) {
count++;
} else {
break; // Stop
}
}
return count >= winStreak; // Return true if win streak is reached
}