mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-10 06:08:34 +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,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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue