ruby-poker 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -2
- data/examples/quick_example.rb +10 -0
- data/lib/poker_hand.rb +6 -8
- data/lib/poker_helper.rb +23 -0
- data/test/tc_poker_hand.rb +15 -0
- metadata +7 -5
- data/lib/array_helper.rb +0 -25
data/CHANGELOG
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
2008-01-10 (0.1.0)
|
2
2
|
* Initial version
|
3
|
-
2008-01-
|
3
|
+
2008-01-12 (0.1.1)
|
4
4
|
* Ranks are now a class.
|
5
|
-
*
|
5
|
+
* Extracted card, rank, and arrays methods to individual files
|
6
|
+
* Added gem packaging
|
7
|
+
2008-01-12 (0.1.2)
|
8
|
+
* Fixed critical bug that was stopping the whole program to not work
|
9
|
+
* Added some test cases as a result
|
10
|
+
* More test cases coming soon
|
data/lib/poker_hand.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'array_helper.rb'
|
1
|
+
require 'poker_helper.rb'
|
4
2
|
require 'rank.rb'
|
5
3
|
require 'card.rb'
|
6
4
|
|
7
5
|
class PokerHand
|
8
6
|
|
9
|
-
include
|
7
|
+
include PokerHelper
|
10
8
|
include Comparable
|
11
9
|
attr_reader :cards, :values_hash
|
12
10
|
|
@@ -104,17 +102,17 @@ class PokerHand
|
|
104
102
|
hand1_pair = @values_hash.index(2) # get key(card value) of the duplicate pair
|
105
103
|
hand2_pair = hand2.values_hash.index(2)
|
106
104
|
if hand1_pair == hand2_pair
|
107
|
-
return self.values.
|
105
|
+
return singles(self.values).reverse <=> singles(hand2.values).reverse
|
108
106
|
else
|
109
107
|
return hand1_pair <=> hand2_pair
|
110
108
|
end
|
111
109
|
when 2 # two people with two pairs
|
112
110
|
# check who has the higher pair, if the pairs are the same
|
113
111
|
# then remove the pairs and check for highest card
|
114
|
-
hand1_pairs = self.values.
|
115
|
-
hand2_pairs = hand2.values.
|
112
|
+
hand1_pairs = duplicates(self.values).reverse
|
113
|
+
hand2_pairs = duplicates(hand2.values).reverse
|
116
114
|
if hand1_pairs == hand2_pairs
|
117
|
-
return self.values
|
115
|
+
return singles(self.values) <=> singles(hand2.values) # there will be only one card remaining
|
118
116
|
else
|
119
117
|
return hand1_pairs <=> hand2_pairs
|
120
118
|
end
|
data/lib/poker_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module PokerHelper
|
2
|
+
# Returns array of elements that only occur once
|
3
|
+
# [1, 1, 2, 3] => [2, 3]
|
4
|
+
def singles array1
|
5
|
+
counts = Hash.new(0)
|
6
|
+
array1.each do |value|
|
7
|
+
counts[value] += 1
|
8
|
+
end
|
9
|
+
|
10
|
+
return counts.collect {|key,value| value == 1 ? key : nil }.compact.sort
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns an array containing values that we duplicated in the original array
|
14
|
+
# [1, 2, 3, 1] => [1]
|
15
|
+
def duplicates array1
|
16
|
+
counts = Hash.new(0)
|
17
|
+
array1.each do |value|
|
18
|
+
counts[value] += 1
|
19
|
+
end
|
20
|
+
|
21
|
+
return counts.collect {|key,value| value > 1 ? key : nil }.compact.sort
|
22
|
+
end
|
23
|
+
end # module ArrayHelper
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'ruby-poker'
|
5
|
+
|
6
|
+
class TestPokerHand < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@hand1 = PokerHand.new("6D 7C 5D 5H 3S")
|
9
|
+
@hand2 = PokerHand.new(["5C", "JC", "2H", "5S", "3D"])
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_comparable
|
13
|
+
assert_equal(-1, @hand1 <=> @hand2)
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-poker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Olson
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
@@ -24,11 +24,13 @@ extra_rdoc_files:
|
|
24
24
|
- CHANGELOG
|
25
25
|
- LICENSE
|
26
26
|
files:
|
27
|
-
-
|
27
|
+
- examples/quick_example.rb
|
28
28
|
- lib/card.rb
|
29
29
|
- lib/poker_hand.rb
|
30
|
+
- lib/poker_helper.rb
|
30
31
|
- lib/rank.rb
|
31
32
|
- lib/ruby-poker.rb
|
33
|
+
- test/tc_poker_hand.rb
|
32
34
|
- README
|
33
35
|
- CHANGELOG
|
34
36
|
- LICENSE
|
@@ -58,5 +60,5 @@ rubygems_version: 1.0.1
|
|
58
60
|
signing_key:
|
59
61
|
specification_version: 2
|
60
62
|
summary: Ruby library for determining the winner in a game of poker.
|
61
|
-
test_files:
|
62
|
-
|
63
|
+
test_files:
|
64
|
+
- test/tc_poker_hand.rb
|
data/lib/array_helper.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module ArrayHelper
|
2
|
-
class Array < ::Array
|
3
|
-
# Returns array of elements that only occur once
|
4
|
-
# [1, 1, 2, 3] => [2, 3]
|
5
|
-
def singles
|
6
|
-
counts = Hash.new(0)
|
7
|
-
self.each do |value|
|
8
|
-
counts[value] += 1
|
9
|
-
end
|
10
|
-
|
11
|
-
return counts.collect {|key,value| value == 1 ? key : nil }.compact.sort
|
12
|
-
end
|
13
|
-
|
14
|
-
# Returns an array containing values that we duplicated in the original array
|
15
|
-
# [1, 2, 3, 1] => [1]
|
16
|
-
def duplicates
|
17
|
-
counts = Hash.new(0)
|
18
|
-
self.each do |value|
|
19
|
-
counts[value] += 1
|
20
|
-
end
|
21
|
-
|
22
|
-
return counts.collect {|key,value| value > 1 ? key : nil }.compact.sort
|
23
|
-
end
|
24
|
-
end # class Array
|
25
|
-
end # module ArrayHelper
|