Class: Card

Inherits:
Object
  • Object
show all
Defined in:
lib/hand_rank/test.rb

Defined Under Namespace

Classes: SuitError, ValueError

Constant Summary collapse

AceOfSpades =
:"🂡"
TwoOfSpades =
:"🂢"
ThreeOfSpades =
:"🂣"
FourOfSpades =
:"🂤"
FiveOfSpades =
:"🂥"
SixOfSpades =
:"🂦"
SevenOfSpades =
:"🂧"
EightOfSpades =
:"🂨"
NineOfSpades =
:"🂩"
TenOfSpades =
:"🂪"
JackOfSpades =
:"🂫"
QueenOfSpades =
:"🂭"
KingOfSpades =
:"🂮"
AceOfHearts =
:"🂱"
TwoOfHearts =
:"🂲"
ThreeOfHearts =
:"🂳"
FourOfHearts =
:"🂴"
FiveOfHearts =
:"🂵"
SixOfHearts =
:"🂶"
SevenOfHearts =
:"🂷"
EightOfHearts =
:"🂸"
NineOfHearts =
:"🂹"
TenOfHearts =
:"🂺"
JackOfHearts =
:"🂻"
QueenOfHearts =
:"🂽"
KingOfHearts =
:"🂾"
AceOfDiamonds =
:"🃁"
TwoOfDiamonds =
:"🃂"
ThreeOfDiamonds =
:"🃃"
FourOfDiamonds =
:"🃄"
FiveOfDiamonds =
:"🃅"
SixOfDiamonds =
:"🃆"
SevenOfDiamonds =
:"🃇"
EightOfDiamonds =
:"🃈"
NineOfDiamonds =
:"🃉"
TenOfDiamonds =
:"🃊"
JackOfDiamonds =
:"🃋"
QueenOfDiamonds =
:"🃍"
KingOfDiamonds =
:"🃎"
AceOfClubs =
:"🃑"
TwoOfClubs =
:"🃒"
ThreeOfClubs =
:"🃓"
FourOfClubs =
:"🃔"
FiveOfClubs =
:"🃕"
SixOfClubs =
:"🃖"
SevenOfClubs =
:"🃗"
EightOfClubs =
:"🃘"
NineOfClubs =
:"🃙"
TenOfClubs =
:"🃚"
JackOfClubs =
:"🃛"
QueenOfClubs =
:"🃝"
KingOfClubs =
:"🃞"
Joker =
:"🃟"
SPADE =
:"♠"
HEART =
:"♡"
DIAMOND =
:"♢"
CLUB =
:"♣"
JOKER =
:"🃟"
SUITS =
[ SPADE, HEART, DIAMOND, CLUB, JOKER ]
VALUES =
[ nil, :"A", :"2", :"3", :"4", :"5", :"6", :"7", :"8", :"9", :"10", :"J", :"Q", :"K", :"A" ]
ABSOLUTE_VALUE =
{
     CLUB => [ nil, 49, 1, 5,  9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49 ],
  DIAMOND => [ nil, 50, 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50 ],
    HEART => [ nil, 51, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51 ],
    SPADE => [ nil, 52, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52 ],
    JOKER => [ nil ],
}
CARDS =
{
    SPADE => [ nil, AceOfSpades, TwoOfSpades, ThreeOfSpades, FourOfSpades, FiveOfSpades, SixOfSpades, SevenOfSpades, EightOfSpades, NineOfSpades, TenOfSpades, JackOfSpades, QueenOfSpades, KingOfSpades, AceOfSpades ],
    HEART => [ nil, AceOfHearts, TwoOfHearts, ThreeOfHearts, FourOfHearts, FiveOfHearts, SixOfHearts, SevenOfHearts, EightOfHearts, NineOfHearts, TenOfHearts, JackOfHearts, QueenOfHearts, KingOfHearts, AceOfHearts ],
  DIAMOND => [ nil, AceOfDiamonds, TwoOfDiamonds, ThreeOfDiamonds, FourOfDiamonds, FiveOfDiamonds, SixOfDiamonds, SevenOfDiamonds, EightOfDiamonds, NineOfDiamonds, TenOfDiamonds, JackOfDiamonds, QueenOfDiamonds, KingOfDiamonds, AceOfDiamonds ],
     CLUB => [ nil, AceOfClubs, TwoOfClubs, ThreeOfClubs, FourOfClubs, FiveOfClubs, SixOfClubs, SevenOfClubs, EightOfClubs, NineOfClubs, TenOfClubs, JackOfClubs, QueenOfClubs, KingOfClubs, AceOfClubs ],
    JOKER => [ Joker ],
}
SUIT_NAMES =
{
    SPADE => 'spades'.freeze,
    HEART => 'hearts'.freeze,
  DIAMOND => 'diamonds'.freeze,
     CLUB => 'clubs'.freeze,
    JOKER => 'joker '.freeze, # The trailing space is a hack to work with SuitError as well as to_str
}
VALUE_NAMES =
[ nil, 'ace'.freeze, 'two'.freeze, 'three'.freeze, 'four'.freeze, 'five'.freeze, 'six'.freeze, 'seven'.freeze, 'eight'.freeze, 'nine'.freeze, 'ten'.freeze, 'jack'.freeze, 'queen'.freeze, 'king'.freeze, 'ace'.freeze ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, suit:) ⇒ Card

Returns a new instance of Card.

Raises:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hand_rank/test.rb', line 118

def initialize( value:, suit: )
  value = normalize_value( value )
  raise ValueError unless value.instance_of? Fixnum
  raise ValueError unless (1..14).cover? value

  suit = normalize_suit( suit )
  raise SuitError unless SUIT_NAMES.keys.include? suit

  value = 0 if suit == JOKER

  @value = value.to_i
  @suit = suit

  @s = "#{ VALUES[ value ] }#{ suit }"

  @sym = CARDS[ suit ][ value ]

  parts = [ VALUE_NAMES[ value ], SUIT_NAMES[ suit ]]
  @str = parts.compact.join( ' of ' ).strip

  @abs = ABSOLUTE_VALUE[ suit ][ value ]
  
  self.freeze
end

Instance Attribute Details

#absObject (readonly) Also known as: to_i

Returns the value of attribute abs.



112
113
114
# File 'lib/hand_rank/test.rb', line 112

def abs
  @abs
end

#sObject (readonly) Also known as: to_s

Returns the value of attribute s.



112
113
114
# File 'lib/hand_rank/test.rb', line 112

def s
  @s
end

#strObject (readonly) Also known as: to_str

Returns the value of attribute str.



112
113
114
# File 'lib/hand_rank/test.rb', line 112

def str
  @str
end

#suitObject (readonly)

Returns the value of attribute suit.



112
113
114
# File 'lib/hand_rank/test.rb', line 112

def suit
  @suit
end

#symObject (readonly) Also known as: to_sym

Returns the value of attribute sym.



112
113
114
# File 'lib/hand_rank/test.rb', line 112

def sym
  @sym
end

#valueObject (readonly)

Returns the value of attribute value.



112
113
114
# File 'lib/hand_rank/test.rb', line 112

def value
  @value
end