Class: RubyCards::Card

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubycards/card.rb

Overview

Class representation of a standard playing card. (French Design: 52 cards)

Constant Summary collapse

CLUB =
''
DIAMOND =
''
HEART =
''
SPADE =
''

Instance Method Summary collapse

Constructor Details

#initialize(rank = 'Ace', suit = 'Spades') ⇒ Card

Returns a new card instance.

Parameters:

  • rank (String, Integer) (defaults to: 'Ace')

    The rank of the card

  • suit (String) (defaults to: 'Spades')

    The suit of the card



21
22
23
24
# File 'lib/rubycards/card.rb', line 21

def initialize(rank = 'Ace', suit = 'Spades')
  @rank = rank_to_i(rank)
  @suit = suit_to_i(suit)
end

Instance Method Details

#<=>(other) ⇒ Integer

Compares the card to another.

Parameters:

  • other (Card)

    A card to use in comparison

Returns:

  • (Integer)


71
72
73
# File 'lib/rubycards/card.rb', line 71

def <=>(other)
  self.to_i <=> other.to_i
end

#rank(short = false) ⇒ String

Returns the rank of the card with an optional ‘short` parameter to limit face cards to one character.

Parameters:

  • short (Boolean) (defaults to: false)

    Optional short setting

Returns:

  • (String)

    The string representation of the card’s rank



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

def rank(short = false)
  if (2..10) === @rank
    @rank.to_s
  else
    h = { 11 => 'Jack', 12 => 'Queen', 13 => 'King', 14 => 'Ace' }
    h[@rank] && short ? h[@rank][0] : h[@rank]
  end
end

#shortString Also known as: inspect

Returns the shortened rank, followed by a suit glyph.

Returns:

  • (String)

    The short representation of the card.



61
62
63
# File 'lib/rubycards/card.rb', line 61

def short
  "#{rank(true)}#{suit(true)}"
end

#suit(glyph = false) ⇒ String

Returns the suit of the card.

of a word

Parameters:

  • glyph (Boolean) (defaults to: false)

    Optional setting to draw a unicode glyph in place

Returns:

  • (String)

    The string (of glyph) representation of the card’s suit



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubycards/card.rb', line 45

def suit(glyph = false)
  case @suit
    when 1
      glyph ? CLUB.black.bold : 'Clubs'
    when 2
      glyph ? DIAMOND.red : 'Diamonds'
    when 3
      glyph ? HEART.red : 'Hearts'
    when 4
      glyph ? SPADE.black.bold : 'Spades'
  end
end

#to_iInteger

Returns the integer representation of the rank.

Returns:

  • (Integer)

    The rank of the card



78
79
80
# File 'lib/rubycards/card.rb', line 78

def to_i
  @rank
end

#to_sString

Returns the ASCII-art representation of the card.

Returns:

  • (String)

    The card drawn in ASCII characters.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rubycards/card.rb', line 85

def to_s
  # A simple template with X's as placeholders
  # YY represents the placement of the card's rank
  template = <<-TPL.gsub(/^\s+/,'')
    ╭───────╮
    | X X X |
    | X X X |
    | X YYX |
    | X X X |
    ╰───────╯
  TPL

  # the patterns represent the configuration of glyphys
  #   read from left to right, top to bottom
  # X means place a glyph, _ means clear the space
  case @rank
    when 2;  pattern = '_X_______X_'
    when 3;  pattern = '_X__X____X_'
    when 4;  pattern = 'X_X_____X_X'
    when 5;  pattern = 'X_X_X___X_X'
    when 6;  pattern = 'X_XX_X__X_X'
    when 7;  pattern = 'X_X_X_XXX_X'
    when 8;  pattern = 'X_XX_XXXX_X'
    when 9;  pattern = 'X_XXXXXXX_X'
    when 10; pattern = 'XXXX_XXXXXX'
    when 11..14;
      pattern = 'X_________X'
  end

  pattern.each_char do |c|
    # replace X's with glyphs
    if c == 'X'
      template.sub!(/X/, "#{suit(true)}")
    # replace _'s with whitespace
    else
      template.sub!(/X/, " ")
    end
  end

  # place the card rank (left-padded)
  template.sub(/YY/, rank(true).ljust(2))
end