Skip to content

Instantly share code, notes, and snippets.

@ryankinal
Created December 18, 2012 20:06
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 ryankinal/4331454 to your computer and use it in GitHub Desktop.
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.
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