-
Notifications
You must be signed in to change notification settings - Fork 5
/
game_01.js
executable file
·54 lines (42 loc) · 1.54 KB
/
game_01.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
/* The Game_01 Class
* This extends the Game class. This class holds
* all the rules for the x01 dart game variation.
*/
Game_01.prototype = new Game(new Array());
function Game_01(playerNames, gameScore) {
//Find the winning game score
this.gameScore = gameScore * 100 + 1;
Game.apply(this, arguments);
}
Game_01.prototype.isPlayerWinner = function(player) {
var score = player.score() == this.gameScore;
var doubleThrown = player.lastDartThrown.isDouble;
if(score && doubleThrown)
return true;
return false;
}
Game_01.prototype.playerScore = function(player) {
var score = 0;
var doubleThrown = this.currentPlayer().lastDartThrown.isDouble;
var that = this;
player.rounds.forEach(function(round) {
var newScore = score + round.score();
if((newScore < (that.gameScore - 1)) || (newScore == that.gameScore && doubleThrown)) {
score = newScore;
}
})
return score;
}
Game_01.prototype.roundIsBust = function() {
var player = this.currentPlayer()
var dartsThrown = player.currentRound().numberOfDartsThrown() > 0;
if(dartsThrown && player.score() == 0)
return false;
if(player.currentRound().score > 0 && player.score() == player.scoreAfterLastRound && dartsThrown)
return true;
return false;
}
Game_01.prototype.roundIsComplete = function() {
var threeDartsThrown = this.currentPlayer().currentRound().isComplete();
return threeDartsThrown || this.roundIsBust();
}