Skip to content

Instantly share code, notes, and snippets.

@jloescher
Created February 6, 2021 02:56
Show Gist options
  • Save jloescher/a7412a198acc1651b79f9e408abe5649 to your computer and use it in GitHub Desktop.
Save jloescher/a7412a198acc1651b79f9e408abe5649 to your computer and use it in GitHub Desktop.
Simple CLI rock, paper, scissors game.
const userInput = 'Bomb'.toLowerCase();
function getComputerChoice() {
let randomInt = Math.floor(Math.random() * 3);
switch (randomInt) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
}
}
function getUserChoice(userInput) {
if (
userInput === 'rock' ||
userInput === 'paper' ||
userInput === 'scissors' ||
userInput === 'bomb'
) {
return userInput;
} else {
console.log('Please enter either rock, paper, or scissors');
}
}
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'The game was a tie.';
} else if (userChoice === 'bomb') {
return `The user wins! The ${userChoice} kills all!`;
} else {
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return `${computerChoice} covers ${userChoice}, computer wins!`;
} else {
return `${userChoice} smashes ${computerChoice}, user wins!`;
}
} else if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return `${computerChoice} cuts ${userChoice}, computer wins!`;
} else {
return `${userChoice} covers ${computerChoice}, user wins!`;
}
} else if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return `${computerChoice} smashes ${userChoice}, computer wins!`;
} else {
return `${userChoice} cuts ${computerChoice}, user wins!`;
}
}
}
}
function playGame() {
console.log(determineWinner(getUserChoice(userInput), getComputerChoice()));
}
playGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment