ticTacToe problem

Solved !!

hi every one !
I really hope there is someone that can help me with this ! :innocent:

Maybe I’m blind, but I can’t get this to run in my console :exploding_head:
I’m getting this error :

VM1690:4 Uncaught TypeError: Cannot read properties of undefined (reading ‘length’)
at getBoardString (:4:35)
at getUserInput (:15:31)
at makeAMove (:41:24)
at ticTacToe (:112:19)
at :1:1

function getBoardString(gameBoard) {
    let boardString = '';

    for (let i = 0; i < gameBoard.length; i += 1) {
        boardString += `${gameBoard[i] ?? i+1}`;
        if (i % 3 === 2) {
            boardString += '\n';
        }
    }

    return boardString;
}

function getUserInput(nextPlayerSymbol, gameBoard) {
    return +prompt(`Board:\n${getBoardString(gameBoard)} enter ${nextPlayerSymbol}'s next move (1-9):`);
    }  
    




function isMoveValid(move, gameBoard){
    const boardIndex = move -1;
    //move is a number
    // move is between 1 and 9 (inklusive)
    // gameBoard does not contain a symbol at the place of the move

    return (
        typeof move === 'number' &&
        move >= 1 && move <= 9 &&
        gameBoard[boardIndex] === null
    );
    
}

function makeAMove(gameBoard, nextPlayerSymbol){
            // clone the game bord before placing moves in it
            const newGameBoard = [...gameBoard];
            let move = null;
        do {
            let move = getUserInput(nextPlayerSymbol);
        }
            while ( !isMoveValid(move, gameBoard) );
            let boardIndex = move -1;
            newGameBoard[boardIndex] = nextPlayerSymbol;
            // make the move
        return newGameBoard;

}

function hasLastMoverWon(lstMove, 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] === lastMove &&
            gameBoard[i1] === gameBoard[i2] &&
            gameBoard[i1] === gameBoard[i3]
     ) {
     return true;
        } 
      }
    return false;      
      }


function isGameOver(gameBoard, currentPlayerSymbol) {
        // 1. check if there is a winner
        if (hasLastMoverWon(lastMove, gameBoard)) {
            // Write a message : last mover has won the game
            alert(`Gratulerer!, ${currentPlayerSymbol} Jøss Du Vant!`);
            return true;
        }
        // 2. check if the bord is full
        //function isBoardFull(board){
           // for (let element of board){
               // if (element === null){
                    //return false; bord
                

            
       //return true;
       // også løsning: 
       if (gameBoard.every(element => element !== null)) { alert(`uavgjort! Spill over!`); return true;}
        

        
        //if (draw) { alert('Uavgjort!') return true;}
        


        // 3. Return winner/draw OR not game over/ game in progress 
    
}

function ticTacToe() {
    //state space
     
    let gameBoard =  new Array(9).fill(null);
    let currentPlayerSymbol = null;
    
    // computations 
    do { 
        currentPlayerSymbol = currentPlayerSymbol === 'x' ? 'o' : 'x';
      gameBoard = makeAMove(gameBoard, currentPlayerSymbol);
    }
    while ( !isGameOver(gameBoard, currentPlayerSymbol) );

        
    
    

    // return value
    // return udefined    




        }  ```

In the ticTacToe function you misspelled gameBoard I marked it with ***

do { 
        currentPlayerSymbol = currentPlayerSymbol === 'x' ? 'o' : 'x';
      **gameBord** = makeAMove(gameBoard, currentPlayerSymbol);
    }
1 Like

thanks a lot… I can se clearly now the rain is gone :slight_smile:
I se a lot of “bord” miss spelling … I have to practise my English also :ok_hand:

got a bit further . the problem and error is updated :rofl: I’m going looney

It’s kind of obvious if you look at the stack trace of the error. Read it from top to bottom and check your corresponding rows and columns.

VM1690:4 Uncaught TypeError: Cannot read properties of undefined (reading ‘length’)
at getBoardString (:4:35)
at getUserInput (:15:31)
at makeAMove (:41:24)
at ticTacToe (:112:19)
at :1:1
  • getBoardString line 4: length is undefined. So gameBoard is not an array. Let’s see why.
  • getUserInput calls getBoardString on line 15. Everything is gravy so far, you see getUserInput gets gameBoard as a parameter and passes it forward.
  • Then comes makeAMove that calls getUserInput. Hm… I can see multiple problems. The immediate answer to your question is that getUserInput inside the do-while loop requires 2 arguments, but you only passed one. gameBoard is missing. Problem solved.

The stack trace gives you all data you need.

Except for semantic mistakes. Notice that you shallow copied gameBoard in the first instruction of makeAMove. So in reality, from here onwards, I assume you’ll want to use newGameBoard, not gameBoard, so I’d put newGameBoard as the second argument of makeAMove.

2 Likes

thanks a lot!
i got a bit frustrated, when tried to fix it .
it seemed to get worse and worse.
and I kind of lost it …
and now I see how the stac trace works :slight_smile:
taking a step back :laughing:

1 Like