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.
https://jasonluxie.github.io/2-1-mp-rock-paper-scissors/
- 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.
User input is received via prompt box
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.
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.
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" },
};
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!");
}
- 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.