class Rypto::Deck
A Krypto {Rypto::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.
@!attribute cards [r]
@return [Array<Fixnum>] all the card values left in the deck
Attributes
Public Class Methods
Create a randomized deck of Krypto cards
# 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
Public Instance Methods
Check if there are enough cards left to deal a new hand @return [Boolean]
# File lib/rypto.rb, line 107 def can_deal? size() >= 6 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
# 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 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
# File lib/rypto.rb, line 94 def deal_hand cards = deal_cards Hand.new(cards[0, 5], cards[5]) 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
# 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 of the deck @return [Fixnum] number of cards left in the deck
# File lib/rypto.rb, line 101 def size @cards.size end