-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
260 lines (226 loc) · 8.53 KB
/
Copy pathscript.js
File metadata and controls
260 lines (226 loc) · 8.53 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// --- Game Constants ---
const BOARD_SIZE = 20;
const WIN_LENGTH = 5;
// --- DOM Elements ---
const board = document.getElementById("board");
const results = document.querySelector("#results");
const playAgainBtn = document.querySelector("#play-again");
const turnBg = document.querySelector(".bg");
const menu = document.querySelector(".menu");
const gameContainer = document.querySelector(".game-container");
const pvpBtn = document.getElementById("pvp-btn");
const pvcXBtn = document.getElementById("pvc-x-btn");
const pvcOBtn = document.getElementById("pvc-o-btn");
// --- Game State Variables ---
let turn = "X";
let isGameOver = false;
let gameMode = "pvp";
let playerSymbol = "X";
let cpuSymbol = "O";
let boardState = [];
// --- Game Initialization ---
function createBoard() {
board.innerHTML = "";
board.style.gridTemplateColumns = `repeat(${BOARD_SIZE}, 1fr)`;
board.style.gridTemplateRows = `repeat(${BOARD_SIZE}, 1fr)`;
document.documentElement.style.setProperty('--board-size', BOARD_SIZE);
const totalCells = BOARD_SIZE * BOARD_SIZE;
boardState = Array(totalCells).fill(null);
for (let i = 0; i < totalCells; i++) {
const cell = document.createElement("div");
cell.classList.add("box");
cell.dataset.index = i;
cell.addEventListener("click", handleBoxClick);
board.appendChild(cell);
}
}
function startGame() {
menu.classList.add("hide");
gameContainer.classList.remove("hide");
createBoard();
resetGame();
}
pvpBtn.addEventListener("click", () => { gameMode = "pvp"; startGame(); });
pvcXBtn.addEventListener("click", () => { gameMode = "pvc"; playerSymbol = "X"; cpuSymbol = "O"; startGame(); });
pvcOBtn.addEventListener("click", () => { gameMode = "pvc"; playerSymbol = "O"; cpuSymbol = "X"; startGame(); });
// --- Main Game Logic ---
function handleBoxClick(e) {
const index = parseInt(e.target.dataset.index);
if (isGameOver || boardState[index] !== null) return;
makeMove(index, turn);
if (!isGameOver) {
changeTurn();
if (gameMode === "pvc" && turn === cpuSymbol) {
board.classList.add("board-locked");
setTimeout(cpuMove, 200);
}
}
}
function makeMove(index, symbol) {
if (isGameOver || boardState[index] !== null) return;
boardState[index] = symbol;
board.children[index].innerHTML = symbol;
if (checkWin(index, symbol)) {
isGameOver = true;
results.innerHTML = `${symbol} wins!`;
playAgainBtn.style.display = "inline";
} else if (boardState.every(cell => cell !== null)) {
isGameOver = true;
results.innerHTML = "It's a Draw!";
playAgainBtn.style.display = "inline";
}
}
function changeTurn() {
turn = (turn === "X") ? "O" : "X";
turnBg.style.left = (turn === "X") ? "0" : "85px";
}
// --- Winning Condition Logic ---
function checkWin(index, symbol, requiredLength = WIN_LENGTH) {
const col = index % BOARD_SIZE;
const row = Math.floor(index / BOARD_SIZE);
const directions = [ {x:1, y:0}, {x:0, y:1}, {x:1, y:1}, {x:1, y:-1} ];
for (const dir of directions) {
let count = 1;
for (let i = 1; i < requiredLength; i++) {
const r = row + i * dir.y; const c = col + i * dir.x;
if (isOutOfBounds(r, c) || boardState[r * BOARD_SIZE + c] !== symbol) break;
count++;
}
for (let i = 1; i < requiredLength; i++) {
const r = row - i * dir.y; const c = col - i * dir.x;
if (isOutOfBounds(r, c) || boardState[r * BOARD_SIZE + c] !== symbol) break;
count++;
}
if (count >= requiredLength) {
if (requiredLength === WIN_LENGTH && !board.classList.contains("board-locked")) {
highlightWinningCells(row, col, dir, symbol, requiredLength);
}
return true;
}
}
return false;
}
function isOutOfBounds(row, col) {
return row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE;
}
function highlightWinningCells(row, col, dir, symbol, length) {
board.children[row * BOARD_SIZE + col].classList.add("winning-cell");
for (let i = 1; i < length; i++) {
const r = row + i * dir.y; const c = col + i * dir.x;
if (isOutOfBounds(r, c) || boardState[r * BOARD_SIZE + c] !== symbol) break;
board.children[r * BOARD_SIZE + c].classList.add("winning-cell");
}
for (let i = 1; i < length; i++) {
const r = row - i * dir.y; const c = col - i * dir.x;
if (isOutOfBounds(r, c) || boardState[r * BOARD_SIZE + c] !== symbol) break;
board.children[r * BOARD_SIZE + c].classList.add("winning-cell");
}
}
// --- AI LOGIC (ƯU TIÊN PHÒNG THỦ) ---
function cpuMove() {
if (isGameOver) {
board.classList.remove("board-locked");
return;
}
const move = findBestMove();
if (move !== -1) {
makeMove(move, cpuSymbol);
}
if (!isGameOver) {
changeTurn();
}
board.classList.remove("board-locked");
}
function findBestMove() {
// <<< FIX: Thay đổi toàn bộ thứ tự ưu tiên của AI
// Ưu tiên 1: PHÒNG THỦ TUYỆT ĐỐI - Chặn người chơi thắng ngay lập tức. Đây là điều bắt buộc.
for (let i = 0; i < boardState.length; i++) {
if (boardState[i] === null) {
boardState[i] = playerSymbol;
if (checkWin(i, playerSymbol, WIN_LENGTH)) { // Chặn đường 5
boardState[i] = null;
return i;
}
boardState[i] = null;
}
}
// Ưu tiên 2: TẤN CÔNG - Nếu không phải phòng thủ, hãy tìm cách thắng ngay.
for (let i = 0; i < boardState.length; i++) {
if (boardState[i] === null) {
boardState[i] = cpuSymbol;
if (checkWin(i, cpuSymbol, WIN_LENGTH)) {
boardState[i] = null;
return i;
}
boardState[i] = null;
}
}
// Ưu tiên 3: PHÒNG THỦ SỚM - Chặn các mối đe dọa nguy hiểm (đường 3, 4) của người chơi.
const DANGEROUS_THREAT_LENGTH = 3;
for (let i = 0; i < boardState.length; i++) {
if (boardState[i] === null) {
boardState[i] = playerSymbol;
if (checkWin(i, playerSymbol, DANGEROUS_THREAT_LENGTH)) {
boardState[i] = null;
return i;
}
boardState[i] = null;
}
}
// Ưu tiên 4: CHIẾN THUẬT - Đi vào vị trí gần các quân cờ đã có.
const candidateMoves = findCandidateMoves();
if (candidateMoves.length > 0) {
return candidateMoves[Math.floor(Math.random() * candidateMoves.length)];
}
// Ưu tiên 5: DỰ PHÒNG - Đi ngẫu nhiên nếu không còn lựa chọn nào tốt hơn.
const emptyCells = [];
boardState.forEach((cell, index) => { if (cell === null) emptyCells.push(index); });
if(emptyCells.length > 0) {
return emptyCells[Math.floor(Math.random() * emptyCells.length)];
}
return -1;
}
// Helper cho AI: tìm các ô trống cạnh các ô đã được đánh
function findCandidateMoves() {
const candidates = new Set();
const directions = [
{x: 0, y: 1}, {x: 0, y: -1}, {x: 1, y: 0}, {x: -1, y: 0},
{x: 1, y: 1}, {x: 1, y: -1}, {x: -1, y: 1}, {x: -1, y: -1}
];
for (let i = 0; i < boardState.length; i++) {
if (boardState[i] !== null) {
const row = Math.floor(i / BOARD_SIZE);
const col = i % BOARD_SIZE;
for (const dir of directions) {
const nRow = row + dir.y;
const nCol = col + dir.x;
if (!isOutOfBounds(nRow, nCol)) {
const neighborIndex = nRow * BOARD_SIZE + nCol;
if (boardState[neighborIndex] === null) {
candidates.add(neighborIndex);
}
}
}
}
}
return Array.from(candidates);
}
// --- Reset Logic ---
function resetGame() {
isGameOver = false;
turn = "X";
turnBg.style.left = "0";
results.innerHTML = "";
playAgainBtn.style.display = "none";
board.classList.remove("board-locked");
boardState.fill(null);
for (let i = 0; i < board.children.length; i++) {
board.children[i].innerHTML = "";
board.children[i].classList.remove("winning-cell");
}
if (gameMode === "pvc" && cpuSymbol === "X") {
board.classList.add("board-locked");
setTimeout(cpuMove, 200);
}
}
playAgainBtn.addEventListener("click", resetGame);