rpoker 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rpoker/card.rb +2 -2
- data/lib/rpoker/hand.rb +40 -33
- data/lib/rpoker/matchup.rb +7 -39
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5741ebe32752dd99d446d310de326c3135044d7e
|
4
|
+
data.tar.gz: 19f9e061844a3cee879bf6b8446021a4c2d885b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30d83de20f3889b6f7136e6da6986e71d87674db9ae7f8c67414aa3c706ba2bc6679a5fdac66dd1334bb64995c2e52b62d13b7e6c287ac172e5f0ff5c2de5b01
|
7
|
+
data.tar.gz: a68d3baea1a99ef7b84e668e2779651929aafbe77931ce9fa8a08cadca05864ab9612dc7d0dd49928a3ca7e9ec16e31ccb412ca437e8b2988ff9412125199689
|
data/lib/rpoker/card.rb
CHANGED
data/lib/rpoker/hand.rb
CHANGED
@@ -2,7 +2,7 @@ class Hand
|
|
2
2
|
include Comparable
|
3
3
|
attr_reader :cards
|
4
4
|
|
5
|
-
|
5
|
+
RANKS = %i{
|
6
6
|
straight_flush
|
7
7
|
four_of_a_kind
|
8
8
|
full_house
|
@@ -11,7 +11,7 @@ class Hand
|
|
11
11
|
three_of_a_kind
|
12
12
|
two_pair
|
13
13
|
pair
|
14
|
-
|
14
|
+
}
|
15
15
|
|
16
16
|
def initialize(cards)
|
17
17
|
@cards =
|
@@ -35,78 +35,85 @@ class Hand
|
|
35
35
|
return -1 if winner == other_hand
|
36
36
|
end
|
37
37
|
|
38
|
+
def rank
|
39
|
+
@rank ||= RANKS.find { |rank| send("#{rank}?") } || :high_card
|
40
|
+
end
|
41
|
+
|
42
|
+
def rank_idx
|
43
|
+
RANKS.index(rank) || RANKS.size
|
44
|
+
end
|
45
|
+
|
38
46
|
def display
|
39
47
|
puts cards.join(" ")
|
40
48
|
end
|
41
49
|
|
42
50
|
def suits
|
43
|
-
|
51
|
+
cards.map(&:suit)
|
44
52
|
end
|
45
53
|
|
46
54
|
def values
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
def int_values
|
51
|
-
@int_values ||= cards.map(&:to_i)
|
55
|
+
cards.map(&:value)
|
52
56
|
end
|
53
57
|
|
54
|
-
def
|
55
|
-
|
58
|
+
def nums
|
59
|
+
wheel? ? [5,4,3,2,1] : cards.map(&:to_i)
|
56
60
|
end
|
57
61
|
|
58
|
-
def
|
59
|
-
|
62
|
+
def straight_flush?
|
63
|
+
flush? && straight?
|
60
64
|
end
|
61
65
|
|
62
66
|
def flush?
|
63
|
-
suits.
|
67
|
+
suits.uniq.size == 1
|
64
68
|
end
|
65
69
|
|
66
70
|
def straight?
|
67
|
-
wheel? || (
|
71
|
+
wheel? || (nums.first == nums.last + 4 && form == :abcde)
|
68
72
|
end
|
69
73
|
|
70
|
-
def
|
71
|
-
|
74
|
+
def wheel?
|
75
|
+
values == %w(A 5 4 3 2)
|
72
76
|
end
|
73
77
|
|
74
|
-
def
|
75
|
-
|
78
|
+
def four_of_a_kind?
|
79
|
+
form == :aaaab
|
76
80
|
end
|
77
81
|
|
78
|
-
def
|
79
|
-
|
82
|
+
def full_house?
|
83
|
+
form == :aaabb
|
80
84
|
end
|
81
85
|
|
82
86
|
def three_of_a_kind?
|
83
|
-
|
87
|
+
form == :aaabc
|
84
88
|
end
|
85
89
|
|
86
90
|
def two_pair?
|
87
|
-
|
91
|
+
form == :aabbc
|
88
92
|
end
|
89
93
|
|
90
94
|
def pair?
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
def rank
|
95
|
-
(TYPES.find { |type| send("#{type}?".to_sym) } || "high_card").gsub('_', ' ')
|
95
|
+
form == :aabcd
|
96
96
|
end
|
97
97
|
|
98
98
|
private
|
99
99
|
# sort cards by their value multiplicity in descending order
|
100
100
|
# e.g. sort Js 2s Jh 4s 2c as Js Jh 2s 2c 4s
|
101
101
|
def sort_cards!
|
102
|
-
|
103
|
-
|
104
|
-
|
102
|
+
@cards.sort_by! { |card| [-value_count[card.value], -card.to_i] }
|
103
|
+
end
|
104
|
+
|
105
|
+
def value_count
|
106
|
+
@value_count ||=
|
107
|
+
values.each_with_object(Hash.new(0)) { |v, hsh| hsh[v] += 1 }
|
108
|
+
end
|
109
|
+
|
110
|
+
def counts
|
111
|
+
value_count.values.sort_by { |count| -count }
|
105
112
|
end
|
106
113
|
|
107
|
-
def
|
108
|
-
|
109
|
-
|
114
|
+
def form
|
115
|
+
@form ||=
|
116
|
+
counts.zip(%w(a b c d e)).map { |count, l| l*count }.join.to_sym
|
110
117
|
end
|
111
118
|
|
112
119
|
def validate!
|
data/lib/rpoker/matchup.rb
CHANGED
@@ -1,52 +1,20 @@
|
|
1
1
|
class Matchup
|
2
2
|
attr_reader :hand1, :hand2
|
3
|
-
|
4
3
|
def initialize(hand1, hand2)
|
5
4
|
@hand1, @hand2 = hand1, hand2
|
6
5
|
end
|
7
6
|
|
8
7
|
def winner
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# check whether either hand is of the type
|
14
|
-
if hand1.send(is_type) && hand2.send(is_type)
|
15
|
-
return same_type_winner
|
16
|
-
elsif hand1.send(is_type)
|
17
|
-
return hand1
|
18
|
-
elsif hand2.send(is_type)
|
19
|
-
return hand2
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# both hands are high card rank and can be compared by
|
24
|
-
# comparing values
|
25
|
-
same_type_winner
|
8
|
+
return hand1 if hand1.rank_idx < hand2.rank_idx
|
9
|
+
return hand2 if hand2.rank_idx < hand1.rank_idx
|
10
|
+
same_rank_winner
|
26
11
|
end
|
27
12
|
|
28
13
|
private
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# check for the special case where either hand is a wheel
|
34
|
-
if hand1.wheel? && hand2.wheel?
|
35
|
-
return nil
|
36
|
-
elsif hand1.wheel?
|
37
|
-
return hand2
|
38
|
-
elsif hand2.wheel?
|
39
|
-
return hand1
|
40
|
-
end
|
41
|
-
|
42
|
-
# compare the numeric value pairs
|
43
|
-
value_pairs = hand1.int_values.zip(hand2.int_values)
|
44
|
-
value_pairs.each do |v1, v2|
|
45
|
-
if v1 > v2
|
46
|
-
return hand1
|
47
|
-
elsif v2 > v1
|
48
|
-
return hand2
|
49
|
-
end
|
14
|
+
def same_rank_winner
|
15
|
+
hand1.nums.zip(hand2.nums).each do |v1, v2|
|
16
|
+
return hand1 if v1 > v2
|
17
|
+
return hand2 if v2 > v1
|
50
18
|
end
|
51
19
|
nil
|
52
20
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rpoker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Cornelis
|
@@ -40,9 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
40
|
version: '0'
|
41
41
|
requirements: []
|
42
42
|
rubyforge_project:
|
43
|
-
rubygems_version: 2.
|
43
|
+
rubygems_version: 2.6.12
|
44
44
|
signing_key:
|
45
45
|
specification_version: 4
|
46
46
|
summary: Ruby library for comparing and ranking poker hands
|
47
47
|
test_files: []
|
48
|
-
has_rdoc:
|