Class: Gamemaker::CardGame::PlayingCard

Inherits:
Card
  • Object
show all
Defined in:
lib/gamemaker/card_game/playing_card.rb

Constant Summary collapse

SUITS =
[:clubs, :diamonds, :hearts, :spades]
RANKS =
[:ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, :jack, :queen, :king]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suit, rank) ⇒ PlayingCard

Returns a new instance of PlayingCard.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gamemaker/card_game/playing_card.rb', line 30

def initialize(suit, rank)
  unless SUITS.include?(suit)
    raise ArgumentError, "Invalid suit: #{suit.inspect}"
  end

  if Fixnum === rank && rank > 0
    rank = RANKS[rank - 1]
  end

  unless RANKS.include?(rank)
    raise ArgumentError, "Invalid rank: #{rank.inspect}"
  end

  @suit = suit
  @rank = rank
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



7
8
9
# File 'lib/gamemaker/card_game/playing_card.rb', line 7

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



7
8
9
# File 'lib/gamemaker/card_game/playing_card.rb', line 7

def suit
  @suit
end

Class Method Details

.allObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gamemaker/card_game/playing_card.rb', line 9

def self.all
  cards = []

  SUITS.each do |suit|
    RANKS.each do |rank|
      cards << new(suit, rank)
    end
  end

  cards
end

.from_json(json) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/gamemaker/card_game/playing_card.rb', line 21

def self.from_json(json)
  suit, rank = json["suit"], json["rank"]

  suit = suit.to_sym
  rank = rank.to_sym if String === rank

  new(suit, rank)
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
# File 'lib/gamemaker/card_game/playing_card.rb', line 79

def ==(other)
  @rank == other.rank && @suit == other.suit
end

#ace?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/gamemaker/card_game/playing_card.rb', line 63

def ace?
  @rank == :ace
end

#as_jsonObject



83
84
85
# File 'lib/gamemaker/card_game/playing_card.rb', line 83

def as_json(*)
  { suit: @suit, rank: @rank }
end

#clubs?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/gamemaker/card_game/playing_card.rb', line 47

def clubs?
  @suit == :clubs
end

#diamonds?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/gamemaker/card_game/playing_card.rb', line 51

def diamonds?
  @suit == :diamonds
end

#hearts?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gamemaker/card_game/playing_card.rb', line 55

def hearts?
  @suit == :hearts
end

#jack?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/gamemaker/card_game/playing_card.rb', line 67

def jack?
  @rank == :jack
end

#king?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/gamemaker/card_game/playing_card.rb', line 75

def king?
  @rank == :king
end

#queen?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/gamemaker/card_game/playing_card.rb', line 71

def queen?
  @rank == :queen
end

#spades?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gamemaker/card_game/playing_card.rb', line 59

def spades?
  @suit == :spades
end