class Gamemaker::CardGame::PlayingCard

Constants

CARD_GLYPHS
RANKS
SUITS
SUIT_GLYPHS

Attributes

rank[R]
suit[R]

Public Class Methods

all() click to toggle source
# 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) click to toggle source
# 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
new(suit, rank) click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 87
def ==(other)
  @rank == other.rank && @suit == other.suit
end
ace?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 71
def ace?
  @rank == :ace
end
as_json(*) click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 114
def as_json(*)
  { suit: @suit, rank: @rank }
end
clubs?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 55
def clubs?
  @suit == :clubs
end
diamonds?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 59
def diamonds?
  @suit == :diamonds
end
hearts?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 63
def hearts?
  @suit == :hearts
end
jack?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 75
def jack?
  @rank == :jack
end
king?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 83
def king?
  @rank == :king
end
queen?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 79
def queen?
  @rank == :queen
end
spades?() click to toggle source
# File lib/gamemaker/card_game/playing_card.rb, line 67
def spades?
  @suit == :spades
end
to_s(format = :simple) click to toggle source
# 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