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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5dfda17b6f6dc46b95f0cf7eb5844376be912a4
4
- data.tar.gz: b0c24d211440037466dddf4c20a1557b756ab91d
3
+ metadata.gz: e982dde9bf2792a95c988d37baa091949c27d651
4
+ data.tar.gz: 1f1d702824f8f6d4380d0e86f176a8097e2b823e
5
5
  SHA512:
6
- metadata.gz: 508755c9cde2afcfe1c10b1279092045c04b5fe5d6e7dfb6364c1083b20e25c2e421de79d75d31e620cb7dc6c32476b52938c84109dbea756612da5be15c5c18
7
- data.tar.gz: fb5ac4aba6c758ca8aac9caa140e142056f0c65197a16fae40e9ce1e57618e752fa452d8b5ab371121de3487d291ac8b83ff67ebcbd1a43b304697753f3af740
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/[USERNAME]/simple_playing_cards.
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
- RANKS = %w[A 2 3 4 5 6 7 8 9 10 J K Q]
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
- def initialize(rank, suit)
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
@@ -1,3 +1,3 @@
1
1
  module SimplePlayingCards
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  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.0
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-07 00:00:00.000000000 Z
11
+ date: 2017-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler