-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplayer.js
More file actions
executable file
·43 lines (36 loc) · 1.17 KB
/
player.js
File metadata and controls
executable file
·43 lines (36 loc) · 1.17 KB
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
/* The player class
* This class keeps track of all player specific actions, and data */
function Player(game, playerName) {
this.playerName = playerName;
this.game = game;
this.lastDartThrown = new Dart();
this.rounds = new Array(new Round());
this.scoreAfterLastRound = 0;
}
//Returns true when the player's round is complete.
Player.prototype.addDart = function(dart) {
this.lastDartThrown = dart;
this.currentRound().addDart(dart);
if(this.game.roundIsComplete()) {
this.endRound();
return true;
}
return false;
}
Player.prototype.endRound = function() {
this.scoreAfterLastRound = this.score();
this.rounds.push(new Round());
}
Player.prototype.score = function() {
//Calls back to the game class since if the
//players score depends on the game rules
return this.game.playerScore(this);
}
Player.prototype.currentRound = function() {
return this.rounds.last();
}
Player.prototype.isWinner = function() {
//Calls back to the game class since if the player is
//a winner depends on the game rules
return this.game.isPlayerWinner(this);
}