Class: Rypto::Deck
- Inherits:
-
Object
- Object
- Rypto::Deck
- Defined in:
- lib/rypto.rb
Overview
A Krypto Deck contains 56 numeric cards. Three each of numbers 1 through 6, four each of 7 through 10, two each of 11 through 17, and one each of 18 through 25.
Instance Attribute Summary collapse
-
#cards ⇒ Array<Fixnum>
readonly
All the card values left in the deck.
Instance Method Summary collapse
-
#can_deal? ⇒ Boolean
Check if there are enough cards left to deal a new hand.
-
#deal_cards ⇒ Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)
Deal six raw card values from the deck.
-
#deal_hand ⇒ Hand
Deal a Hand from the deck.
-
#draw_card(card) ⇒ Fixnum
Draw a single card from the deck.
-
#initialize ⇒ Deck
constructor
Create a randomized deck of Krypto cards.
-
#size ⇒ Fixnum
Size of the deck.
Constructor Details
#initialize ⇒ Deck
Create a randomized deck of Krypto cards
67 68 69 |
# File 'lib/rypto.rb', line 67 def initialize @cards = ((1..6).to_a * 3 + (7..10).to_a * 4 + (11..17).to_a * 2 + (18..25).to_a).shuffle end |
Instance Attribute Details
#cards ⇒ Array<Fixnum> (readonly)
Returns all the card values left in the deck.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rypto.rb', line 63 class Deck attr_reader :cards # Create a randomized deck of Krypto cards def initialize @cards = ((1..6).to_a * 3 + (7..10).to_a * 4 + (11..17).to_a * 2 + (18..25).to_a).shuffle end # Draw a single card from the deck # # @param cards [Fixnum] the card value to draw # @return [Fixnum] the card value that was drawn # @raise [ArgumentError] if the card is not in the deck def draw_card(card) index = @cards.index card raise ArgumentError, "#{card} is not in the deck" if index.nil? @cards.delete_at index end # Deal six raw card values from the deck # # @return [Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)] a random hand from the deck # @raise [RuntimeError] if there are not enough cards to deal a full hand in the deck def deal_cards raise "Not enough cards to deal a hand" unless can_deal? @cards.slice!(0, 6) end # Deal a {Rypto::Hand} from the deck # @return [Hand] a random hand from the deck # @raise [RuntimeError] if there are not enough cards to deal a full hand in the deck def deal_hand cards = deal_cards Hand.new(cards[0, 5], cards[5]) end # Size of the deck # @return [Fixnum] number of cards left in the deck def size @cards.size end # Check if there are enough cards left to deal a new hand # @return [Boolean] def can_deal? size() >= 6 end end |
Instance Method Details
#can_deal? ⇒ Boolean
Check if there are enough cards left to deal a new hand
107 108 109 |
# File 'lib/rypto.rb', line 107 def can_deal? size() >= 6 end |
#deal_cards ⇒ Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)
Deal six raw card values from the deck
86 87 88 89 |
# File 'lib/rypto.rb', line 86 def deal_cards raise "Not enough cards to deal a hand" unless can_deal? @cards.slice!(0, 6) end |
#deal_hand ⇒ Hand
Deal a Hand from the deck
94 95 96 97 |
# File 'lib/rypto.rb', line 94 def deal_hand cards = deal_cards Hand.new(cards[0, 5], cards[5]) end |
#draw_card(card) ⇒ Fixnum
Draw a single card from the deck
76 77 78 79 80 |
# File 'lib/rypto.rb', line 76 def draw_card(card) index = @cards.index card raise ArgumentError, "#{card} is not in the deck" if index.nil? @cards.delete_at index end |
#size ⇒ Fixnum
Size of the deck
101 102 103 |
# File 'lib/rypto.rb', line 101 def size @cards.size end |