simple_playing_cards 0.1.0 → 0.1.1
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 +29 -2
- data/lib/simple_playing_cards/card.rb +35 -3
- data/lib/simple_playing_cards/deck.rb +5 -4
- data/lib/simple_playing_cards/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e982dde9bf2792a95c988d37baa091949c27d651
|
|
4
|
+
data.tar.gz: 1f1d702824f8f6d4380d0e86f176a8097e2b823e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: da1b4770e3f02eeccef9e32cca9a49246fee33945d42a8c44bf0d269923395377d64c1cfe9a73fc339a2b31aff2f51f9fa4215c0ab8562773aff0f6021652f7a
|
|
7
|
+
data.tar.gz: 9c4f7bd3824512afaf27dd0d1d8b6ce7dc891f1c2f82f8a8eae5786e6cf2d0f661f9fbbc737431b4003b9b809dc7a11f1f5dbda75b55c6874022fafab9e7ac6c
|
data/README.md
CHANGED
|
@@ -34,13 +34,40 @@ Deal some cards:
|
|
|
34
34
|
|
|
35
35
|
Get the card details:
|
|
36
36
|
|
|
37
|
-
```
|
|
37
|
+
```ruby
|
|
38
38
|
card = hand.first
|
|
39
39
|
card.rank #=> "A"
|
|
40
40
|
card.suit #=> "Spades"
|
|
41
41
|
card.name #=> "A of Spades"
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
+
Every card has a numeric value and comparisons can be made based on that.
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
card_a = SimplePlayingCards::Card.new('10', 'Hearts')
|
|
48
|
+
card_b = SimplePlayingCards::Card.new('8', 'Spades')
|
|
49
|
+
|
|
50
|
+
card_a < card_b #=> false
|
|
51
|
+
card_a > card_b #=> true
|
|
52
|
+
card_b == card_a #=> false
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Aces default to a numerical value of `1`. In order to set aces
|
|
56
|
+
to high you can pass an options hash to the `Deck` or `Card`.
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
options = { 'aces_high' => true }
|
|
60
|
+
deck = SimplePlayingCards.deck.new(options)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Options Hash
|
|
64
|
+
|
|
65
|
+
The `Deck` and `Card` objects can be initialized with an options hash.
|
|
66
|
+
```ruby
|
|
67
|
+
# Available options
|
|
68
|
+
{ 'aces_high' => true } # sets the value of all aces to 14
|
|
69
|
+
```
|
|
70
|
+
|
|
44
71
|
## Development
|
|
45
72
|
|
|
46
73
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -49,7 +76,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
49
76
|
|
|
50
77
|
## Contributing
|
|
51
78
|
|
|
52
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
79
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/k-p-jones/simple_playing_cards.
|
|
53
80
|
|
|
54
81
|
|
|
55
82
|
## License
|
|
@@ -1,21 +1,53 @@
|
|
|
1
1
|
module SimplePlayingCards
|
|
2
2
|
class Card
|
|
3
|
-
|
|
3
|
+
include Comparable
|
|
4
|
+
RANKS = %w[Ace 2 3 4 5 6 7 8 9 10 Jack King Queen]
|
|
4
5
|
SUITS = %w[Spades Hearts Diamonds Clubs]
|
|
5
6
|
|
|
6
7
|
attr_reader :rank, :suit
|
|
7
|
-
|
|
8
|
+
attr_accessor :value, :options
|
|
9
|
+
|
|
10
|
+
def initialize(rank, suit, options = {})
|
|
8
11
|
validate_inputs(rank, suit)
|
|
12
|
+
@options = options
|
|
9
13
|
@rank = rank
|
|
10
14
|
@suit = suit
|
|
15
|
+
@value = assign_value
|
|
11
16
|
end
|
|
12
17
|
|
|
13
18
|
def name
|
|
14
19
|
"#{rank} of #{suit}"
|
|
15
20
|
end
|
|
16
21
|
|
|
22
|
+
def <=> (card)
|
|
23
|
+
if self.value < card.value
|
|
24
|
+
-1
|
|
25
|
+
elsif self.value > card.value
|
|
26
|
+
1
|
|
27
|
+
else
|
|
28
|
+
0
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
17
32
|
private
|
|
18
33
|
|
|
34
|
+
def assign_value
|
|
35
|
+
if rank.to_i > 0
|
|
36
|
+
rank.to_i
|
|
37
|
+
else
|
|
38
|
+
case rank
|
|
39
|
+
when 'Jack'
|
|
40
|
+
11
|
|
41
|
+
when 'Queen'
|
|
42
|
+
12
|
|
43
|
+
when 'King'
|
|
44
|
+
13
|
|
45
|
+
when 'Ace'
|
|
46
|
+
options['aces_high'] ? 14 : 1
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
19
51
|
def validate_inputs(rank, suit)
|
|
20
52
|
errors = []
|
|
21
53
|
errors << "Invalid argument type. Rank and Suit most both be strings!" unless rank.is_a?(String) && suit.is_a?(String)
|
|
@@ -25,4 +57,4 @@ module SimplePlayingCards
|
|
|
25
57
|
raise ArgumentError.new(errors.join(" ")) unless errors.empty?
|
|
26
58
|
end
|
|
27
59
|
end
|
|
28
|
-
end
|
|
60
|
+
end
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
module SimplePlayingCards
|
|
2
2
|
class Deck
|
|
3
|
-
attr_accessor :cards
|
|
4
|
-
def initialize
|
|
3
|
+
attr_accessor :cards, :options
|
|
4
|
+
def initialize(options = {})
|
|
5
5
|
@cards = []
|
|
6
|
+
@options = options
|
|
6
7
|
build_deck
|
|
7
8
|
end
|
|
8
9
|
|
|
@@ -19,9 +20,9 @@ module SimplePlayingCards
|
|
|
19
20
|
def build_deck
|
|
20
21
|
Card::SUITS.each do |suit|
|
|
21
22
|
Card::RANKS.each do |rank|
|
|
22
|
-
cards << Card.new(rank, suit)
|
|
23
|
+
cards << Card.new(rank, suit, options)
|
|
23
24
|
end
|
|
24
25
|
end
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
|
-
end
|
|
28
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_playing_cards
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ken Jones
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-08-
|
|
11
|
+
date: 2017-08-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|