Class: Gamemaker::CardGame::Hand
- Inherits:
-
Deck
- Object
- Deck
- Gamemaker::CardGame::Hand
show all
- Defined in:
- lib/gamemaker/card_game/hand.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Deck
#<<, #==, #as_json, #draw, #draw!, #empty?, from_json, #length, #merge, #put, #shuffle, #to_a, #undraw
Constructor Details
#initialize(cards = []) ⇒ Hand
Returns a new instance of Hand.
15
16
17
|
# File 'lib/gamemaker/card_game/hand.rb', line 15
def initialize(cards = [])
super(cards)
end
|
Class Method Details
.card_class ⇒ Object
4
5
6
|
# 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
|
.of(card_class) ⇒ Object
8
9
10
11
12
13
|
# 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
|
Instance Method Details
#draw_randomly(n = nil) ⇒ Object
23
24
25
26
27
|
# 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) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# 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) ⇒ Boolean
19
20
21
|
# File 'lib/gamemaker/card_game/hand.rb', line 19
def include?(*cards)
cards.flatten.all? { |card| @cards.include?(card) }
end
|
#remove(*cards) ⇒ Object
39
40
41
42
43
|
# 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) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# 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
|