Skip to content

Instantly share code, notes, and snippets.

@MicFin
Last active August 29, 2015 14:19
Show Gist options
  • Save MicFin/1ae92b0857ae4d22e00c to your computer and use it in GitHub Desktop.
Save MicFin/1ae92b0857ae4d22e00c to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
////////////////////////////////////////////////
/* Provided Code - Please Don't Edit */
////////////////////////////////////////////////
// 'use strict';
function getInput() {
var answer = prompt("Please choose either 'Rock', 'Paper', or 'Scissors' must be capitalized and spelled correctly.");
return answer;
}
function randomPlay() {
var randomNumber = Math.random();
if (randomNumber < 0.33) {
return "Rock";
} else if (randomNumber < 0.66) {
return "Paper";
} else {
return "Scissors";
}
}
////////////////////////////////////////////////
/* Write Your Code Below */
////////////////////////////////////////////////
function getPlayerMove(move) {
// Write an expression that operates on a variable called `move`
// var move;
// if (move !== undefined) {
// return move;
// } else {
return getInput();
// }
// If a `move` has a value, your expression should evaluate to that value.
// However, if `move` is not specified / is null, your expression should equal `getInput()`.
// return move; Your Expression ;
}
function getComputerMove(move) {
// Write an expression that operates on a variable called `move`
// var move;
if (move !== undefined) {
return move;
} else {
return randomPlay()
}
// If a `move` has a value, your expression should evaluate to that value.
// However, if `move` is not specified / is null, your expression should equal `randomPlay()`.
// return move; Your Expression ;
}
function getWinner(playerMove, computerMove) {
var winner;
if ( (playerMove === "Rock") && (computerMove === "Scissors") ) {
winner = "player";
} else if ((playerMove === "Rock") && (computerMove === "Paper") ){
winner = "computer";
} else if ((playerMove === "Paper") && (computerMove === "Rock") ){
winner = "player";
} else if ((playerMove === "Paper") && (computerMove === "Scissors") ){
winner = "computer";
} else if ((playerMove === "Paper") && (computerMove === "Rock") ){
winner = "player";
} else if ((playerMove === "Scissors") && (computerMove === "Rock") ){
winner = "computer";
} else {
winner = "tie";
}
// Write code that will set winner to either 'player', 'computer', or 'tie' based on the values of playerMove and computerMove.
// Assume that the only values playerMove and computerMove can have are 'rock', 'paper', and 'scissors'.
// The rules of the game are that 'rock' beats 'scissors', 'scissors' beats 'paper', and 'paper' beats 'rock'.
/* YOUR CODE HERE */
return winner;
}
var playerWins = 0;
var computerWins = 0;
function playNewGame() {
var player = getPlayerMove();
var computer = getComputerMove();
var game_winner = getWinner(player, computer);
if (game_winner === "player") {
playerWins++;
alert("player won round" + " score: computer-"+computerWins+" player-"+playerWins);
} else if (game_winner === "computer") {
computerWins++;
alert("computer won round" + " score: computer-"+computerWins+" player-"+playerWins);
} else{
alert("tie round")
}
}
function resetGame(){
playerWins = 0;
computerWins = 0;
playToFive();
}
function playToFive() {
playNewGame();
if (computerWins === 5) {
alert("Computer Won GAME");
if (confirm("Play Again?") ) {
// playerWins = 0;
// computerWins = 0;
// playToFive();
resetGame();
} else{
alert("Goodbye");
}
} else if (playerWins === 5) {
alert("Player Won GAME");
if (confirm("Play Again?") ) {
// playerWins = 0;
// computerWins = 0;
// playToFive();
resetGame();
} else{
alert("Goodbye");
}
} else {
playToFive();
}
}
playToFive();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment