-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_eyes.js
81 lines (75 loc) · 1.77 KB
/
snake_eyes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var players = [];
function Player(blah){
this.name = blah;
var score = 0;
this.addToScore = function(add){
score += add;
};
this.getScore = function(){
return score;
};
}
function rollSingleDice()
{
return Math.floor(Math.random()*6+1);
}
var snakeEyes = false;
//Make the rollDice function roll dice,
//check for doubles, and return the
//total score achieved by all rolls
function rollDice(player)
{
var roll1 = rollSingleDice();
var roll2 = rollSingleDice();
player.addToScore(roll1 + roll2);
console.log("Die 1: " + roll1 + " Die 2: " + roll2 + " Score: " + player.getScore());
if(roll1 === 1 && roll2 ===1)
{
console.log("Snake Eyes!");
snakeEyes = true;
return true;
}
if(roll1 === roll2)
console.log("Congratulations Double Thrown!");
return false;
}
var i;
for(i=0; i<4; i++)
{
temp = prompt("Enter player name");
players.push(new Player(temp));
}
var turn = 1;
while(!snakeEyes){
console.log("Turn " + turn);
for(i=0; i<players.length; i++)
{
rollDice(players[i]);
}
turn++;
}
for(i=0; i<players.length; i++)
console.log(players[i].name + " scored " + players[i].getScore());
var highScore = 0;
for(i=0; i<players.length; i++)
{
if(players[i].getScore() > highScore)
highScore = players[i].getScore();
}
console.log("high score is: " + highScore);
var winners = [];
for(i=0; i<players.length; i++)
{
if(players[i].getScore() === highScore)
winners.push(players[i].name);
}
if(winners.length > 1)
{
console.log("Congratulations " + winners[0]);
for(i=1; i<winners.length; i++)
console.log(" and " + winners[i]);
console.log(" you have won!");
}else
{
console.log("Congratulations " + winners[0] + " you have won!");
}