0

I'm not sure if I can ask this question but I have a logic error that I cannot pin point. After the player has landed on a square they should be offered to buy the square. What's currently happening is player 1 is being offered to buy player 2's square. player 2 is working fine. The console.log in movePiece is outputting the correct player. Wondering if anyone can see the problem?

/**
 * Moves players pieces around the board. 
 */
function movePiece() {
    let diceRoll = rollDice();
    if (currentPlayer === players[0]) {
        currentPosition1 += diceRoll;
        if (currentPosition1 >= board.length) {
            currentPosition1 -= board.length;
        }
        let currentSquare = squares[currentPosition1];
        currentSquare.appendChild(pieceOne);
        message.innerHTML = `${players[0]} you are on ${pieceOne.parentElement.getAttribute('data-type')} `;
        currentPlayer = players[0];
        console.log(currentPlayer);
        checkIfOwned();
        updatePeople();
        currentPlayer = players[1];
    } else {
        currentPosition2 += diceRoll;
        if (currentPosition2 >= board.length) {
            currentPosition2 -= board.length;
        }
        let currentSquare = squares[currentPosition2];
        currentSquare.appendChild(pieceTwo);
        message.innerHTML = `${players[1]} you are on ${pieceTwo.parentElement.getAttribute('data-type')}`;
        currentPlayer = players[1];
        checkIfOwned();
        updatePeople();

        currentPlayer = players[0];
    }
}

async function buySquare(currentPlayer) {
    await sleep(4000);
    if (currentPlayer === players[0]) {
        message.innerHTML = `<p>Would you like to buy ${pieceOne.parentElement.getAttribute('data-type')} for 5 people?`;
        let input = await getUserInput();
        if (input === 'y') {
            player1Owned.push(pieceOne.parentElement.getAttribute('data-type'));
        } else {
            console.log('you have not bought this square');

        }
    } else {
        message.innerHTML = `<p>Would you like to buy ${pieceTwo.parentElement.getAttribute('data-type')} for 5 people?`;
        let input = await getUserInput();
        if (input === 'y') {
            player2Owned.push(pieceTwo.parentElement.getAttribute('data-type'));
        } else {
            console.log('you have not bought this square');

        }
    }
}

function checkIfOwned() {
    if (player1Owned.includes(pieceOne.parentElement.getAttribute('data-type'))) {
        squareOwned = true;
    } else {
        buySquare();
    }
}

NovaBrownie
  • 117
  • 6
  • 2
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David May 04 '23 at 18:45
  • 2
    At a glance... `buySquare` is expecting a parameter, but you're not passing it one. – David May 04 '23 at 18:47
  • That was it thanks, ill look into a debugger! – NovaBrownie May 04 '23 at 18:58
  • You could eliminate a lot of the repeated code if you stored all the info about a player in an object or class. Example: `let players[0] = {name: "Something", position: 0, owns: [whatever]...};` – 001 May 04 '23 at 19:05

0 Answers0