Skip to content

Instantly share code, notes, and snippets.

@Ebugo
Forked from SinachPat/RPS Project
Last active January 19, 2022 14:22
Show Gist options
  • Save Ebugo/ed4ded27f688fb9227a5d9a853f35aaa to your computer and use it in GitHub Desktop.
Save Ebugo/ed4ded27f688fb9227a5d9a853f35aaa to your computer and use it in GitHub Desktop.
const choices = ["rock", "paper", "scissors"];
function computerPlay(choices) {
let option = choices[Math.floor(Math.random() * choices.length)];
return option;
}
const possibleResults = {
rock_rock: { action: "tie", message: "It's a tie! rock holds rock" },
rock_paper: { action: "lose", message: "You lose! paper covers rock" },
rock_scissors: { action: "win", message: "You win! rock beats scissors" },
scissors_scissors: { action: "tie", message: "It's a tie" },
scissors_rock: { action: "lose", message: "You lose! rock beats scissors" },
scissors_paper: { action: "win", message: "You win! scissors beats paper" },
paper_paper: { action: "tie", message: "It's a tie!" },
paper_rock: { action: "win", message: "You win! paper covers rock" },
paper_scissors: { action: "lose", message: "You lose! scissors beats paper" }
}
function oneRound(playerSelection, computerSelection) {
if (choices.includes(playerSelection) && choices.includes(computerSelection)) {
return possibleResults[`${playerSelection}_${computerSelection}`];
} else return { message: "There was no play!" };
}
function game() {
let playerScore = 0, computerScore = 0;
const totalRounds = 5;
let playerSelection, computerSelection;
for (let i = 0; i < totalRounds; i++) {
playerSelection = prompt("What do you play").toLowerCase();
computerSelection = computerPlay(choices);
const result = oneRound(playerSelection, computerSelection);
console.log(result.message);
switch (result.action) {
case "win":
playerScore++;
break;
case "lose":
computerScore++;
break;
default:
console.log("We play again!");
break;
}
}
console.log(playerScore);
console.log(computerScore);
}
game();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment