-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayerOrderEngine.js
More file actions
36 lines (31 loc) · 873 Bytes
/
Copy pathPlayerOrderEngine.js
File metadata and controls
36 lines (31 loc) · 873 Bytes
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
'use strict';
class PlayerOrderEngine {
constructor(players) {
if (!(players instanceof Array) || players.length === 0) {
throw new Error("Specify at least one players");
}
this._players = players;
this._currentPlayerIndex = 0;
this._scores = {};
}
currentPlayer() {
return this._players[this._currentPlayerIndex];
}
recordThrow(number, factor) {
var score = number * (factor || 1);
if (!this._scores[score]) {
this._scores[score] = [this.currentPlayer()];
} else {
this._scores[score] = this._scores[score].concat([this.currentPlayer()]);
}
this._currentPlayerIndex ++;
}
playerOrder() {
var players = [];
for (var score of Object.keys(this._scores).reverse()) {
players = players.concat(this._scores[score]);
}
return players;
}
}
module.exports = PlayerOrderEngine;