class Gamemaker::CardGame::Hand

Public Class Methods

card_class() click to toggle source
# File lib/gamemaker/card_game/hand.rb, line 4
def self.card_class
  raise NotImplementedError, "Hand is an abstract class, use Hand.of(CardType) to create a usable subclass"
end
new(cards = []) click to toggle source
Calls superclass method Gamemaker::CardGame::Deck::new
# File lib/gamemaker/card_game/hand.rb, line 15
def initialize(cards = [])
  super(cards)
end
of(card_class) click to toggle source
Calls superclass method Gamemaker::CardGame::Deck::of
# File lib/gamemaker/card_game/hand.rb, line 8
def self.of(card_class)
  Class.new(self) do
    define_singleton_method(:name) { super() || "#{card_class.name}Hand" }
    define_singleton_method(:card_class) { card_class }
  end
end

Public Instance Methods

draw_randomly(n = nil) click to toggle source
# File lib/gamemaker/card_game/hand.rb, line 23
def draw_randomly(n = nil)
  drawn = n ? @cards.sample(n) : @cards.sample
  remove(*Array(drawn))
  drawn
end
draw_randomly!(n = nil) click to toggle source
# File lib/gamemaker/card_game/hand.rb, line 29
def draw_randomly!(n = nil)
  if n && n > @cards.length
    raise IndexError, "Tried to draw #{n} cards when there were only #{@cards.length} cards left"
  elsif !n && empty?
    raise IndexError, "Tried to draw a card when there were no cards left"
  else
    draw_randomly(n)
  end
end
include?(*cards) click to toggle source
# File lib/gamemaker/card_game/hand.rb, line 19
def include?(*cards)
  cards.flatten.all? { |card| @cards.include?(card) }
end
remove(*cards) click to toggle source
# File lib/gamemaker/card_game/hand.rb, line 39
def remove(*cards)
  cards = cards.flatten
  @cards.delete_if { |card| cards.include?(card) }
  self
end
remove!(*cards) click to toggle source
# File lib/gamemaker/card_game/hand.rb, line 45
def remove!(*cards)
  cards.flatten.each do |card|
    unless @cards.include?(card)
      raise ArgumentError, "Cannot remove #{card} as it's not part of the hand"
    end
  end

  remove(*cards)
end