samedi 28 août 2021

Rock Paper Scissors does an extra loop before return, not sure how to fix this

If you scroll down to my Game loop comment, I have my function game(). I want my game to stop looping when either the playerPoints or computerPoints reaches 5. Both of them have a separate return in the if else statements. The problem for me is that instead of adding the final point and then returning the "you win" or "you lose" statements, it prompts the user one more time, and then returns the "You win" or "you lose". It doesn't interfere with the points, however it's a small bug I still want to fix.

When I review my code, I know my alert(playRound) is after my gameContinue declaration and I wonder if it's placement is the reason it is happening again and then returning my result. If that is the problem, how can I continue my game loop until someone reaches five points? If that isn't the problem, then could I just get a tip on what method I should try instead? Thank you.

let playerPoints = 0;
let computerPoints = 0;


// Computer Selection
function computerPlay() {

    const compAnswer = options[Math.floor(Math.random() * options.length)];
    return compAnswer;
}
// One round of the game results

function playRound(playerSelection, computerSelection) {

    let rock = options[0];
    let paper = options[1];
    let scissors = options[2];
    let youWin = "You Win! ";
    let youLose = "You Lose! ";
    let rockWin = "Rock beats scissors.";
    let scissorWin = "Scissors beats paper.";
    let paperWin = "Paper beats rock.";
    let tie = "It's a tie, no points added."
    if (playerSelection === computerSelection) {
        console.log(playerPoints, computerPoints);
        return tie;
    } else if (playerSelection === rock && computerSelection === scissors) {
        playerPoints = playerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youWin + rockWin;

    } else if (playerSelection === rock && computerSelection == paper) {
        computerPoints = computerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youLose + paperWin;

    } else if (playerSelection === scissors && computerSelection === rock) {
        computerPoints = computerPoints + 1;
        console.log(playerPoints, computerPoints)
        return youLose + rockWin;
    } else if (playerSelection === scissors && computerSelection === paper) {
        playerPoints = playerPoints + 1;
        console.log(playerPoints, computerPoints)
        return youWin + scissorWin;
    } else if (playerSelection === paper && computerSelection === rock) {
        playerPoints = playerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youWin + paperWin;
    } else if (playerSelection === paper && computerSelection === scissors) {
        computerPoints = computerPoints + 1;
        console.log(playerPoints, computerPoints);
        return youLose + scissorWin;
    } else {
        console.log(playerPoints, computerPoints);
        return "I'm sorry please try another answer"
    }


};
//Game loop
function game() {

    let gameContinue = true;
    while (gameContinue) {
        const computerSelection = computerPlay();
        const playerSelection = prompt("Choose your weapon");

        if (playerPoints < 5 && computerPoints < 5) {
            gameContinue = true;
            alert(playRound(playerSelection, computerSelection));
        } else if (playerPoints === 5) {
            gameContinue = false;
            console.log("You win");
        } else if (computerPoints === 5) {
            gameContinue = false;
            console.log("You Lose")
        }
    }

};

game();

Aucun commentaire:

Enregistrer un commentaire