Class: Poker::Card

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

Constant Summary collapse

RANKS =
%w{ 2 3 4 5 6 7 8 9 T J Q K A }
SUITS =
%w{ c d h s }
PRIMES =
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Card

Returns a new instance of Card.



9
10
11
12
13
14
# File 'lib/poker/card.rb', line 9

def initialize(str)
  @str = str
  @rank, @suit = str.split(//)
  @rank_value = RANKS.index(@rank)
  @suit_value = SUITS.index(@suit)
end

Instance Attribute Details

#rankObject (readonly)

Returns the value of attribute rank.



7
8
9
# File 'lib/poker/card.rb', line 7

def rank
  @rank
end

#rank_valueObject (readonly)

Returns the value of attribute rank_value.



7
8
9
# File 'lib/poker/card.rb', line 7

def rank_value
  @rank_value
end

#suitObject (readonly)

Returns the value of attribute suit.



7
8
9
# File 'lib/poker/card.rb', line 7

def suit
  @suit
end

#suit_valueObject (readonly)

Returns the value of attribute suit_value.



7
8
9
# File 'lib/poker/card.rb', line 7

def suit_value
  @suit_value
end

Instance Method Details

#to_sObject



34
35
36
# File 'lib/poker/card.rb', line 34

def to_s
  @str
end

#valueObject

An integer is made up of four bytes. The high-order bytes are used to hold the rank bit pattern, whereas the low-order bytes hold the suit/rank/prime value of the card.

--------——–--------——–+ |xxxbbbbb|bbbbbbbb|cdhsrrrr|xxpppppp| --------——–--------——–+

p = prime number of rank (deuce=2,trey=3,four=5,five=7,…,ace=41) r = rank of card (deuce=0,trey=1,four=2,five=3,…,ace=12) cdhs = suit of card b = bit turned on depending on rank of card #



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

def value
  PRIMES[rank_value] | (rank_value << 8) | 0x8000 >> suit_value |
    (1 << (16 + rank_value))
end