Class: BetweenTheSheets::Card

Inherits:
Object
  • Object
show all
Defined in:
lib/between_the_sheets/card.rb

Constant Summary collapse

NAMES =
%w{A 2 3 4 5 6 7 8 9 10 J Q K JOKER}.freeze
SUITS =
%w{clubs diamonds hearts spades}.freeze
SUIT_COLORS =
{
  clubs: :white,
  diamonds: :red,
  hearts: :red,
  spades: :white
}.freeze
IMAGES =
{
  clubs: "\u2664",
  hearts: "\u2661",
  diamonds: "\u2662",
  spades: "\u2667"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCard

Returns a new instance of Card.



31
32
33
34
35
36
# File 'lib/between_the_sheets/card.rb', line 31

def initialize
  @name = NAMES.sample
  @suit = SUITS.sample
  @image = IMAGES[@suit.to_sym]
  @value = self.class.value(@name)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/between_the_sheets/card.rb', line 29

def name
  @name
end

#suitObject (readonly)

Returns the value of attribute suit.



29
30
31
# File 'lib/between_the_sheets/card.rb', line 29

def suit
  @suit
end

#valueObject (readonly)

Returns the value of attribute value.



29
30
31
# File 'lib/between_the_sheets/card.rb', line 29

def value
  @value
end

Class Method Details

.draw(*skip_cards) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/between_the_sheets/card.rb', line 38

def self.draw(*skip_cards)
  return Card.new if skip_cards.empty?

  loop do
    card = Card.new
    break card unless skip_cards.include? card.id
  end
end

.value(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/between_the_sheets/card.rb', line 47

def self.value(name)
  case name
  when "A"
    1
  when "J"
    11
  when "Q"
    12
  when "K"
    13
  when "JOKER"
    0
  else
    NAMES.index(name) + 1
  end
end

Instance Method Details

#idObject



70
71
72
# File 'lib/between_the_sheets/card.rb', line 70

def id
  (Digest::MD5.new << self.to_s).to_s
end

#joker?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/between_the_sheets/card.rb', line 74

def joker?
  @name == "JOKER"
end

#to_sObject



64
65
66
67
68
# File 'lib/between_the_sheets/card.rb', line 64

def to_s
  # The space at the end is intentional to pad the image with the
  # next character.
  "#{@name}#{@image.colorize(SUIT_COLORS[@suit.to_sym])} "
end