Created
August 10, 2015 01:31
-
-
Save MicFin/becb230b75f007c52913 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// user select rocks, paper or scissors | |
function getUserChoice(){ | |
var userSelection = prompt("Do you choose rock, paper or scissors?"); | |
return userSelection; | |
} | |
// computer randomly selects rock paper or scissors | |
function getComputerChoice(){ | |
var computerSelection = Math.random(); | |
if (computerSelection < 0.34) { | |
computerSelection = "rock"; | |
} else if(computerSelection <= 0.67) { | |
computerSelection = "paper"; | |
} else { | |
computerSelection = "scissors"; | |
} | |
return computerSelection; | |
} | |
function getWinner(computerSelection, userSelection){ | |
if (computerSelection === userSelection ) { | |
return "Tie"; | |
} else if (computerSelection === "rock" && userSelection == "scissors") { | |
return "Computer"; | |
} else if (computerSelection === "paper" && userSelection == "rock") { | |
return "Computer"; | |
} else if (computerSelection === "scissors" && userSelection == "paper") { | |
return "Computer"; | |
}else { | |
return "User"; | |
} | |
} | |
function playGame(){ | |
var userChoice = getUserChoice(); | |
var computerChoice = getComputerChoice(); | |
var winner = getWinner(userChoice, computerChoice); | |
return winner; | |
} | |
function playTournament(final_score){ | |
var userScore = 0; | |
var computerScore = 0; | |
while (computerScore < final_score && userScore < final_score){ | |
var gameWinner = playGame(); | |
if (gameWinner == "Computer") { | |
computerScore++; | |
alert("Computer wins round, Score: Computer-"+computerScore+" User-"+userScore); | |
} else if (gameWinner == "User"){ | |
userScore++; | |
alert("User wins round, Score: Computer-"+computerScore+" User-"+userScore); | |
}else { | |
alert("Tie round, Score: Computer-"+computerScore+" User-"+userScore); | |
} | |
} | |
if (userScore > computerScore) { | |
alert("User wins game, Score: Computer-"+computerScore+" User-"+userScore); | |
} else { | |
alert("Computer wins game, Score: Computer-"+computerScore+" User-"+userScore) | |
} | |
} | |
playTournament(5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment