Skip to content

Instantly share code, notes, and snippets.

@gitdagray
Created October 8, 2020 18:31
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save gitdagray/c33ef93ba4c18aa3d123c26be23d539a to your computer and use it in GitHub Desktop.
Save gitdagray/c33ef93ba4c18aa3d123c26be23d539a to your computer and use it in GitHub Desktop.
Rock_Paper_Scissors_v3
// Rock, Paper, Scissors: Refactored with Functions
const initGame = () => {
const startGame = confirm("Shall we play rock, paper, or scissors?");
startGame ? playGame() : alert("Ok, maybe next time.");
};
// Game flow function
const playGame = () => {
while (true) {
let playerChoice = getPlayerChoice();
playerChoice = formatPlayerChoice(playerChoice);
if (playerChoice === "") {
invalidChoice();
continue;
}
if (!playerChoice) {
decidedNotToPlay();
break;
}
playerChoice = evaluatePlayerChoice(playerChoice);
if (!playerChoice) {
invalidChoice();
continue;
}
const computerChoice = getComputerChoice();
const result = determineWinner(playerChoice, computerChoice);
displayResult(result);
if (askToPlayAgain()) {
continue;
} else {
thanksForPlaying();
break;
}
}
};
const getPlayerChoice = () => {
return prompt("Please enter rock, paper, or scissors.");
};
const formatPlayerChoice = (playerChoice) => {
if (playerChoice || playerChoice === "") {
return playerChoice.trim().toLowerCase();
} else {
return false;
}
};
const decidedNotToPlay = () => {
alert("I guess you changed your mind. Maybe next time.");
};
const evaluatePlayerChoice = (playerChoice) => {
if (
playerChoice === "rock" ||
playerChoice === "paper" ||
playerChoice === "scissors"
) {
return playerChoice;
} else {
return false;
}
};
const invalidChoice = () => {
alert("You didn't enter rock, paper, or scissors.");
};
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
const rpsArray = ["rock", "paper", "scissors"];
return rpsArray[randomNumber];
};
const determineWinner = (player, computer) => {
const winner =
player === computer
? "Tie game!"
: player === "rock" && computer === "paper"
? `playerOne: ${player}\nComputer: ${computer}\nComputer wins!`
: player === "paper" && computer === "scissors"
? `playerOne: ${player}\nComputer: ${computer}\nComputer wins!`
: player === "scissors" && computer === "rock"
? `playerOne: ${player}\nComputer: ${computer}\nComputer wins!`
: `playerOne: ${player}\nComputer: ${computer}\nplayerOne wins!`;
return winner;
};
const displayResult = (result) => {
alert(result);
};
const askToPlayAgain = () => {
return confirm("Play Again?");
};
const thanksForPlaying = () => {
alert("Ok, thanks for playing.");
};
initGame();
@MizanSafin
Copy link

💌

@Tayokanch
Copy link

Thank you, Dave, you are such an amazing tutor

@muwaffaqelbadawi
Copy link

Thanks, I will get a lot of inspiration from this!!

@Eddiexxx
Copy link

Eddiexxx commented Oct 2, 2023

I don't care if structured method takes fewer lines , functional programming always makes sense and much more organized

@ifykenchi
Copy link

Thanks Dave.

@AdebayoGlover
Copy link

Intelligent.

Thanks a lot Dave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment