-
Notifications
You must be signed in to change notification settings - Fork 0
/
Suit.ts
67 lines (58 loc) · 2.41 KB
/
Suit.ts
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
class Suit {
// COMMENTS: these are the member variables of Suit
private name: string;
static suitStrings: Array<string> = [
"Diamonds",
"Clubs",
"Hearts",
"Spades",
];
static suitStrengths: Map<string, number> = new Map([
["Diamonds", 0],
["Clubs", 1],
["Hearts", 2],
["Spades", 3],
]);
// REQUIRES: stringSuit is a string
// EXAMPLES: stringSuit = "Spades", stringRank2 = SUIT.SPADES
constructor(stringSuit: string) {
this.name = stringSuit;
}
// EFFECTS: returns a string of this suit
getSuitName(): string {
return this.name;
}
// REQUIRES: otherSuit is a Suit object
// EFFECTS: returns true if this object has same suit as otherCard, false otherwise
isSameSuitAs(otherSuit: Suit): boolean {
return this.name == otherSuit.name;
}
// REQUIRES: otherSuit is a Suit object
// EFFECTS: returns true if this object has better suit than otherCard, false otherwise
isBetterSuitThan(otherSuit: Suit): boolean {
// COMMENTS: these strength variables represent the importance of suits
const first_suit_strength = Suit.suitStrengths.get(this.name)!;
const second_suit_strength = Suit.suitStrengths.get(otherSuit.name)!;
return first_suit_strength > second_suit_strength;
}
// REQUIRES: otherSuit is a Suit object
// EFFECTS: if this < other_suit, return -1. if this > other_suit, return 1. return 0 otherwise
compareToTheSuit(otherSuit: Suit): number {
return Suit.compareTwoSuits(this, otherSuit);
}
// REQUIRES: firstSuit and secondSuit are Suit objects
// EFFECTS: if firstSuit < secondSuit, return -1. if firstSuit > secondSuit, return 1. return 0 otherwise
// USAGE: suitsVector.sort(Suit.compareTwoSuits);
static compareTwoSuits(firstSuit: Suit, secondSuit: Suit): number {
// COMMENTS: these strength variables represent the importance of suits
const firstSuitStrength = Suit.suitStrengths.get(firstSuit.getSuitName())!;
const secondSuitStrength = Suit.suitStrengths.get(secondSuit.getSuitName())!;
// COMMENTS: go compare
if (firstSuitStrength < secondSuitStrength) return -1;
else if (firstSuitStrength == secondSuitStrength) {
return 0;
}
return 1;
}
}
export {Suit}