deck_of_cards_handler 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +76 -19
- data/lib/deck_of_cards_handler/card/card.rb +29 -0
- data/lib/deck_of_cards_handler/packet/packet.rb +28 -6
- data/lib/deck_of_cards_handler/version.rb +1 -1
- data/sorbet/config +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9bb4b9ad9eeda85ab0251a2d2a0d1030ad1713956582d204726402254d1b423b
|
|
4
|
+
data.tar.gz: 61b9a15a482800bc77b55f3e81402314ad1668fdfb0d8d1076bec4388ab05cb6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f10d2d29dabdd04098c987d992e06c303cb3a142097973aceb4f0e11cbfaf52feeea8d23418f488167fa645742accfb44ac6841efc71b84cada89b5362275865
|
|
7
|
+
data.tar.gz: aa06d832d4e713cade2a60cd8a06fc7d7da776e3f86b9b1ce62c6101448c91c930f4248efd7a82c84587add52de9d3d08c05205a623eca53dde2d7c5f4fcb95a
|
data/README.md
CHANGED
|
@@ -1,38 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Deck of cards handler
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
A ruby gem for simulating real-world deck handling: shuffle, cut, deal, cull
|
|
4
|
+
and more.
|
|
5
5
|
|
|
6
6
|
## Installation
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
Run the following terminal command:
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
```bash
|
|
13
|
-
bundle add deck_of_card_handler
|
|
10
|
+
```zsh
|
|
11
|
+
gem install deck_of_cards_handler
|
|
14
12
|
```
|
|
15
13
|
|
|
16
|
-
|
|
14
|
+
## Usage examples
|
|
15
|
+
|
|
16
|
+
<details>
|
|
17
|
+
<summary>Create the Mnemonica stack from Stay Stack</summary>
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require "deck_of_cards_handler"
|
|
21
|
+
|
|
22
|
+
# create a deck in stay stack order
|
|
23
|
+
clubs = Card.values.map { Card.new(suit: "C", value: _1) }
|
|
24
|
+
hearts = Card.values.map { Card.new(suit: "H", value: _1) }
|
|
25
|
+
diamonds = Card.values.map { Card.new(suit: "D", value: _1) }
|
|
26
|
+
spades = Card.values.map { Card.new(suit: "S", value: _1) }
|
|
27
|
+
deck = Packet.new(cards: [clubs, hearts, diamonds.reverse, spades.reverse].flatten)
|
|
28
|
+
|
|
29
|
+
# make 4 faro shuffles
|
|
30
|
+
4.times do
|
|
31
|
+
top_half = deck.cut(number: 26)
|
|
32
|
+
deck.faro(other_packet: top_half)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# reverse the first 26 cards
|
|
36
|
+
top_half = deck.cut(number: 26)
|
|
37
|
+
top_half.reverse
|
|
38
|
+
deck.cards = [top_half.cards, deck.cards].flatten
|
|
39
|
+
|
|
40
|
+
# faro the 18 first cards
|
|
41
|
+
top_half = deck.cut(number: 18)
|
|
42
|
+
deck.faro(other_packet: top_half)
|
|
43
|
+
|
|
44
|
+
# cut the 9D to the bottom
|
|
45
|
+
deck.cut_and_complete(number: 9)
|
|
46
|
+
# assign a position value to the cards
|
|
47
|
+
deck.set_cards_positions
|
|
17
48
|
|
|
18
|
-
```bash
|
|
19
|
-
gem install deck_of_card_handler
|
|
20
49
|
```
|
|
21
50
|
|
|
22
|
-
|
|
51
|
+
</details>
|
|
52
|
+
|
|
53
|
+
<details>
|
|
54
|
+
<summary>Distribute 5 hands of poker</summary>
|
|
23
55
|
|
|
24
|
-
|
|
56
|
+
```ruby
|
|
57
|
+
require "deck_of_cards_handler"
|
|
58
|
+
|
|
59
|
+
# create a full deck of cards
|
|
60
|
+
cards = []
|
|
61
|
+
Card.suits.each do |suit|
|
|
62
|
+
Card.values.each do |value|
|
|
63
|
+
cards << Card.new(suit:, value:)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
deck = Packet.new(cards:)
|
|
67
|
+
|
|
68
|
+
deck.shuffle
|
|
69
|
+
|
|
70
|
+
hands = deck.deal_into_piles(number_of_piles: 5, number_of_cards: 5)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
</details>
|
|
25
74
|
|
|
26
75
|
## Development
|
|
27
76
|
|
|
28
|
-
After checking out the repo, run
|
|
77
|
+
After checking out the repo, run:
|
|
29
78
|
|
|
30
|
-
|
|
79
|
+
```zsh
|
|
80
|
+
bin/setup
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
This installs dependencies.
|
|
31
84
|
|
|
32
|
-
|
|
85
|
+
Run the test suite:
|
|
33
86
|
|
|
34
|
-
|
|
87
|
+
```zsh
|
|
88
|
+
rake test
|
|
89
|
+
```
|
|
35
90
|
|
|
36
|
-
|
|
91
|
+
You can also open an interactive console for experimentation:
|
|
37
92
|
|
|
38
|
-
|
|
93
|
+
```zsh
|
|
94
|
+
bin/console
|
|
95
|
+
```
|
|
@@ -45,6 +45,35 @@ class Card
|
|
|
45
45
|
"#{value} of #{suit}"
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
+
sig { params(other: Card).returns(T::Boolean) }
|
|
49
|
+
def ==(other)
|
|
50
|
+
other.suit == suit && other.value == value
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
sig { returns(T::Array[Integer]) }
|
|
54
|
+
def rank
|
|
55
|
+
case value
|
|
56
|
+
when "A" then [1, 14]
|
|
57
|
+
when "J" then [11]
|
|
58
|
+
when "Q" then [12]
|
|
59
|
+
when "K" then [13]
|
|
60
|
+
else [value.to_i]
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
sig { params(other: Card).returns(Integer) }
|
|
65
|
+
def <=>(other)
|
|
66
|
+
# compare by all possible ranks (Ace flexible)
|
|
67
|
+
my_ranks = rank
|
|
68
|
+
other_ranks = other.rank
|
|
69
|
+
|
|
70
|
+
# check if any ranks overlap (Ace == 1 or 14)
|
|
71
|
+
return 0 if (my_ranks & other_ranks).any?
|
|
72
|
+
|
|
73
|
+
# otherwise compare by highest possible
|
|
74
|
+
T.must(my_ranks.max) <=> T.must(other_ranks.max)
|
|
75
|
+
end
|
|
76
|
+
|
|
48
77
|
class << self
|
|
49
78
|
extend T::Sig
|
|
50
79
|
sig { returns(T::Array[String]) }
|
|
@@ -47,18 +47,33 @@ class Packet
|
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
packet = Packet.new(cards:)
|
|
50
|
-
set_cards_positions
|
|
50
|
+
packet.set_cards_positions
|
|
51
51
|
|
|
52
52
|
packet
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
sig { params(string: String).returns(Packet) }
|
|
56
|
+
def build_from_string(string:) # rubocop:disable Metrics
|
|
57
|
+
content = string.split(",")
|
|
58
|
+
cards = []
|
|
59
|
+
cards_set = Set.new
|
|
60
|
+
|
|
61
|
+
content.each do |line|
|
|
62
|
+
value, suit = line.chomp.split(":")
|
|
63
|
+
raise StandardError unless value && suit
|
|
56
64
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
card = Card.new(suit:, value:)
|
|
66
|
+
card_string = card.to_s
|
|
67
|
+
raise StandardError, "Duplicate card. (#{card_string})" if cards_set.include?(card_string)
|
|
68
|
+
|
|
69
|
+
cards_set << card_string
|
|
70
|
+
cards << card
|
|
61
71
|
end
|
|
72
|
+
|
|
73
|
+
packet = Packet.new(cards:)
|
|
74
|
+
packet.set_cards_positions
|
|
75
|
+
|
|
76
|
+
packet
|
|
62
77
|
end
|
|
63
78
|
end
|
|
64
79
|
|
|
@@ -82,6 +97,13 @@ class Packet
|
|
|
82
97
|
self.cards = cards.reverse
|
|
83
98
|
end
|
|
84
99
|
|
|
100
|
+
sig { void }
|
|
101
|
+
def set_cards_positions
|
|
102
|
+
cards.each_with_index do |card, index|
|
|
103
|
+
card.position = index + 1
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
85
107
|
sig { returns(T::Array[String]) }
|
|
86
108
|
def to_s
|
|
87
109
|
cards.map(&:to_s)
|
data/sorbet/config
CHANGED