Skip to content

Instantly share code, notes, and snippets.

@emohamed
Created January 20, 2014 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emohamed/8522888 to your computer and use it in GitHub Desktop.
Save emohamed/8522888 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<title>Document</title>
</head>
<body>
<div id="buttons">
<input type="button" data-user-choice="rock" value="Rock" />
<input type="button" data-user-choice="scissor" value="Scissors" />
<input type="button" data-user-choice="paper" value="Paper" />
</div>
<div id="result">Make a choise!</div><!-- /#result -->
<script type="text/javascript">
"use strict";
console.time("Executed in:");
console.group("Task - Rock Paper Scissor");
(function () {
var ROCK = "rock",
SCISSOR = "scissor",
PAPER = "paper";
var GAME_RESULT_TIE = 0,
GAME_RESULT_WINNER_PLAYER = 1,
GAME_RESULT_WINNER_COMPUTER = 2;
var rules = {};
// Winner = loser
rules[SCISSORS] = PAPER;
rules[PAPER] = ROCK;
rules[ROCK] = SCISSORS;
var choices_names = {};
rules[SCISSORS] = "Paper";
rules[PAPER] = "Rock";
rules[ROCK] = "Scissors";
var sum = function (number_a , number_b) {
return number_a + number_b;
}
function pick_random(min, max) {
return Math.floor(Math.random() * max) + min;
}
function get_winner(computer_choice, player_choice) {
if (computer_choice === player_choice) {
return GAME_RESULT_TIE;
}
if (rules[computer_choice] == player_choice) {
return GAME_RESULT_WINNER_COMPUTER;
}
return GAME_RESULT_WINNER_PLAYER;
}
var choose_winner = function(person_choice) {
var computer_choice = Math.floor(Math.random() * 3) + 1;
var game_result = get_winner(person_choice, computer_choice);
var result = '';
if (game_result == GAME_RESULT_TIE) {
result = "Tie!";
} else if(person_choice === GAME_RESULT_WINNER_PLAYER) {
result = "You are the winner!";
} else {
result = "You lose!";
}
result += "Computer choice: " + choices_names[computer_choice] + ".";
console.log(result);
document.getElementById("result").innerHTML = result;
}
document.getElementById("buttons").getElementsByTagName('button').onclick = function() {
choose_winner(this.getAttribute('data-user-choice'));
}
})();
console.groupEnd();
console.timeEnd("Executed in:");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment