fiftytwo 0.0.2 → 0.0.3
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 +46 -8
- data/lib/fiftytwo/has_cards.rb +11 -5
- data/lib/fiftytwo/version.rb +1 -1
- data/spec/lib/fiftytwo/deck_spec.rb +27 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5c1752b761a6a418abc5635ece62d1dde656012ca5b548e107ab633a915d868
|
4
|
+
data.tar.gz: f044e63524c51c7f25987f8a682e5876157bda0bbb10c2c3b7959535afbe88c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e413c3f029b93322be3ed98725dc99dc7621ddc2bd53f9e3d18abb417419be200b53d055620e3dbffce932e691c56dd5466d097a4ffb24800d7c20a8dfc34df
|
7
|
+
data.tar.gz: a1db0d51936d8c0d20b4bf25ac1f9d9f6525e509cbe557a5b327c582c22d32831c2e0fb460a96ddf30a0826299c9e2039759937def354e1fa7851483c0f5d081
|
data/README.md
CHANGED
@@ -116,15 +116,15 @@ deck.deal([my_hand, your_hand], 5)
|
|
116
116
|
# => "Deck has 42 cards, I have 5 cards, you have 5 cards"
|
117
117
|
|
118
118
|
puts my_hand.render, your_hand.render # by the way renderings are colored red/black in your terminal, just like the suit!
|
119
|
-
3♦ 4♠ 5♦ 7♥ 2♣
|
120
|
-
6♠ A♦ 5♠ 10♦ Q♣
|
119
|
+
# 3♦ 4♠ 5♦ 7♥ 2♣
|
120
|
+
# 6♠ A♦ 5♠ 10♦ Q♣
|
121
121
|
```
|
122
122
|
|
123
123
|
Hands, just like the deck, can be shuffled, sorted, searched, etc:
|
124
124
|
```ruby
|
125
125
|
your_hand.sort!
|
126
126
|
puts your_hand.render
|
127
|
-
5♠ 6♠ 10♦ Q♣ A♦
|
127
|
+
# 5♠ 6♠ 10♦ Q♣ A♦
|
128
128
|
|
129
129
|
your_hand.aces.count
|
130
130
|
# => 1
|
@@ -132,15 +132,15 @@ your_hand.aces.count
|
|
132
132
|
|
133
133
|
Pass your cards around:
|
134
134
|
```ruby
|
135
|
-
my_hand.transfer("4S", your_hand)
|
135
|
+
my_hand.transfer("4S", your_hand) # try an array of card identifiers, too!
|
136
136
|
puts my_hand.render, your_hand.render
|
137
|
-
3♦ 5♦ 7♥ 2♣
|
138
|
-
5♠ 6♠ 10♦ Q♣ A♦ 4♠
|
137
|
+
# 3♦ 5♦ 7♥ 2♣
|
138
|
+
# 5♠ 6♠ 10♦ Q♣ A♦ 4♠
|
139
139
|
|
140
140
|
your_hand.transfer("QC") # goes back to the deck
|
141
141
|
puts my_hand.render, your_hand.render
|
142
|
-
3♦ 5♦ 7♥ 2♣
|
143
|
-
5♠ 6♠ 10♦ A♦ 4♠
|
142
|
+
# 3♦ 5♦ 7♥ 2♣
|
143
|
+
# 5♠ 6♠ 10♦ A♦ 4♠
|
144
144
|
|
145
145
|
deck.count
|
146
146
|
# => 43
|
@@ -155,6 +155,44 @@ your_hand.release
|
|
155
155
|
# => "Deck has 52 cards, I have 0 cards, you have 0 cards"
|
156
156
|
```
|
157
157
|
|
158
|
+
## Example: 5 card draw poker
|
159
|
+
_Reminder, we are implementing a deck of cards, and the operations you can perform with that deck. Evaluation of hands is dependent upon some
|
160
|
+
particular game, and is outside the scope of this gem!_
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
require "fiftytwo"
|
164
|
+
|
165
|
+
deck = FiftyTwo::Deck.standard
|
166
|
+
deck.shuffle!
|
167
|
+
|
168
|
+
my_hand = FiftyTwo::Hand.new
|
169
|
+
your_hand = FiftyTwo::Hand.new
|
170
|
+
discard = FiftyTwo::Hand.new
|
171
|
+
|
172
|
+
deck.deal([your_hand, my_hand], 5)
|
173
|
+
|
174
|
+
[my_hand, your_hand].each(&:sort!)
|
175
|
+
puts "you: #{your_hand.render}", "me: #{my_hand.render}"
|
176
|
+
# you: 6♦ 6♠ 7♠ K♣ A♦
|
177
|
+
# me: 2♦ 5♥ 8♠ 9♥ 10♦
|
178
|
+
|
179
|
+
your_hand.transfer(%w[7s kc], discard)
|
180
|
+
deck.deal(your_hand, 2)
|
181
|
+
|
182
|
+
my_hand.transfer(%w[2D 5H], discard)
|
183
|
+
deck.deal(my_hand, 2)
|
184
|
+
|
185
|
+
[my_hand, your_hand].each(&:sort!)
|
186
|
+
puts "you: #{your_hand.render}", "me: #{my_hand.render}"
|
187
|
+
# you: 4♠ 6♦ 6♠ K♦ A♦
|
188
|
+
# me: 2♣ 4♣ 8♠ 9♥ 10♦
|
189
|
+
|
190
|
+
puts "Congratulations, you win!"
|
191
|
+
|
192
|
+
[my_hand, your_hand, discard].each(&:release)
|
193
|
+
deck.shuffle!
|
194
|
+
```
|
195
|
+
|
158
196
|
# Problems?
|
159
197
|
Please submit an [issue](https://github.com/kevinstuffandthings/fiftytwo/issues).
|
160
198
|
We'll figure out how to get you up and running with FiftyTwo as smoothly as possible.
|
data/lib/fiftytwo/has_cards.rb
CHANGED
@@ -24,11 +24,8 @@ module FiftyTwo
|
|
24
24
|
cards.find { |c| c.identifier == identifier.upcase }
|
25
25
|
end
|
26
26
|
|
27
|
-
def transfer(
|
28
|
-
|
29
|
-
raise CardUnavailableError if card.nil?
|
30
|
-
raise CardUnavailableError unless cards.include?(card)
|
31
|
-
(destination || card.deck) << cards.delete(card)
|
27
|
+
def transfer(cards, destination = nil)
|
28
|
+
gather(cards).map { |c| (destination || c.deck) << self.cards.delete(c) }
|
32
29
|
end
|
33
30
|
|
34
31
|
FiftyTwo::Suit::ALL.each do |suit|
|
@@ -48,5 +45,14 @@ module FiftyTwo
|
|
48
45
|
def select(identifier)
|
49
46
|
self.class.new(cards.select { |c| c.send("#{identifier}?") })
|
50
47
|
end
|
48
|
+
|
49
|
+
def gather(cards)
|
50
|
+
Array(cards).map do |card|
|
51
|
+
card = locate(card) unless card.is_a?(FiftyTwo::Card)
|
52
|
+
raise CardUnavailableError if card.nil?
|
53
|
+
raise CardUnavailableError unless self.cards.include?(card)
|
54
|
+
card
|
55
|
+
end
|
56
|
+
end
|
51
57
|
end
|
52
58
|
end
|
data/lib/fiftytwo/version.rb
CHANGED
@@ -7,6 +7,7 @@ module FiftyTwo
|
|
7
7
|
before(:each) do
|
8
8
|
minideck << FiftyTwo::Card.new(minideck, FiftyTwo::Rank::THREE, FiftyTwo::Suit::CLUBS)
|
9
9
|
minideck << FiftyTwo::Card.new(minideck, FiftyTwo::Rank::NINE, FiftyTwo::Suit::DIAMONDS)
|
10
|
+
minideck << FiftyTwo::Card.new(minideck, FiftyTwo::Rank::TEN, FiftyTwo::Suit::DIAMONDS)
|
10
11
|
end
|
11
12
|
|
12
13
|
it "is a deck, made up of cards" do
|
@@ -117,17 +118,22 @@ module FiftyTwo
|
|
117
118
|
let(:subject) { minideck }
|
118
119
|
|
119
120
|
it "takes cards in order from the deck" do
|
121
|
+
expect(subject.count).to eq 3
|
122
|
+
|
123
|
+
card = subject.draw
|
120
124
|
expect(subject.count).to eq 2
|
125
|
+
expect(card).to be_a FiftyTwo::Card
|
126
|
+
expect(card.identifier).to eq "3C"
|
121
127
|
|
122
128
|
card = subject.draw
|
123
129
|
expect(subject.count).to eq 1
|
124
130
|
expect(card).to be_a FiftyTwo::Card
|
125
|
-
expect(card.identifier).to eq "
|
131
|
+
expect(card.identifier).to eq "9D"
|
126
132
|
|
127
133
|
card = subject.draw
|
128
134
|
expect(subject.count).to eq 0
|
129
135
|
expect(card).to be_a FiftyTwo::Card
|
130
|
-
expect(card.identifier).to eq "
|
136
|
+
expect(card.identifier).to eq "10D"
|
131
137
|
|
132
138
|
card = subject.draw
|
133
139
|
expect(subject.count).to eq 0
|
@@ -174,22 +180,34 @@ module FiftyTwo
|
|
174
180
|
let(:hand) { FiftyTwo::Hand.new }
|
175
181
|
|
176
182
|
it "has a deck with 2 cards, and a hand with none" do
|
177
|
-
expect(subject.count).to eq
|
183
|
+
expect(subject.count).to eq 3
|
178
184
|
expect(hand.count).to eq 0
|
179
185
|
end
|
180
186
|
|
181
187
|
it "can transfer a card by object" do
|
182
188
|
subject.transfer(subject[1], hand)
|
183
|
-
expect(subject.count).to eq
|
189
|
+
expect(subject.count).to eq 2
|
184
190
|
expect(hand.count).to eq 1
|
185
191
|
end
|
186
192
|
|
187
193
|
it "can transfer a card by identifier" do
|
188
194
|
subject.transfer("3C", hand)
|
189
|
-
expect(subject.count).to eq
|
195
|
+
expect(subject.count).to eq 2
|
190
196
|
expect(hand.count).to eq 1
|
191
197
|
end
|
192
198
|
|
199
|
+
it "can transfer multiple cards" do
|
200
|
+
subject.transfer(%w[3C 10D], hand)
|
201
|
+
expect(subject.count).to eq 1
|
202
|
+
expect(hand.count).to eq 2
|
203
|
+
end
|
204
|
+
|
205
|
+
it "will not transfer any and raise an error if any one card reference is invalid" do
|
206
|
+
expect { subject.transfer(%w[3C 10K], hand) }.to raise_error FiftyTwo::HasCards::CardUnavailableError
|
207
|
+
expect(subject.count).to eq 3
|
208
|
+
expect(hand.count).to eq 0
|
209
|
+
end
|
210
|
+
|
193
211
|
context "missing card" do
|
194
212
|
let(:missing_card) { FiftyTwo::Card.new(subject, FiftyTwo::Rank::THREE, FiftyTwo::Suit::HEARTS) }
|
195
213
|
|
@@ -206,14 +224,14 @@ module FiftyTwo
|
|
206
224
|
let(:card) { subject[1] }
|
207
225
|
before(:each) { subject.transfer(card, hand) }
|
208
226
|
|
209
|
-
it "starts with a card in the hand and
|
210
|
-
expect(subject.count).to eq
|
227
|
+
it "starts with a card in the hand and 2 in the deck" do
|
228
|
+
expect(subject.count).to eq 2
|
211
229
|
expect(hand.count).to eq 1
|
212
230
|
end
|
213
231
|
|
214
232
|
it "can transfer a card from a hand back to the bottom of its originating deck" do
|
215
233
|
hand.transfer(card)
|
216
|
-
expect(subject.count).to eq
|
234
|
+
expect(subject.count).to eq 3
|
217
235
|
expect(hand.count).to eq 0
|
218
236
|
expect(subject.last).to eq card
|
219
237
|
end
|
@@ -230,7 +248,7 @@ module FiftyTwo
|
|
230
248
|
end
|
231
249
|
|
232
250
|
it "renders the set of cards in this deck" do
|
233
|
-
expect(subject.render).to eq "CARD0 CARD1"
|
251
|
+
expect(subject.render).to eq "CARD0 CARD1 CARD2"
|
234
252
|
end
|
235
253
|
end
|
236
254
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fiftytwo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin McDonald
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|