1
0
Fork 0
mirror of https://codeberg.org/ashley/poke.git synced 2024-11-17 02:34:38 -05:00

make ttc way harder

This commit is contained in:
Ashley 2024-01-07 11:32:20 +00:00
parent a8f96ab8b6
commit 49e8c6882a

View file

@ -391,36 +391,76 @@ 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 emptyCells = board.reduce((acc, value, index) => { const winningMove = findWinningMove();
if (value === "") { const blockingMove = findBlockingMove();
acc.push(index);
}
return acc;
}, []);
if (emptyCells.length > 0) { if (winningMove !== null) {
const randomIndex = Math.floor(Math.random() * emptyCells.length); board[winningMove] = currentPlayer;
const computerMove = emptyCells[randomIndex]; } else if (blockingMove !== null) {
board[computerMove] = currentPlayer; board[blockingMove] = currentPlayer;
renderBoard(); } else {
const winner = checkWinner(); // Randomly choose an empty cell for the computer's move
if (winner) { const emptyCells = board.reduce((acc, value, index) => {
messageElement.textContent = `Player ${winner} won!!!!!! woaah`; if (value === "") {
} else if (checkDraw()) { acc.push(index);
messageElement.textContent = "It's a draw! oh welp >~<"; }
} else { return acc;
currentPlayer = currentPlayer === "X" ? "O" : "X"; }, []);
messageElement.textContent = `Player ${currentPlayer}'s turn :3`;
if (emptyCells.length > 0) {
const randomIndex = Math.floor(Math.random() * emptyCells.length);
const computerMove = emptyCells[randomIndex];
board[computerMove] = currentPlayer;
} }
} }
renderBoard();
const winner = checkWinner();
if (winner) {
messageElement.textContent = `Player ${winner} won!!!!!! woaah`;
} else if (checkDraw()) {
messageElement.textContent = "It's a draw! oh welp >~<";
} else {
currentPlayer = currentPlayer === "X" ? "O" : "X";
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
} }
} }