-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaccMan.js
159 lines (147 loc) · 4.44 KB
/
BaccMan.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class BaccMan {
constructor(){
this.deck = this.generateDefault52CardDeck();
this.playerTotalScore = 0;
this.playerHand = [];
this.bankerTotalScore = 0;
this.bankerHand = [];
}
generateDefault52CardDeck(){
let suits = ['C', 'D', 'H','S'];
let faces = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'];
let cards = [];
for(let suitCtr = 0; suitCtr<suits.length; suitCtr++)
{
for(let faceCtr = 0; faceCtr < faces.length; faceCtr++)
{
cards.push(suits[suitCtr]+'_'+faces[faceCtr]);
}
}
return cards;
}
DealCardFromDeck(){
let randomCardPosition = Math.floor(Math.random()*this.deck.length);
let randomCard = this.deck[randomCardPosition];
this.deck.splice(randomCardPosition,1);
return randomCard;
}
DealToPlayer()
{
this.playerHand.push(this.DealCardFromDeck());
this.playerTotalScore = this.ParseHandScore(this.playerHand);
}
DealToBanker()
{
this.bankerHand.push(this.DealCardFromDeck());
this.bankerTotalScore = this.ParseHandScore(this.bankerHand);
}
ParseCardScore(card)
{
let splitter = card.split("_");
let face = splitter[1];
switch (face)
{
case 'J':
return 0;
case 'Q':
return 0;
case 'K':
return 0;
case 'A':
return 1;
default:
return parseInt(face);
}
}
ParseHandScore(cardHand)
{
let totalScore = 0;
for(let card of cardHand)
{
totalScore += this.ParseCardScore(card);
}
return totalScore%10;
}
PlayAndGetResults()
{
for(let i=0; i<2; i++)
{
this.DealToPlayer();
this.DealToBanker();
}
//check for score of 8-9 to go for auto finalize
if(this.playerTotalScore<=8 || this.bankerTotalScore<=8)
{
//finalize!
}else
{
//check if player gets third card
if(this.playerTotalScore<=5)
{
this.DealToPlayer();
}
//check if banker gets third card
if(this.playerHand.length == 2)
{
if(this.bankerTotalScore<=5)
{
this.DealToBanker();
}
}else {
let playerThirdCardScore = this.ParseCardScore(this.playerHand[this.playerHand.length-1]);
switch(this.bankerTotalScore)
{
case 0:
case 1:
case 2:
this.DealToBanker();
break;
case 3:
if(playerThirdCardScore!=8) this.DealToBanker();
break;
case 4:
if(playerThirdCardScore>=2 &&playerThirdCardScore<=7) this.DealToBanker();
break;
case 5:
if(playerThirdCardScore>=4 &&playerThirdCardScore<=7) this.DealToBanker();
break;
case 6:
if(playerThirdCardScore==6 || playerThirdCardScore==7) this.DealToBanker();
break;
default:
//stand
break;
}
}
}
//final results
// console.log(this.playerHand);
// console.log(this.playerTotalScore);
// console.log(this.bankerHand);
// console.log(this.bankerTotalScore);
let winner;
if(this.playerTotalScore == this.bankerTotalScore)
{
// console.log("tie!");
winner = 'T';
}else
{
if(this.playerTotalScore>this.bankerTotalScore)
{
// console.log("player wins");
winner = 'P';
}else
{
// console.log("banker wins!")
winner = "B";
}
}
return {
"playerHand":this.playerHand,
"playerScore":this.playerTotalScore,
"bankerHand":this.bankerHand,
"bankerScore":this.bankerTotalScore,
"winner":winner
}
}
}