Skip to content

Commit

Permalink
Add in function computer move so that machine look choose winning move
Browse files Browse the repository at this point in the history
  • Loading branch information
atchutchi committed May 16, 2023
1 parent 3f291b2 commit 6f23323
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,39 @@ function handleCellClick(event) {

// Adapted from code by lando464 on CodePen: https://codepen.io/lando464/pen/BPGEKO
function computerMove() {
var emptyCells = [];
var random;
var emptyCells = [];
var random;

for (var i = 0; i < board.length; i++) {
if (board[i] == '') {
emptyCells.push(i);
for (var i = 0; i < board.length; i++) {
if (board[i] == '') {
emptyCells.push(i);
}
}

// Check for winning moves
for (var i = 0; i < winningCombinations.length; i++) {
var combination = winningCombinations[i];
var foundWinner = true;
for (var j = 0; j < combination.length; j++) {
if (board[combination[j]] != currentPlayer) {
foundWinner = false;
break;
}
}

random = Math.floor(Math.random() * emptyCells.length);
board[emptyCells[random]] = currentPlayer;
document.getElementById(`cell${emptyCells[random] + 1}`).textContent = currentPlayer;

// Check if computer won
if (checkWin(currentPlayer)) {
playerOScore++;
updateScores();
if (currentRound === 5) {
endGame();
} else {
startNextRound();
if (foundWinner) {
// The computer has a winning move
board[combination[0]] = currentPlayer;
document.getElementById(`cell${combination[0] + 1}`).textContent = currentPlayer;
return;
}
} else {
currentPlayer = "X";
}

// No winning moves found, so select a random empty cell
random = Math.floor(Math.random() * emptyCells.length);
board[emptyCells[random]] = currentPlayer;
document.getElementById(`cell${emptyCells[random] + 1}`).textContent = currentPlayer;
}


// Function to determine whether a player won
function checkWin(player) {
Expand Down

0 comments on commit 6f23323

Please sign in to comment.