class Gamemaker::CardGame::Deck

Public Class Methods

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

Public Instance Methods

<<(*cards) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 65
def <<(*cards)
  @cards.concat(cards.flatten)
  self
end
==(other) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 85
def ==(other)
  @cards == other.to_a
end
as_json(*) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 89
def as_json(*)
  { cards: to_a }
end
draw(n = nil) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 36
def draw(n = nil)
  n ? @cards.shift(n) : @cards.shift
end
Also aliased as: take
draw!(n = nil) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 42
def draw!(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(n)
  end
end
Also aliased as: take!
empty?() click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 27
def empty?
  @cards.empty?
end
length() click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 23
def length
  @cards.length
end
merge(other) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 77
def merge(other)
  self << other.draw(other.length)
end
put(*cards, position: :bottom) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 54
def put(*cards, position: :bottom)
  case position
  when :top
    undraw(*cards)
  when :bottom
    self << cards
  else
    raise ArgumentError, "Cannot put cards to #{position.inspect}"
  end
end
shuffle() click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 31
def shuffle
  @cards.shuffle!
  self
end
take(n = nil)
Alias for: draw
take!(n = nil)
Alias for: draw!
to_a() click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 81
def to_a
  @cards.dup
end
undraw(*cards) click to toggle source
# File lib/gamemaker/card_game/deck.rb, line 70
def undraw(*cards)
  @cards = cards.flatten + @cards
  self
end
Also aliased as: untake
untake(*cards)
Alias for: undraw