jasonluxie / 2-1-mp-rock-paper-scissors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2-1-mp-rock-paper-scissors

Description

Rudimentary rock, paper, scissors game which accepts R, S, and P inputs from user and compares it to values of computer to decides if user won, lost, or tied. Wins, losses, and ties are tracked and displayed after each comparison.

Deployed Page:

https://jasonluxie.github.io/2-1-mp-rock-paper-scissors/

Notes

  • At the moment, the game only recieves R, S, and P as inputs and terminates for any other input.
  • The game will terminate if you change tabs, as it relies on prompts, alerts, and confirmation windows to work.

Images of Functionality

User input is received via prompt box Receiving Input

Result is shown and tallied Results and Scores

Play again confirmation Play Again Confirmation

Exit message displays Loop Exit

Code used and thought process

Variables

userVar // user input
compVar // computer input
winVar // Tracks number of wins
loseVar // Tracks number of Losses
tieVar // Tracks number of ties
optionVar = {R: {win: S, lose: P}, S: {win: P, lose: R}, P {win: R, lose: S}}; //Object with win/lose comparisons
gameRunning // 0 for game loop, 1 for game exit. 

Recieving User Input

Collect Information from user and assign user input to userVar

    let userVar = prompt("Would you like to play Rock(R), Paper(P), or Scissors(S)? Please type R for rock, P for paper, and S for scissors", default);

For more user inputs, take user input and process it such that rock/r/ROCK/etc. = R, etc.

Generating Computer Value

For compVar Generate number from 1 to 3; 1 = R, 2 = S, 3 = P

//Generates computer hand
let compHand = ["R", "P", "S"];
//Generated whole number 0,...,max
function randomInt(max) {
    return Math.floor(Math.random() * max);
}
//Shows user what computer chose
alert("Computer chose " + compVar);
//Selects number 0, 1, 2
let compVar = compHand[randomInt(3)];
const optionVar = {
    R: { win: "S", lose: "P" },
    S: { win: "P", lose: "R" },
    P: { win: "R", lose: "S" },
};

Comparing userVar and compVar and displaying information about game state.

Compare information from user to information generated by computer Display results of comparison and show number of W/L/T. If Win, winVar++; else if lose, loseVar++, else tie++.

if (optionVar[userVar].win == compVar) {
    winVar++;
    alert(
        "You win! " +
            "Your wins: " +
            winVar +
            ", your losses: " +
            loseVar +
            ", ties: " +
            tieVar
    );
} else if (optionVar[userVar].lose == compVar) {
    loseVar++;
    alert(
        "You lose! " +
            "Your wins: " +
            winVar +
            ", your losses: " +
            loseVar +
            ", ties: " +
            tieVar
    );
} else {
    tieVar++;
    alert(
        "You tied! " +
            "Your wins: " +
            winVar +
            ", your losses: " +
            loseVar +
            ", ties: " +
            tieVar
    );
}

Confirm window with boolean values set to false=continue, true=end loop. If gameStatus = 1, then exit message displays.

//If cancel is pressed, game will continue as gameStatus remains 0.
if (!window.confirm("Press Ok to play again")) {
    gameStatus = gameStatus + 1;
}

//Exit message
if (gameStatus == 1) {
    alert("Thanks for playing!");
}

To Do

  • Add processing for user input so that the user can input strings such as "rock", "Rock", "r".
  • Add user input validation so that wrong inputs return the user input prompt instead of terminating the game.

About


Languages

Language:JavaScript 74.5%Language:HTML 25.5%