-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.ts
More file actions
65 lines (63 loc) · 1.56 KB
/
player.ts
File metadata and controls
65 lines (63 loc) · 1.56 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// This is a single player class, it shall hold all info
// specific to that player
class Player{
name: string; // let's have a unique name please
hand: Card[] = []; // and their hands, cool
constructor(s: string){
this.name = s;
this.emptyHand();
}
addCard(c: Card){ // you deserve to receive a card
this.hand.push(c);
}
emptyHand(){ // you ain't got nothin'
this.hand = [];
}
printHand(i: number){ // I want to know what you have in your hand
console.log(this.name + " has: " + this.score());
for (; i < this.hand.length; i++)
console.log(
this.hand[i].val() +
" of " +
this.hand[i].suit
);
}
score(): number{ // Let's tally that score for you
var output: number = 0;
var numAces: number = 0;
this.hand.forEach(function(c: Card){
if (c.val() == "Ace") numAces++;
else output += c.valNum();
});
for (;numAces > 0; numAces--){
output += (output + 10 + numAces - 1 <= 21) ? 10 : 1;
}
return output;
}
}
// This container shall hold all current players and will
// handle information for all of them
class PlayerContainer{
data: Player[] = []; // who's playing?
constructor(){
this.addPlayer('Dealer');
}
addPlayer(s: string){ // challenger approaching
this.data.push(new Player(s));
}
getPlayer(i: number): Player{
return this.data[i];
}
firstDeal(d: Deck){
for (var i = this.data.length - 1; i > 0; i--){
this.data[i].addCard(d.deal());
this.data[i].addCard(d.deal());
}
this.data[0].addCard(d.deal());
}
printAll(){
this.data.forEach(function(p){
p.printHand(0);
});
}
}