Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zilveer
Forked from damianesteban/snakeEyes.js
Created April 30, 2017 23:18
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 zilveer/c8dd9f62030082cb8174fb041c1363b7 to your computer and use it in GitHub Desktop.
Save zilveer/c8dd9f62030082cb8174fb041c1363b7 to your computer and use it in GitHub Desktop.
Dice Game in JavaScript
function rollSingleDice()
{
return Math.floor(Math.random()*6+1);
}
//Make the rollDice function roll dice,
//check for doubles, and return the
//total score achieved by all rolls
function rollDice(player) {
var score = 0;
var roll1; var roll2;
var playerScore;
do {
roll1 = rollSingleDice();
roll2 = rollSingleDice();
score = roll1 + roll2;
player.addToScore(score);
playerScore = player.getScore();
console.log("Roll 1: " + roll1 + " Roll 2: " + roll2 + " Total Score: " + playerScore);
if (roll1 == roll2) {
if (roll1 === 1) {
console.log("Snake Eyes!");
return true;
} else {
console.log("Congrats! Double Thrown!");
}
}
} while (roll1 === roll2);
return false;
}
function Player(name) {
this.name = name;
var score = 0; // this is a private attribute
this.addToScore = function(points) {
score = score + points;
};
this.getScore = function() {
return score;
};
}
var player1 = new Player("Bobby");
var snakeEyes = false;
while(snakeEyes === false) {
snakeEyes = rollDice(player1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment