Created
December 18, 2012 20:06
-
-
Save ryankinal/4331454 to your computer and use it in GitHub Desktop.
Petals Around the Rose Game, played in the JavaScript console. `PetalsAroundtheRose.start()` to start. `PetalsAroundTheRose.answer()` to answer.
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 PetalsAroundTheRose = (function() { | |
var sides = 6, | |
dice = 5, | |
currentRolls; | |
function getPetals(rolls) | |
{ | |
var score = 0; | |
rolls.forEach(function(item) { | |
if (item === 3 || item === 5) | |
{ | |
score += item - 1; | |
} | |
}); | |
return score; | |
} | |
function getRolls(count) | |
{ | |
var rolls = []; | |
while (count-- > 0) | |
{ | |
rolls.push(Math.ceil(Math.random() * sides)); | |
} | |
return rolls; | |
} | |
function answer(value) | |
{ | |
var correctAnswer = getPetals(currentRolls); | |
if (correctAnswer === value) | |
{ | |
console.log('Correct'); | |
} | |
else | |
{ | |
console.log('You answered ' + value); | |
console.log('The correct answer is ' + correctAnswer); | |
} | |
start(); | |
} | |
function start() | |
{ | |
currentRolls = getRolls(dice); | |
console.log('The five dice are: ' + currentRolls.join(', ')); | |
console.log('What is the score?'); | |
} | |
return { | |
start: start, | |
answer: answer | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment