Class: Gamemaker::CardGame::Deck
- Inherits:
-
Object
- Object
- Gamemaker::CardGame::Deck
- Defined in:
- lib/gamemaker/card_game/deck.rb
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #<<(*cards) ⇒ Object
- #==(other) ⇒ Object
- #as_json ⇒ Object
- #draw(n = nil) ⇒ Object (also: #take)
- #draw!(n = nil) ⇒ Object (also: #take!)
- #empty? ⇒ Boolean
-
#initialize(cards = self.class.card_class.all) ⇒ Deck
constructor
A new instance of Deck.
- #length ⇒ Object
- #merge!(other) ⇒ Object
- #put(*cards, position: :bottom) ⇒ Object
- #shuffle! ⇒ Object
- #to_a ⇒ Object
- #undraw(*cards) ⇒ Object (also: #untake)
Constructor Details
#initialize(cards = self.class.card_class.all) ⇒ Deck
Returns a new instance of Deck.
19 20 21 |
# File 'lib/gamemaker/card_game/deck.rb', line 19 def initialize(cards = self.class.card_class.all) @cards = cards.dup end |
Class Method Details
.card_class ⇒ Object
4 5 6 |
# 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) ⇒ Object
15 16 17 |
# 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 |
.of(card_class) ⇒ Object
8 9 10 11 12 13 |
# 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 |
Instance Method Details
#<<(*cards) ⇒ Object
65 66 67 68 |
# File 'lib/gamemaker/card_game/deck.rb', line 65 def <<(*cards) @cards.concat(cards.flatten) self end |
#==(other) ⇒ Object
85 86 87 |
# File 'lib/gamemaker/card_game/deck.rb', line 85 def ==(other) @cards == other.to_a end |
#as_json ⇒ Object
89 90 91 |
# File 'lib/gamemaker/card_game/deck.rb', line 89 def as_json(*) { cards: to_a } end |
#draw(n = nil) ⇒ Object Also known as: take
36 37 38 |
# File 'lib/gamemaker/card_game/deck.rb', line 36 def draw(n = nil) n ? @cards.shift(n) : @cards.shift end |
#draw!(n = nil) ⇒ Object Also known as: take!
42 43 44 45 46 47 48 49 50 |
# File 'lib/gamemaker/card_game/deck.rb', line 42 def draw!(n = nil) if n && n > @cards.length raise IndexError, "Cannot draw #{n} cards from the deck: only #{@cards.length} cards left" elsif !n && empty? raise IndexError, "Cannot draw from the deck: no cards left" else draw(n) end end |
#empty? ⇒ Boolean
27 28 29 |
# File 'lib/gamemaker/card_game/deck.rb', line 27 def empty? @cards.empty? end |
#length ⇒ Object
23 24 25 |
# File 'lib/gamemaker/card_game/deck.rb', line 23 def length @cards.length end |
#merge!(other) ⇒ Object
77 78 79 |
# File 'lib/gamemaker/card_game/deck.rb', line 77 def merge!(other) self << other.draw(other.length) end |
#put(*cards, position: :bottom) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# 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! ⇒ Object
31 32 33 34 |
# File 'lib/gamemaker/card_game/deck.rb', line 31 def shuffle! @cards.shuffle! self end |
#to_a ⇒ Object
81 82 83 |
# File 'lib/gamemaker/card_game/deck.rb', line 81 def to_a @cards.dup end |
#undraw(*cards) ⇒ Object Also known as: untake
70 71 72 73 |
# File 'lib/gamemaker/card_game/deck.rb', line 70 def undraw(*cards) @cards = cards.flatten + @cards self end |