/
cards.ts
121 lines (118 loc) · 2.84 KB
/
cards.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
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
/**
* The rank of a card going from ace to king, where aces are `1`.
*
* @public
* @category Cards
*/
export enum Rank {
Ace = 1,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
}
/**
* A suit of cards, sorted in alphabetical order.
*
* @public
* @category Cards
*/
export enum Suit {
Clubs,
Diamonds,
Hearts,
Spades,
}
/**
* A playing card.
*
* @public
* @category Cards
*/
export type Card = {
rank: Rank;
suit: Suit;
};
/**
* Get a sorted deck of cards.
*
* Time complexity: _O(n)_
*
* Space complexity: _O(n)_
*
* @example
* ```js
* import { shuffle } from '@jonahsnider/util';
*
* const deck = newDeck();
* ```
*
* @returns A sorted deck of cards
*
* @public
* @category Cards
*/
export function newDeck(): Card[] {
return [
{rank: Rank.Ace, suit: Suit.Clubs},
{rank: Rank.Two, suit: Suit.Clubs},
{rank: Rank.Three, suit: Suit.Clubs},
{rank: Rank.Four, suit: Suit.Clubs},
{rank: Rank.Five, suit: Suit.Clubs},
{rank: Rank.Six, suit: Suit.Clubs},
{rank: Rank.Seven, suit: Suit.Clubs},
{rank: Rank.Eight, suit: Suit.Clubs},
{rank: Rank.Nine, suit: Suit.Clubs},
{rank: Rank.Ten, suit: Suit.Clubs},
{rank: Rank.Jack, suit: Suit.Clubs},
{rank: Rank.Queen, suit: Suit.Clubs},
{rank: Rank.King, suit: Suit.Clubs},
{rank: Rank.Ace, suit: Suit.Diamonds},
{rank: Rank.Two, suit: Suit.Diamonds},
{rank: Rank.Three, suit: Suit.Diamonds},
{rank: Rank.Four, suit: Suit.Diamonds},
{rank: Rank.Five, suit: Suit.Diamonds},
{rank: Rank.Six, suit: Suit.Diamonds},
{rank: Rank.Seven, suit: Suit.Diamonds},
{rank: Rank.Eight, suit: Suit.Diamonds},
{rank: Rank.Nine, suit: Suit.Diamonds},
{rank: Rank.Ten, suit: Suit.Diamonds},
{rank: Rank.Jack, suit: Suit.Diamonds},
{rank: Rank.Queen, suit: Suit.Diamonds},
{rank: Rank.King, suit: Suit.Diamonds},
{rank: Rank.Ace, suit: Suit.Hearts},
{rank: Rank.Two, suit: Suit.Hearts},
{rank: Rank.Three, suit: Suit.Hearts},
{rank: Rank.Four, suit: Suit.Hearts},
{rank: Rank.Five, suit: Suit.Hearts},
{rank: Rank.Six, suit: Suit.Hearts},
{rank: Rank.Seven, suit: Suit.Hearts},
{rank: Rank.Eight, suit: Suit.Hearts},
{rank: Rank.Nine, suit: Suit.Hearts},
{rank: Rank.Ten, suit: Suit.Hearts},
{rank: Rank.Jack, suit: Suit.Hearts},
{rank: Rank.Queen, suit: Suit.Hearts},
{rank: Rank.King, suit: Suit.Hearts},
{rank: Rank.Ace, suit: Suit.Spades},
{rank: Rank.Two, suit: Suit.Spades},
{rank: Rank.Three, suit: Suit.Spades},
{rank: Rank.Four, suit: Suit.Spades},
{rank: Rank.Five, suit: Suit.Spades},
{rank: Rank.Six, suit: Suit.Spades},
{rank: Rank.Seven, suit: Suit.Spades},
{rank: Rank.Eight, suit: Suit.Spades},
{rank: Rank.Nine, suit: Suit.Spades},
{rank: Rank.Ten, suit: Suit.Spades},
{rank: Rank.Jack, suit: Suit.Spades},
{rank: Rank.Queen, suit: Suit.Spades},
{rank: Rank.King, suit: Suit.Spades},
];
}