Class: Rypto::Deck

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeDeck

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

#cardsArray<Fixnum> (readonly)

Returns all the card values left in the deck.

Returns:

  • (Array<Fixnum>)

    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

Returns:

  • (Boolean)


107
108
109
# File 'lib/rypto.rb', line 107

def can_deal?
  size() >= 6
end

#deal_cardsArray(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum, Fixnum)

Deal six raw card values from the deck

Returns:

  • (Array(Fixnum, Fixnum, Fixnum, Fixnum, Fixnum, Fixnum))

    a random hand from the deck

Raises:

  • (RuntimeError)

    if there are not enough cards to deal a full hand in 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_handHand

Deal a Hand from the deck

Returns:

  • (Hand)

    a random hand from the deck

Raises:

  • (RuntimeError)

    if there are not enough cards to deal a full hand in 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

Parameters:

  • cards (Fixnum)

    the card value to draw

Returns:

  • (Fixnum)

    the card value that was drawn

Raises:

  • (ArgumentError)

    if the card is not in 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

#sizeFixnum

Size of the deck

Returns:

  • (Fixnum)

    number of cards left in the deck



101
102
103
# File 'lib/rypto.rb', line 101

def size
  @cards.size
end