My final "Next Steps" game code if anyone's interested


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661

Not a question. Just posting this here if someone wants to look at it. I'm still getting returns echoing in the log sometimes, but I think that's just a bug with codecademy's editor. The rest seems to work fine.

I originally was trying to make a loop somehow with only if/else statements that would keep going if there were multiple ties or multiple incorrect answers entered, but this seems all but impossible, so I'm leaving it at this. Hope someone gets some use out of it


var decideUserChoice = function() {
    var userChoice = prompt("Do you choose rock, paper, scissors, or rope? Use lowercase letters only.");
    return userChoice;
}

var decideComputerChoice = function() {

    var computerChoice = Math.random();
    
    if (computerChoice <= 0.99) {
        computerChoice = "rock";
    } 
    else if (computerChoice <= 0.50) {
        computerChoice = "paper";
    } 
    else if (computerChoice <= 0.75) {
        computerChoice = "scissors";
    }
    else {
        computerChoice = "rope";
    }
    return computerChoice;
}

var compare = function(choice1, choice2) {
    var response;
    var responseRestart;
    if (choice1 === choice2) {
        response = 1;
        return response;
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            response = "Rock smashes scissors. Player wins!";
            return response;
        }
        else if (choice2 === "paper") {
            response = "Paper covers rock. Computer wins!";
            return response;
        }
        else {
            response = "Rope ties up rock. Computer wins!";
            return response;
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            response = "Paper covers rock. User wins!";
            return response;
        }
        else if (choice2 === "scissors") {
            response = "Scissors cut paper. Computer wins!";
            return response;
        }
        else {
            response = "Rope and paper fall in love. Everybody wins!";
            return response;
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            response = "Rock smashes scissors. Computer wins!";
            return response;
        }
        else if (choice2 === "paper") {
            response = "Scissors cut paper. User wins!";
            return response;
        }
        else {
            response = "Scissors need sharpening and can't cut rope. Game rescheduled.";
            return response;
        }
    }
    else if (choice1 === "rope") {
        if (choice2 === "rock") {
            response = "Rope ties up rock. User wins!";
            return response;
        }
        else if (choice2 === "paper") {
            response = "Rope and paper fall in love. Everybody wins!";
            return response;
        }
        else {
            response = "Scissors need sharpening and can't cut rope. Game rescheduled.";
            return response;
        }
    }
    else {
        response = 2;
        return response;
    }
}

var runGame = function() {
    var userChoice = decideUserChoice();
    var computerChoice = decideComputerChoice();
    console.log("User: " + userChoice);
    console.log("Computer: " + computerChoice);
    var answer = compare(userChoice, computerChoice);
    console.log(answer);
    return answer;
}

var outcome = runGame();
if (outcome === 1) {
    console.log("The result is a tie!");
    console.log("Let's try again");
    outcome = runGame();
    if (outcome === 1) {
        console.log("The result is a tie again!");
        console.log("Two ties in a row is bad luck. Sorry, game over.")
    }
    else if (outcome === 2) {
        console.log("You really have to follow the directions. Incorrect Response.");
        console.log("Sorry, you only get two tries. Game over. Nobody wins.")
    }
    else {
        console.log("Game Over");
    }
}
else if (outcome === 2) {
    console.log("You must choose rock, paper, scissors, or rope and use lowercase letters only.");
    console.log("Try again and enter a correct response.");
    outcome = runGame();
    if (outcome === 1) {
        console.log("The result is a tie!");
        console.log("Sorry, only two tries per player. Game over. Nobody wins.")
    }
    else if (outcome === 2) {
        console.log("You really have to follow the directions. Incorrect Response.");
        console.log("Sorry, you only get two tries. Game over. Nobody wins.")
    }
    else {
        console.log("Game Over");
    }
}
else {
    console.log("Game Over");
}


#2

Nice game, also good idea to use the output of the runGame function to decide how to go on.
I just have the impression that the game is a bit unfair:

Chance to get paper: 50%
Chance to get scissors: 25%
Chance to get rock: 24%
Chance to get rope: 1%

Results for rock: 2 losses 1 tie and a win
Results for paper: 1 loss 1 tie and 2 wins
Results for scissors: 1 loss 2 ties and a win
Results for rock: 0 loss 2 ties and 2 wins

So choosing scissors has a 50% chance to win and only a 24% chance to lose.

About the loops well you might go on with the course as the very next thing you'll encounter are loops :wink:
And about the extra output could you elaborate on this one?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.