I'm working on the Tic Tac Toe project for TheOdinProject. I'm in the process of defining the game logic, currently just making it work in console before I add my UI elements.

everything Is largely working as intended but the while loop I'm using doesn't seem to be iterating the amount of times I believe it should.

the code for the game controller is as follows:

const gameController = (() => { gameRound = 0; turnCount = 0; gameOver = false; player1 = playerController.getPlayers()[0]; player2 = playerController.getPlayers()[1]; function getRandomInt () { return Math.floor(Math.random() * 3); } const playTenRounds = () => { while (!gameOver){ do { placeMarker = gameBoard.placePlayerMark(getRandomInt(), getRandomInt() , playerController.getCurrentPlayer());} while (placeMarker === false); console.table(gameBoard.getBoard()); turnCount++; console.log(turnCount); if (turnCount > 5 && turnCount < 9) { if (gameBoard.checkForWinner(gameBoard.getBoard(), playerController.getCurrentPlayer())){ gameRound++; playerController.getCurrentPlayer().score++; console.log(playerController.getCurrentPlayer().name + " is the winner! Their score is: " + playerController.getCurrentPlayer().score); turnCount = 0; gameBoard.resetBoard(); }; } else if (turnCount == 9) { if (gameBoard.checkForWinner(gameBoard.getBoard(), playerController.getCurrentPlayer())){ gameRound++; playerController.getCurrentPlayer().score++; console.log(playerController.getCurrentPlayer().name + " is the winner! Their score is " + playerController.getCurrentPlayer().score); turnCount = 0; gameBoard.resetBoard(); } else { console.log("Its a draw!" + " Player1 score: " + player1.score + " Player2 score: " + player2.score); gameRound++; turnCount = 0; gameBoard.resetBoard(); }; }; playerController.changeCurrentPlayer(); console.log(gameRound); if (gameRound == 10) { gameOver = true; }; }; }; return {playTenRounds}; })();

the while loop should iterate through until the 2 computers have played 10 rounds correct? instead it is doing 1 loop then ending after it finds a winner or draw. It's not absolutely necessary I get it to do this I just wanna know why it isn't iterating 10 times lol.

user32502603's user avatar

New contributor

user32502603 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.