Class: Card
Constant Summary collapse
- CLUB =
constants for glyphs
'♣'- DIAMOND =
'♦'- HEART =
'♥'- SPADE =
'♠'
Instance Method Summary collapse
-
#<=>(c) ⇒ Object
comparator.
-
#initialize(rank = 'Ace', suit = 'Spades') ⇒ Card
constructor
A new instance of Card.
-
#rank(short = false) ⇒ Object
returns the rank of the card optional short parameter will limit face cards to one character.
-
#short ⇒ Object
returns the short rank, followed by suit icon.
-
#suit(glyph = false) ⇒ Object
returns the suit of a card optional glyph parameter displays a colored unicode character.
-
#to_i ⇒ Object
returns the numerical representation of the rank.
-
#to_s ⇒ Object
draws a card picture.
Constructor Details
#initialize(rank = 'Ace', suit = 'Spades') ⇒ Card
Returns a new instance of Card.
14 15 16 17 |
# File 'lib/rubycards/card.rb', line 14 def initialize(rank = 'Ace', suit = 'Spades') @rank = rank_to_i(rank) @suit = suit_to_i(suit) end |
Instance Method Details
#<=>(c) ⇒ Object
comparator
51 52 53 |
# File 'lib/rubycards/card.rb', line 51 def <=>(c) self.to_i <=> c.to_i end |
#rank(short = false) ⇒ Object
returns the rank of the card optional short parameter will limit face cards to one character
21 22 23 24 25 26 27 28 |
# File 'lib/rubycards/card.rb', line 21 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 |
#short ⇒ Object
returns the short rank, followed by suit icon
46 47 48 |
# File 'lib/rubycards/card.rb', line 46 def short "#{rank(true)}#{suit(true)}" end |
#suit(glyph = false) ⇒ Object
returns the suit of a card optional glyph parameter displays a colored unicode character
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubycards/card.rb', line 32 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_i ⇒ Object
returns the numerical representation of the rank
56 57 58 |
# File 'lib/rubycards/card.rb', line 56 def to_i @rank end |
#to_s ⇒ Object
draws a card picture
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rubycards/card.rb', line 61 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 |