A Pen by Evelyn Rios on CodePen.
Created
October 6, 2013 23:01
-
-
Save evelynriossf/6860258 to your computer and use it in GitHub Desktop.
A Pen by Evelyn Rios.
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
var Choice = ("yes"); | |
var computerScore = 0; | |
var userScore = 0; | |
var playRPS = function (Choice, computerScore, userScore) { | |
var userChoice = 0; | |
do { | |
if (Choice.toLowerCase() == ("yes")) { | |
userChoice = prompt("This is a game of Rock-Paper-Scissors. Do you choose rock, paper or scissors?"); | |
if (userChoice.toLowerCase() == "no") { | |
alert("Thanks for playing... Goodbye!"); | |
return; | |
} | |
} else { | |
userChoice = Choice; | |
} | |
} while (userChoice.toLowerCase() !== "rock" && userChoice.toLowerCase() !== "paper" && userChoice.toLowerCase() !== "scissors"); | |
var computerChoice = Math.random(); | |
if (computerChoice < 0.34) { | |
computerChoice = "rock"; | |
} else if (computerChoice <= 0.67) { | |
computerChoice = "paper"; | |
} else { | |
computerChoice = "scissors"; | |
} | |
var compare = function (choice1, choice2) { | |
if (choice1 == choice2) { | |
return ("tie"); | |
} else if (choice1 == ("rock")) { | |
if (choice2 == ("scissors")) { | |
userScore++; | |
return ("You picked rock. The computer picked scissors. Rock beats scissors, so you win! The score is: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " ."); | |
} else { | |
computerScore++; | |
return ("You picked rock. The computer picked paper. Paper beats rock, so the computer wins! The score is: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " ."); | |
} | |
} else if (choice1 == ("paper")) { | |
if (choice2 == ("rock")) { | |
userScore++; | |
return ("You picked paper. The computer picked rock. Paper beats rock, so you win! The score is: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " ."); | |
} else { | |
computerScore++; | |
return ("You picked paper. The computer picked scissors. Scissors beats paper, so the computer wins! The score is: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " ."); | |
} | |
} else if (choice1 == ("scissors")) { | |
if (choice2 == ("rock")) { | |
computerScore++; | |
return ("You picked scissors. The computer picked rock. Rock beats scissors, so the computer wins! The score is: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " ."); | |
} else { | |
userScore++; | |
return ("You picked scissors. The computer picked paper. Scissors beats paper, so you win! The score is: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " ."); | |
} | |
} | |
}; | |
var result = compare(userChoice.toLowerCase(), computerChoice); | |
if (result === "tie") { | |
var playAgain = prompt("You and the computer both picked " + (userChoice.toLowerCase()) + ". The result is a tie. The score is still: YOU - " + (userScore) + ", COMPUTER - " + (computerScore) + " . Try again!"); | |
// playRPS("yes", computerScore, userScore); | |
} else { | |
var playAgain = prompt(result + " Do you want to play again?"); | |
} | |
var options = ["rock", "paper", "scissors"]; | |
if (playAgain.toLowerCase() == ("yes")) { | |
playRPS("yes", computerScore, userScore); | |
} else if (options.indexOf(playAgain.toLowerCase()) !== -1) { | |
playRPS(playAgain, computerScore, userScore); | |
} else { | |
alert("Thanks for playing. Goodbye!"); | |
} | |
}; | |
playRPS("yes", 0 , 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment