tictactoe program keeps looping until draw even if it's won

50 views Asked by At

It keeps asking for input even though it's already won until you fill all the empty spaces and at that moment it says you drew.

function getboardstring(gameBoard) {
  let string = ``;
  for (let i = 0; i < gameBoard.length; i++) {
    i % 3 === 0 ? string += `\n` : ``;
    gameBoard[i] === null ? string += i : string += gameBoard[i];
  }
  return string;
}

function getUserInput(nextPlayerSymbol, gameBoard) {
  return +prompt(`${getboardstring(gameBoard)}\n dove vuoi posizionare la ${nextPlayerSymbol}?`);
}

function isMoveValid(move, gameBoard) {
  if (gameBoard[move] === null) {
    return true;
  } else {
    return false;
  }
}

function makeAMove(gameBoard, nextPlayerSymbol) {
  // clone the game board before placing moves in it
  let newGameBoard = [...gameBoard];
  let move = 0;
  do {
    move = getUserInput(nextPlayerSymbol, gameBoard);
  } while (!isMoveValid(move, gameBoard));
  newGameBoard[move] = nextPlayerSymbol;
  return newGameBoard;
}

function hasLastMoverWon(currentPlayerSymbol, gameBoard) {
  let winnerCombos = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
  ];
  for (let [i1, i2, i3] of winnerCombos) {
    if (gameBoard[i1] === currentPlayerSymbol &&
      gameBoard[i2] === gameBoard[i1] &&
      gameBoard[i3] === gameBoard[i1]) {
      return true;
    }
  }
  return false;
}

function isGameOver(gameBoard, currentPlayerSymbol) {
  // 1. check if there is a winner 
  if (hasLastMoverWon(gameBoard, currentPlayerSymbol) === true) {
    alert(`${currentPlayerSymbol} has won the game!`);
    return true;
  }
  // 2. check if the board is full
  else if (gameBoard.includes(null) === false) {
    alert(`game ended in a draw`);
    return true;
  }
  return false;
}

function ticTacToe() {
  let gameBoard = new Array(9).fill(null)
  let currentPlayerSymbol = null;
  do {
    currentPlayerSymbol = currentPlayerSymbol === `X` ? `O` : `X`;
    gameBoard = makeAMove(gameBoard, currentPlayerSymbol);
  } while (!isGameOver(gameBoard, currentPlayerSymbol));
}
<!-- HTML here; use the snippet edit link -->

1

There are 1 answers

0
Armali On

The parameter order of function hasLastMoverWon was wrong. It works when corrected:

function getboardstring(gameBoard) {
let string = ``;
for(let i = 0; i < gameBoard.length; i++) {
    i % 3 === 0 ? string += `\n`: ``;
    gameBoard[i] === null ? string += i : string += gameBoard[i];
}
return string;
}
function getUserInput(nextPlayerSymbol, gameBoard) {
return prompt(`${getboardstring(gameBoard)}\n dove vuoi posizionare la ${nextPlayerSymbol}?`);
}

function isMoveValid(move, gameBoard) {
if (gameBoard[move] === null) {
    return true;
} else {
    return false;
}
}

function makeAMove(gameBoard, nextPlayerSymbol) {
// clone the game board before placing moves in it
let newGameBoard = [...gameBoard];
let move = 0;
do {
    move = getUserInput(nextPlayerSymbol, gameBoard);
    if (move == null) throw move
} while (!isMoveValid(move, gameBoard));
newGameBoard[move] = nextPlayerSymbol;
return newGameBoard;
}

function hasLastMoverWon(gameBoard, currentPlayerSymbol) {
let winnerCombos = [
    [0, 1, 2], 
    [3, 4, 5], 
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7], 
    [2, 5, 8],
    [0, 4, 8], 
    [2, 4, 6]
];
for (let [i1, i2, i3] of winnerCombos) {
    if (gameBoard[i1] === currentPlayerSymbol &&
        gameBoard[i2] === gameBoard[i1] &&
        gameBoard[i3] === gameBoard[i1]) {
        return true;
    }       
}
return false;
}

function isGameOver(gameBoard, currentPlayerSymbol) {
// 1. check if there is a winner 
if (hasLastMoverWon(gameBoard, currentPlayerSymbol) === true) {
    alert(`${currentPlayerSymbol} has won the game!`);
    return true;
}
// 2. check if the board is full
else if (gameBoard.includes(null) === false) {
    alert(`game ended in a draw`);
    return true;
}
return false;
}

function ticTacToe() {
let gameBoard = new Array(9).fill(null)
let currentPlayerSymbol = null;
do {
    currentPlayerSymbol = currentPlayerSymbol === `X` ? `O` : `X`;
    gameBoard = makeAMove(gameBoard, currentPlayerSymbol);
} while (!isGameOver(gameBoard, currentPlayerSymbol));
}

try { ticTacToe() } catch (e) { if (e) throw e }