mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-22 19:37:48 +01:00
make ttc way harder
This commit is contained in:
parent
a8f96ab8b6
commit
49e8c6882a
1 changed files with 62 additions and 22 deletions
|
@ -391,14 +391,23 @@ function resetGame() {
|
||||||
currentPlayer = currentPlayer === "X" ? "O" : "X";
|
currentPlayer = currentPlayer === "X" ? "O" : "X";
|
||||||
messageElement.textContent = `Player ${currentPlayer}'s turn :3`;
|
messageElement.textContent = `Player ${currentPlayer}'s turn :3`;
|
||||||
if (currentPlayer === "O") {
|
if (currentPlayer === "O") {
|
||||||
setTimeout(makeComputerMove, 1000); // AI waits for 1 second
|
setTimeout(makeComputerMove, 500); // AI waits for 0.5 second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeComputerMove() {
|
function makeComputerMove() {
|
||||||
// Simple AI: Randomly choose an empty cell for the computer's move
|
// Look for a winning move, then look to block player, otherwise, choose a random move
|
||||||
|
const winningMove = findWinningMove();
|
||||||
|
const blockingMove = findBlockingMove();
|
||||||
|
|
||||||
|
if (winningMove !== null) {
|
||||||
|
board[winningMove] = currentPlayer;
|
||||||
|
} else if (blockingMove !== null) {
|
||||||
|
board[blockingMove] = currentPlayer;
|
||||||
|
} else {
|
||||||
|
// Randomly choose an empty cell for the computer's move
|
||||||
const emptyCells = board.reduce((acc, value, index) => {
|
const emptyCells = board.reduce((acc, value, index) => {
|
||||||
if (value === "") {
|
if (value === "") {
|
||||||
acc.push(index);
|
acc.push(index);
|
||||||
|
@ -410,6 +419,9 @@ function resetGame() {
|
||||||
const randomIndex = Math.floor(Math.random() * emptyCells.length);
|
const randomIndex = Math.floor(Math.random() * emptyCells.length);
|
||||||
const computerMove = emptyCells[randomIndex];
|
const computerMove = emptyCells[randomIndex];
|
||||||
board[computerMove] = currentPlayer;
|
board[computerMove] = currentPlayer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
renderBoard();
|
renderBoard();
|
||||||
const winner = checkWinner();
|
const winner = checkWinner();
|
||||||
if (winner) {
|
if (winner) {
|
||||||
|
@ -421,6 +433,34 @@ function resetGame() {
|
||||||
messageElement.textContent = `Player ${currentPlayer}'s turn :3`;
|
messageElement.textContent = `Player ${currentPlayer}'s turn :3`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findWinningMove() {
|
||||||
|
for (let i = 0; i < board.length; i++) {
|
||||||
|
if (board[i] === "") {
|
||||||
|
board[i] = currentPlayer;
|
||||||
|
if (checkWinner() === currentPlayer) {
|
||||||
|
board[i] = ""; // Reset the move
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
board[i] = ""; // Reset the move
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findBlockingMove() {
|
||||||
|
const opponent = currentPlayer === "X" ? "O" : "X";
|
||||||
|
for (let i = 0; i < board.length; i++) {
|
||||||
|
if (board[i] === "") {
|
||||||
|
board[i] = opponent;
|
||||||
|
if (checkWinner() === opponent) {
|
||||||
|
board[i] = ""; // Reset the move
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
board[i] = ""; // Reset the move
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderBoard() {
|
function renderBoard() {
|
||||||
|
@ -442,7 +482,7 @@ function resetGame() {
|
||||||
|
|
||||||
// If AI is the starting player, make the first move
|
// If AI is the starting player, make the first move
|
||||||
if (currentPlayer === "O") {
|
if (currentPlayer === "O") {
|
||||||
setTimeout(makeComputerMove, 1000); // AI waits for 1 second
|
setTimeout(makeComputerMove, 500); // AI waits for 0.5 second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue