-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hand.js
230 lines (230 loc) · 10.7 KB
/
Hand.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
exports.__esModule = true;
exports.Hand = void 0;
var Card_js_1 = require("./Card.js");
var Rank_js_1 = require("./Rank.js");
/////////////////////////////////
//
//
/* SECTION BELOW: this is the hand class */
//
//
/////////////////////////////////
var Hand = /** @class */ (function () {
function Hand() {
this.cardObjectsInHand = [];
this.numberCardsInHand = 0;
}
//EFFECTS: returns number of Card objects in hand
Hand.prototype.getSize = function () {
return this.numberCardsInHand;
};
//EFFECTS: returns array of cards
Hand.prototype.getCardObjectsInHand = function () {
return this.cardObjectsInHand;
};
// EFFECTS: returns true if hand has three of diamonds
Hand.prototype.hasThreeOfDiamonds = function () {
for (var i = 0; i < this.numberCardsInHand; ++i) {
if (this.cardObjectsInHand[i].isThreeOfDiamonds())
return true;
}
return false;
};
// REQUIRES: addedCard is a Card object
// EFFECTS: adds addedCard to cardObjectsInHand
Hand.prototype.addCardToHand = function (addedCard) {
this.cardObjectsInHand.push(addedCard);
++this.numberCardsInHand;
// FUTURE: addCardToPlayer currently sorts this.allCards, but this may change in future
this.cardObjectsInHand.sort(Card_js_1.Card.compareCards);
};
// EFFECTS: returns true if cardHand is a single card, false otherwise
Hand.prototype.isSingleCard = function () {
// COMMENTS: check if played cards is proper length
return this.numberCardsInHand == 1;
};
// EFFECTS: returns true if cardHand is a pair of cards, false otherwise
Hand.prototype.isPair = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 2)
return false;
// COMMENTS: check if both cards are same rank
return this.cardObjectsInHand[0].hasSameRankAs(this.cardObjectsInHand[1]);
};
// EFFECTS: returns true if cardHand is three of a kind, false otherwise
Hand.prototype.isThreeOfAKind = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 3)
return false;
// COMMENTS: check if all three cards are same rank
return this.cardObjectsInHand[0].hasSameRankAs(this.cardObjectsInHand[1])
&& this.cardObjectsInHand[0].hasSameRankAs(this.cardObjectsInHand[2]);
};
// EFFECTS: returns true if cardHand is a four of a kind, false otherwise
Hand.prototype.isFourOfAKind = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 5)
return false;
// COMMENTS: check if middle three cards are same rank (this works since it's a sorted hand)
if (!this.cardObjectsInHand[1].hasSameRankAs(this.cardObjectsInHand[2]))
return false;
if (!this.cardObjectsInHand[2].hasSameRankAs(this.cardObjectsInHand[3]))
return false;
// COMMENTS: check if either end cards are equal to the center card
return this.cardObjectsInHand[0].hasSameRankAs(this.cardObjectsInHand[2])
|| this.cardObjectsInHand[2].hasSameRankAs(this.cardObjectsInHand[4]);
};
// EFFECTS: returns true if cardHand is a straight, false otherwise
Hand.prototype.isStraight = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 5)
return false;
// COMMENTS: if cards are not consecutive, then it is false
if (Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[0].getRankName()) + 1
!= Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[1].getRankName()))
return false;
if (Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[1].getRankName()) + 1
!= Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[2].getRankName()))
return false;
if (Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[2].getRankName()) + 1
!= Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[3].getRankName()))
return false;
if (Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[3].getRankName()) + 1
!= Rank_js_1.Rank.rankStrengths.get(this.cardObjectsInHand[4].getRankName()))
return false;
// COMMENTS: if all cards are consecutive, then it is true
return true;
};
// EFFECTS: returns true if cardHand is a flush, false otherwise
Hand.prototype.isFlush = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 5)
return false;
// COMMENTS: if cards do not have same suit, then it is false
if (this.cardObjectsInHand[0].getSuitName() != this.cardObjectsInHand[1].getSuitName())
return false;
if (this.cardObjectsInHand[1].getSuitName() != this.cardObjectsInHand[2].getSuitName())
return false;
if (this.cardObjectsInHand[2].getSuitName() != this.cardObjectsInHand[3].getSuitName())
return false;
if (this.cardObjectsInHand[3].getSuitName() != this.cardObjectsInHand[4].getSuitName())
return false;
// COMMENTS: if all cards have same suit, then it is true
return true;
};
// EFFECTS: returns true if cardHand is a house, false otherwise
Hand.prototype.isHouse = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 5)
return false;
// COMMENTS: check if first two are same rank and last two are same rank
if (this.cardObjectsInHand[0].getRankName() != this.cardObjectsInHand[1].getRankName())
return false;
if (this.cardObjectsInHand[3].getRankName() != this.cardObjectsInHand[4].getRankName())
return false;
// COMMENTS: if middle card has same rank as either first or last cards, then return true
return (this.cardObjectsInHand[1].getRankName() == this.cardObjectsInHand[2].getRankName())
|| (this.cardObjectsInHand[2].getRankName() == this.cardObjectsInHand[3].getRankName());
};
// EFFECTS: returns true if cardHand is a straight flush, false otherwise
Hand.prototype.isStraightFlush = function () {
// COMMENTS: check if played cards is proper length
if (this.numberCardsInHand != 5)
return false;
return this.isFlush() && this.isStraight();
};
// EFFECTS: returns true if this.cardObjectsInHand is valid start to event, false otherwise
Hand.prototype.isValidStart = function () {
switch (this.numberCardsInHand) {
case 1:
return this.isSingleCard();
case 2:
return this.isPair();
case 3:
return this.isThreeOfAKind();
case 5:
if (this.isStraight())
return true;
if (this.isFlush())
return true;
if (this.isHouse())
return true;
if (this.isFourOfAKind())
return true;
default:
return false;
}
};
// REQUIRES: otherHand is a Hand object and this Hand is valid hand
// EFFECTS: returns true if this Hand is weaker, or beaten by, otherHand. false otherwise
Hand.prototype.isBeatenBy = function (otherHand) {
if (!otherHand.isValidStart())
return false;
switch (this.numberCardsInHand) {
case 1:
if (otherHand.numberCardsInHand != 1)
return false;
if (!otherHand.isSingleCard())
return false;
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[0], this.cardObjectsInHand[0]) == 1;
case 2:
if (otherHand.numberCardsInHand != 2)
return false;
if (!otherHand.isPair())
return false;
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[0], this.cardObjectsInHand[0]) == 1;
case 3:
if (otherHand.numberCardsInHand != 3)
return false;
if (!otherHand.isThreeOfAKind())
return false;
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[0], this.cardObjectsInHand[0]) == 1;
case 5:
if (otherHand.numberCardsInHand != 5)
return false;
if (this.isStraight()) {
if (otherHand.isFlush() || otherHand.isHouse() || otherHand.isFourOfAKind())
return true;
else if (!otherHand.isStraight)
return false;
// COMMENTS: case where both are straight
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[4], this.cardObjectsInHand[4]) == 1;
}
if (this.isFlush()) {
if (otherHand.isHouse() || otherHand.isFourOfAKind())
return true;
else if (!otherHand.isFlush)
return false;
// COMMENTS: case where both are flush
return (Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[4], this.cardObjectsInHand[4]) == 1)
|| otherHand.isStraight();
}
if (this.isHouse()) {
if (otherHand.isFourOfAKind() || (otherHand.isStraightFlush()))
return true;
else if (!otherHand.isHouse)
return false;
// COMMENTS: case where both are house
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[2], this.cardObjectsInHand[2]) == 1;
}
if (this.isFourOfAKind()) {
if (otherHand.isStraightFlush())
return true;
else if (!otherHand.isFourOfAKind)
return false;
// COMMENTS: case where both are four of a kind
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[2], this.cardObjectsInHand[2]) == 1;
}
// COMMENTS: case where it is straight flush
if (!otherHand.isStraightFlush())
return false;
// COMMENTS: case where both are straight flushes
return Card_js_1.Card.compareCards(otherHand.cardObjectsInHand[4], this.cardObjectsInHand[4]) == 1;
default:
return false;
}
};
return Hand;
}());
exports.Hand = Hand;