-
Notifications
You must be signed in to change notification settings - Fork 0
/
poker.rb
100 lines (77 loc) · 1.62 KB
/
poker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
class Deck
def how_many_cards
52
end
def draw
Card.new('CT')
end
def deal
Hand.new('C2C3C4C5C6')
end
end
class Hand
def initialize description
chunks = chop(description)
@hand = build_from(chunks)
end
def how_many_cards
@hand.size
end
def rank
Rank.new(@hand)
end
private
def chop(description)
description.scan(/.{2}/)
end
def build_from(chunks)
chunks.map do |chunk|
Card.new(chunk)
end
end
end
class Rank
PAIR = 2
def initialize(cards)
@cards = cards
end
def highcard
card = @cards.max
card.to_s
end
def pair
paired = @cards.select do |card|
@cards.count(card) == PAIR
end
paired.first.value
end
end
class Card
VALUES=['2','3','4','5','6','7','8','9','T','J','Q','K','A']
def initialize description
@suit = description[0]
@value = description[1]
end
def suit
@suit
end
def value
@value
end
def to_s
@suit + @value
end
def > another_card
VALUES.find_index(@value) > VALUES.find_index(another_card.value)
end
def == another_card
VALUES.find_index(@value) == VALUES.find_index(another_card.value)
end
def <=> another_card
score = VALUES.find_index(@value)
another_score = VALUES.find_index(another_card.value)
return 0 if score == another_score
return -1 if score < another_score
return 1 if score > another_score
end
end