deck_of_cards_handler 0.1.91 → 0.1.92
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/CHANGELOG.md +5 -0
- data/README.md +69 -41
- data/lib/deck_of_cards_handler/packet/packet.rb +11 -0
- data/lib/deck_of_cards_handler/packet/shuffles.rb +18 -0
- data/lib/deck_of_cards_handler/version.rb +1 -1
- 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: a76a16db0441bf9921dcf676cb1fbcf23f7bb674ee0628d146b9206955d138ff
|
4
|
+
data.tar.gz: eb6fe424f4cc1e08ac79494e08b9807c427901b78f86e202c351113d3ea7e1d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 349564212186a705e3607c92ee86a49d62882f66625b04793e072114ce16b1d8914a5a312efbc993037375cf0a16ad564ed2c55f0c365b85911e79d9caabb03b
|
7
|
+
data.tar.gz: 702af9029ac27a8ff51a828efc11f364d8552c7cfa55ac77200239ba51d68fcc28e03b02e99354ec4ade7067508b592960ff9844c51e0b62cd68034425583337
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://badge.fury.io/rb/deck_of_cards_handler) [](https://github.com/simonbernard2/deck_of_cards/actions/workflows/ruby.yml)
|
1
|
+
[](https://badge.fury.io/rb/deck_of_cards_handler) [](https://github.com/simonbernard2/deck_of_cards/actions/workflows/ruby.yml)
|
2
2
|
|
3
3
|
# Deck of cards handler
|
4
4
|
|
@@ -13,6 +13,70 @@ Run the following terminal command:
|
|
13
13
|
gem install deck_of_cards_handler
|
14
14
|
```
|
15
15
|
|
16
|
+
## Quickstart
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require "deck_of_cards_handler"
|
20
|
+
|
21
|
+
# Cards
|
22
|
+
c = Card.new(suit: "S", value: "A")
|
23
|
+
c.red? # => false
|
24
|
+
c.black? # => true
|
25
|
+
c.spades? # => true
|
26
|
+
c.hearts? # => false
|
27
|
+
c.to_s # => "A of S"
|
28
|
+
c.rank # => 14 (Ace high)
|
29
|
+
|
30
|
+
Card.suits # => ["C","D","H","S"]
|
31
|
+
Card.values # => ["A","2",...,"10","J","Q","K"]
|
32
|
+
|
33
|
+
# Packets (decks or piles)
|
34
|
+
deck = Packet.new # empty unless you pass cards
|
35
|
+
one_card = Packet.new(cards: [c])
|
36
|
+
full = Packet.build_from_text_file(file_path: "data/mnemonica.txt")
|
37
|
+
five = Packet.build_from_string(string: "A:C, K:D, Q:H, J:S, 10:C")
|
38
|
+
|
39
|
+
full.size # => 52
|
40
|
+
full.shuffle # in-place shuffle
|
41
|
+
full.reverse # in-place reverse
|
42
|
+
top = full.top_deal # remove and return top card (Card)
|
43
|
+
sec = full.second_deal # remove and return second card (Card)
|
44
|
+
bot = full.bottom_deal # remove and return bottom card (Card)
|
45
|
+
|
46
|
+
piles = full.deal_into_piles(number_of_piles: 4, number_of_cards: 5)
|
47
|
+
# => [[Card,...],[...],[...],[...]]
|
48
|
+
|
49
|
+
# Reassemble piles (all return Packet)
|
50
|
+
full.reassemble_left_to_right_on_top(piles)
|
51
|
+
|
52
|
+
# Packet ↔︎ Poker hand
|
53
|
+
hand = five.to_poker_hand # => a PokerHands::PokerHand subclass
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
## Shuffling
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
# Perfect interleaving (Faro)
|
61
|
+
top_half = deck.cut(number: 26)
|
62
|
+
deck.faro(other_packet: top_half)
|
63
|
+
|
64
|
+
# Imperfect riffle shuffle
|
65
|
+
left_half = deck.cut(number: 26)
|
66
|
+
deck.riffle_shuffle(other_packet: left_half)
|
67
|
+
|
68
|
+
# Random shuffle
|
69
|
+
deck.shuffle
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
## Convert Packets to Poker Hand
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
five = Packet.build_from_string(string: "A:C, K:D, Q:H, J:S, 10:C")
|
77
|
+
hand = five.to_poker_hand # => PokerHands::Straight
|
78
|
+
```
|
79
|
+
|
16
80
|
## Usage examples
|
17
81
|
|
18
82
|
<details>
|
@@ -59,43 +123,7 @@ gem install deck_of_cards_handler
|
|
59
123
|
#<Card:0x000000012ae2c9f0 @position=5, @suit="H", @value="4">,
|
60
124
|
#<Card:0x000000012ae2c298 @position=6, @suit="D", @value="6">,
|
61
125
|
#<Card:0x000000012ae2df08 @position=7, @suit="C", @value="A">,
|
62
|
-
|
63
|
-
#<Card:0x000000012ae2ce00 @position=9, @suit="C", @value="9">,
|
64
|
-
#<Card:0x000000012ae2d260 @position=10, @suit="C", @value="2">,
|
65
|
-
#<Card:0x000000012ae2c630 @position=11, @suit="H", @value="Q">,
|
66
|
-
#<Card:0x000000012ae2c400 @position=12, @suit="D", @value="3">,
|
67
|
-
#<Card:0x000000012ae2b960 @position=13, @suit="S", @value="Q">,
|
68
|
-
#<Card:0x000000012ae2c810 @position=14, @suit="H", @value="8">,
|
69
|
-
#<Card:0x000000012ae2cf68 @position=15, @suit="C", @value="6">,
|
70
|
-
#<Card:0x000000012ae2cfe0 @position=16, @suit="C", @value="5">,
|
71
|
-
#<Card:0x000000012ae2c798 @position=17, @suit="H", @value="9">,
|
72
|
-
#<Card:0x000000012ae2b8e8 @position=18, @suit="S", @value="K">,
|
73
|
-
#<Card:0x000000012ae2c478 @position=19, @suit="D", @value="2">,
|
74
|
-
#<Card:0x000000012ae2c6a8 @position=20, @suit="H", @value="J">,
|
75
|
-
#<Card:0x000000012ae2d0d0 @position=21, @suit="C", @value="3">,
|
76
|
-
#<Card:0x000000012ae2ce78 @position=22, @suit="C", @value="8">,
|
77
|
-
#<Card:0x000000012ae2c900 @position=23, @suit="H", @value="6">,
|
78
|
-
#<Card:0x000000012ae2ba50 @position=24, @suit="S", @value="10">,
|
79
|
-
#<Card:0x000000012ae2c310 @position=25, @suit="D", @value="5">,
|
80
|
-
#<Card:0x000000012ae2bf50 @position=26, @suit="D", @value="K">,
|
81
|
-
#<Card:0x000000012ae2be10 @position=27, @suit="S", @value="2">,
|
82
|
-
#<Card:0x000000012ae2ca68 @position=28, @suit="H", @value="3">,
|
83
|
-
#<Card:0x000000012ae2c1a8 @position=29, @suit="D", @value="8">,
|
84
|
-
#<Card:0x000000012ae2bca8 @position=30, @suit="S", @value="5">,
|
85
|
-
#<Card:0x000000012ae2cc20 @position=31, @suit="C", @value="K">,
|
86
|
-
#<Card:0x000000012ae2c040 @position=32, @suit="D", @value="J">,
|
87
|
-
#<Card:0x000000012ae2bb40 @position=33, @suit="S", @value="8">,
|
88
|
-
#<Card:0x000000012ae2cd88 @position=34, @suit="C", @value="10">,
|
89
|
-
#<Card:0x000000012ae2c5b8 @position=35, @suit="H", @value="K">,
|
90
|
-
#<Card:0x000000012ae2b9d8 @position=36, @suit="S", @value="J">,
|
91
|
-
#<Card:0x000000012ae2cef0 @position=37, @suit="C", @value="7">,
|
92
|
-
#<Card:0x000000012ae2c720 @position=38, @suit="H", @value="10">,
|
93
|
-
#<Card:0x000000012ae2c4f0 @position=39, @suit="D", @value="A">,
|
94
|
-
#<Card:0x000000012ae2d058 @position=40, @suit="C", @value="4">,
|
95
|
-
#<Card:0x000000012ae2c888 @position=41, @suit="H", @value="7">,
|
96
|
-
#<Card:0x000000012ae2c388 @position=42, @suit="D", @value="4">,
|
97
|
-
#<Card:0x000000012ae2be88 @position=43, @suit="S", @value="A">,
|
98
|
-
#<Card:0x000000012ae2bac8 @position=44, @suit="S", @value="9">,
|
126
|
+
#...
|
99
127
|
#<Card:0x000000012ae2cd10 @position=45, @suit="C", @value="J">,
|
100
128
|
#<Card:0x000000012ae2bfc8 @position=46, @suit="D", @value="Q">,
|
101
129
|
#<Card:0x000000012ae2bbb8 @position=47, @suit="S", @value="7">,
|
@@ -125,10 +153,10 @@ gem install deck_of_cards_handler
|
|
125
153
|
|
126
154
|
deck.shuffle
|
127
155
|
|
128
|
-
|
156
|
+
piles = deck.deal_into_piles(number_of_piles: 5, number_of_cards: 5)
|
129
157
|
|
130
|
-
hands.map(&:
|
131
|
-
# => [
|
158
|
+
hands = piles.map(&:to_poker_hand)
|
159
|
+
# => [#PokerHands::OnePair, [...],[...],[...],[...]]
|
132
160
|
```
|
133
161
|
|
134
162
|
</details>
|
@@ -10,6 +10,7 @@ class Packet
|
|
10
10
|
require "deck_of_cards_handler/packet/cuts"
|
11
11
|
include Deals
|
12
12
|
include Cuts
|
13
|
+
include Shuffles
|
13
14
|
|
14
15
|
sig { returns(T::Array[Card]) }
|
15
16
|
attr_accessor :cards
|
@@ -93,6 +94,16 @@ class Packet
|
|
93
94
|
self.cards = cards.reverse
|
94
95
|
end
|
95
96
|
|
97
|
+
# Moves a card from position A to position B
|
98
|
+
# The first card position IS 1, NOT 0 as in a traditional array
|
99
|
+
sig { params(from: Integer, to: Integer).void }
|
100
|
+
def cull(from:, to:)
|
101
|
+
raise ArgumentError, "`from` must be in range with 1..#{size}" if from <= 0 || from > size
|
102
|
+
raise ArgumentError, "`to` must be in range with 1..#{size}" if to <= 0 || to > size
|
103
|
+
|
104
|
+
cards.insert(to - 1, T.must(cards.delete_at(from - 1)))
|
105
|
+
end
|
106
|
+
|
96
107
|
sig { void }
|
97
108
|
def set_cards_positions
|
98
109
|
cards.each_with_index do |card, index|
|
@@ -2,6 +2,24 @@
|
|
2
2
|
# typed: strict
|
3
3
|
|
4
4
|
module Shuffles
|
5
|
+
extend T::Helpers
|
6
|
+
extend T::Sig
|
7
|
+
|
8
|
+
requires_ancestor { Packet }
|
9
|
+
|
10
|
+
sig { void }
|
11
|
+
def overhand_shuffle
|
12
|
+
piles = []
|
13
|
+
|
14
|
+
until size.zero?
|
15
|
+
chunk_size = Kernel.rand(1..8)
|
16
|
+
chunk_size = size if chunk_size > size
|
17
|
+
piles << cut(number: chunk_size)
|
18
|
+
end
|
19
|
+
|
20
|
+
reassemble_right_to_left_on_top(piles)
|
21
|
+
end
|
22
|
+
|
5
23
|
class << self
|
6
24
|
extend T::Sig
|
7
25
|
|