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]
SUIT_GLYPHS =
{ clubs: "♣", diamonds: "♦", hearts: "♥", spades: "♠" }
CARD_GLYPHS =
{
  clubs:    { ace: "🃑", 2 => "🃒", 3 => "🃓", 4 => "🃔", 5 => "🃕", 6 => "🃖", 7 => "🃗", 8 => "🃘", 9 => "🃙", 10 => "🃚", jack: "🃛", queen: "🃝", king: "🃞" },
  diamonds: { ace: "🃁", 2 => "🃂", 3 => "🃃", 4 => "🃄", 5 => "🃅", 6 => "🃆", 7 => "🃇", 8 => "🃈", 9 => "🃉", 10 => "🃊", jack: "🃋", queen: "🃍", king: "🃎" },
  hearts:   { ace: "🂱", 2 => "🂲", 3 => "🂳", 4 => "🂴", 5 => "🂵", 6 => "🂶", 7 => "🂷", 8 => "🂸", 9 => "🂹", 10 => "🂺", jack: "🂻", queen: "🂽", king: "🂾" },
  spades:   { 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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gamemaker/card_game/playing_card.rb', line 38

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.



15
16
17
# File 'lib/gamemaker/card_game/playing_card.rb', line 15

def rank
  @rank
end

#suitObject (readonly)

Returns the value of attribute suit.



15
16
17
# File 'lib/gamemaker/card_game/playing_card.rb', line 15

def suit
  @suit
end

Class Method Details

.allObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gamemaker/card_game/playing_card.rb', line 17

def self.all
  cards = []

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

  cards
end

.from_json(json) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/gamemaker/card_game/playing_card.rb', line 29

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



87
88
89
# File 'lib/gamemaker/card_game/playing_card.rb', line 87

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

#ace?Boolean

Returns:

  • (Boolean)


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

def ace?
  @rank == :ace
end

#as_jsonObject



114
115
116
# File 'lib/gamemaker/card_game/playing_card.rb', line 114

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

#clubs?Boolean

Returns:

  • (Boolean)


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

def clubs?
  @suit == :clubs
end

#diamonds?Boolean

Returns:

  • (Boolean)


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

def diamonds?
  @suit == :diamonds
end

#hearts?Boolean

Returns:

  • (Boolean)


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

def hearts?
  @suit == :hearts
end

#jack?Boolean

Returns:

  • (Boolean)


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

def jack?
  @rank == :jack
end

#king?Boolean

Returns:

  • (Boolean)


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

def king?
  @rank == :king
end

#queen?Boolean

Returns:

  • (Boolean)


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

def queen?
  @rank == :queen
end

#spades?Boolean

Returns:

  • (Boolean)


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

def spades?
  @suit == :spades
end

#to_s(format = :simple) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gamemaker/card_game/playing_card.rb', line 91

def to_s(format = :simple)
  suit = SUIT_GLYPHS[@suit]

  if Fixnum === @rank
    rank = @rank.to_s
  else
    rank = @rank.to_s.upcase[0]
  end

  simple = suit + rank

  case format
  when :simple
    simple
  when :fancy
    "┌──┐\n│#{simple.ljust(3,'|')}\n└──┘\n"
  when :glyph
    CARD_GLYPHS[@suit][@rank]
  else
    raise ArgumentError, "Unknown format #{format.inspect}"
  end
end