-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeckBuilder.js
75 lines (54 loc) · 1.64 KB
/
deckBuilder.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
var suits = require('./enum/suit.js');
var Card = require('./card.js');
var DeckBuilder = function() {
// Amount of players
this._playerCount = -1;
// Cards per suit (hearts, diamonds, clubs, spades)
this._cardsPerSuit = 13;
// The amount of players that can play with 1 deck
// If got more than 5 players we need 2 decks
this._playersPerDeck = 4;
}
// Set the player amount
DeckBuilder.prototype.setPlayerCount = function(playerCount){
if( typeof playerCount != "number")
return false;
this._playerCount = playerCount;
}
// Return the player count
DeckBuilder.prototype.getPlayerCount = function(){
return this._playerCount;
}
DeckBuilder.prototype.suitExists = function(s){
for (var suit in suits) {
if( s == suit){
return true;
}
}
return false;
}
// Build the Deck of cards
DeckBuilder.prototype.build = function() {
var cards = [];
var playerCount = this.getPlayerCount();
var amountOfDecks = Math.ceil( playerCount / this._playersPerDeck );
// Loop the amount of decks that we need to create
for( var i = 0; i < amountOfDecks; i++ ){
// We need to create 13 cards per suit (diamonds, spades, etc..)
for (var suit in suits) {
// Check is important, because the object has also prototype properties
if (suits.hasOwnProperty(suit)) {
for( var j = 1; j <= this._cardsPerSuit; j++ ){
var value = j;
var card = new Card(value, suit);
cards.push(card);
}
}
}
}
// Add 2 jokers
cards.push( new Card(0) );
cards.push( new Card(0) );
return cards;
};
module.exports = DeckBuilder;