rpoker 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59b9ef52714d62177bd664c67c4d2364d689a25c
4
- data.tar.gz: 46884f0610f8cbb5bd184ca2b97a1ee8868ba21b
3
+ metadata.gz: 5741ebe32752dd99d446d310de326c3135044d7e
4
+ data.tar.gz: 19f9e061844a3cee879bf6b8446021a4c2d885b6
5
5
  SHA512:
6
- metadata.gz: 33a3401c1f6272b60b2f8dadc28af9f3ec26070ec67afc0500cdefd1d7f15dc7fa164d4a204670bad1c9f2f8f0b68cb4b6990520150e71a9e31c151b6be42996
7
- data.tar.gz: 16e3413647dedf0457e538ed7aaef29066ae891706b92b11a769a0239cf95766a97e3c8d3f641207b2e5cfecf09c6dadcf62bb7c141392c7346f9c859fc78c10
6
+ metadata.gz: 30d83de20f3889b6f7136e6da6986e71d87674db9ae7f8c67414aa3c706ba2bc6679a5fdac66dd1334bb64995c2e52b62d13b7e6c287ac172e5f0ff5c2de5b01
7
+ data.tar.gz: a68d3baea1a99ef7b84e668e2779651929aafbe77931ce9fa8a08cadca05864ab9612dc7d0dd49928a3ca7e9ec16e31ccb412ca437e8b2988ff9412125199689
data/lib/rpoker/card.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  class Card
2
2
  attr_reader :value, :suit
3
3
 
4
- FACES = %w(T J Q K A)
5
- SUITS = %w(h d c s)
4
+ FACES = %w(T J Q K A)
5
+ SUITS = %w(h d c s)
6
6
  VALUES = %w(2 3 4 5 6 7 8 9) + FACES
7
7
 
8
8
  FACE_VALUES = {
data/lib/rpoker/hand.rb CHANGED
@@ -2,7 +2,7 @@ class Hand
2
2
  include Comparable
3
3
  attr_reader :cards
4
4
 
5
- TYPES = %w(
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
- @suits ||= cards.map(&:suit)
51
+ cards.map(&:suit)
44
52
  end
45
53
 
46
54
  def values
47
- @values ||= cards.map(&:value)
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 num_uniq_values
55
- @num_uniq_values ||= values.uniq.size
58
+ def nums
59
+ wheel? ? [5,4,3,2,1] : cards.map(&:to_i)
56
60
  end
57
61
 
58
- def wheel?
59
- values == %w(A 5 4 3 2)
62
+ def straight_flush?
63
+ flush? && straight?
60
64
  end
61
65
 
62
66
  def flush?
63
- suits.all? { |suit| suit == suits.first }
67
+ suits.uniq.size == 1
64
68
  end
65
69
 
66
70
  def straight?
67
- wheel? || (int_values.first == int_values.last + 4 && num_uniq_values == 5)
71
+ wheel? || (nums.first == nums.last + 4 && form == :abcde)
68
72
  end
69
73
 
70
- def straight_flush?
71
- flush? && straight?
74
+ def wheel?
75
+ values == %w(A 5 4 3 2)
72
76
  end
73
77
 
74
- def full_house?
75
- same_values?(0, 2) && same_values?(3, 4)
78
+ def four_of_a_kind?
79
+ form == :aaaab
76
80
  end
77
81
 
78
- def four_of_a_kind?
79
- same_values?(0, 3)
82
+ def full_house?
83
+ form == :aaabb
80
84
  end
81
85
 
82
86
  def three_of_a_kind?
83
- same_values?(0, 2) && num_uniq_values == 3
87
+ form == :aaabc
84
88
  end
85
89
 
86
90
  def two_pair?
87
- same_values?(0, 1) && same_values?(2, 3) && num_uniq_values == 3
91
+ form == :aabbc
88
92
  end
89
93
 
90
94
  def pair?
91
- same_values?(0, 1) && num_uniq_values == 4
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
- vals = cards.map(&:to_i)
103
- counts = Hash[vals.map { |val| [val, vals.count(val)] }]
104
- @cards.sort_by! { |card| [-counts[card.to_i], -card.to_i] }
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 same_values?(start, stop)
108
- vals = values[start..stop]
109
- vals.all? { |value| value == vals.first }
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!
@@ -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
- Hand::TYPES.each do |type|
10
- # build the boolean type check method
11
- is_type = "#{type}?".to_sym
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
- # for two hands of the same type (straight, flush, etc):
30
- # returns the winner by sorting the hands' integer values
31
- # and then comparing value pairs in descending order
32
- def same_type_winner
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.2
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.4.8
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: