Created
October 8, 2020 18:31
-
-
Save gitdagray/c33ef93ba4c18aa3d123c26be23d539a to your computer and use it in GitHub Desktop.
Rock_Paper_Scissors_v3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
Thank you, Dave, you are such an amazing tutor
Thanks, I will get a lot of inspiration from this!!
I don't care if structured method takes fewer lines , functional programming always makes sense and much more organized
Thanks Dave.
Intelligent.
Thanks a lot Dave
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
💌