the_gambler 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/Rakefile +12 -0
- data/VERSION +1 -1
- data/lib/the_gambler/card.rb +5 -2
- data/lib/the_gambler/poker.rb +7 -9
- data/spec/cards/initialization_spec.rb +14 -0
- data/spec/hands/poker/nine_card_hands_spec.rb +63 -0
- data/spec/hands/{seven_or_nine_card_spec.rb → poker/seven_card_hands_spec.rb} +4 -0
- data/the_gambler.gemspec +4 -5
- metadata +5 -6
- data/spec/decks/deck_spec.rb +0 -33
- data/spec/players/player_spec.rb +0 -0
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -43,3 +43,15 @@ task :default => :spec
|
|
43
43
|
|
44
44
|
require 'yard'
|
45
45
|
YARD::Rake::YardocTask.new
|
46
|
+
|
47
|
+
namespace :benchmark do
|
48
|
+
require 'the_gambler'
|
49
|
+
|
50
|
+
desc 'Test how long it takes to evaluate a given number of random hands.'
|
51
|
+
task :randoms do
|
52
|
+
10_000_000.times do |i|
|
53
|
+
TheGambler::Hand.new(rand(52), rand(52), rand(52), rand(52), rand(52)).poker_value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/the_gambler/card.rb
CHANGED
@@ -8,6 +8,11 @@ module TheGambler
|
|
8
8
|
|
9
9
|
def initialize(arg)
|
10
10
|
case arg.class.to_s
|
11
|
+
when 'Card','TheGambler::Card'
|
12
|
+
rank, suit = arg.rank, arg.suit
|
13
|
+
when 'Fixnum'
|
14
|
+
rank = RANKS[arg.zero? ? -1 : arg % 13]
|
15
|
+
suit = SUIT_STRINGS[arg / 13]
|
11
16
|
when 'String'
|
12
17
|
if arg =~ /([ajqk2-9]|10)([SCHD])/i then
|
13
18
|
rank, suit = $1.upcase, $2
|
@@ -26,8 +31,6 @@ module TheGambler
|
|
26
31
|
else
|
27
32
|
raise ArgumentError.new("Invalid hash: #{arg.inspect}")
|
28
33
|
end
|
29
|
-
when 'Card','TheGambler::Card'
|
30
|
-
rank, suit = arg.rank, arg.suit
|
31
34
|
else
|
32
35
|
raise ArgumentError.new("Must supply either a String, an Array, or a Hash, not a #{arg.class.to_s}")
|
33
36
|
end
|
data/lib/the_gambler/poker.rb
CHANGED
@@ -95,19 +95,17 @@ module TheGambler
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def straight?
|
98
|
-
|
99
|
-
|
100
|
-
nums = contents.map(&:numerical_value)
|
101
|
-
return true if nums.sort == [2, 3, 4, 5, 14]
|
98
|
+
nums = contents.map(&:numerical_value).sort
|
99
|
+
return true if nums == [2, 3, 4, 5, 14]
|
102
100
|
|
103
|
-
(2
|
104
|
-
|
105
|
-
|
101
|
+
one_different = nums[1..-1].zip(nums[0..-2]).group_by{|a, b| a - b}[1]
|
102
|
+
|
103
|
+
one_different and one_different.count >= 4
|
106
104
|
end
|
107
105
|
|
108
106
|
def flush?
|
109
107
|
c = contents.group_by(&:suit)
|
110
|
-
!! contents.group_by(&:suit).values.detect{|k| k.count
|
108
|
+
!! contents.group_by(&:suit).values.detect{|k| k.count >= 5}
|
111
109
|
end
|
112
110
|
|
113
111
|
def full_house?
|
@@ -124,7 +122,7 @@ module TheGambler
|
|
124
122
|
end
|
125
123
|
|
126
124
|
def royal_flush?
|
127
|
-
contents.sort_by(&:numerical_value).map(&:to_s).join =~ %r{10([SCHD])J\
|
125
|
+
contents.sort_by(&:numerical_value).map(&:to_s).join =~ %r{10([SCHD]).*J\1.*Q\1.*K\1.*A\1}i
|
128
126
|
end
|
129
127
|
|
130
128
|
end
|
@@ -20,6 +20,10 @@ module TheGambler
|
|
20
20
|
it 'can take a Card as its argument' do
|
21
21
|
(->{ Card.new(Card.new rank: 'A', suit: 'D') }).should_not raise_error
|
22
22
|
end
|
23
|
+
|
24
|
+
it 'can take a Fixnum as its argument' do
|
25
|
+
(->{ Card.new 39 }).should_not raise_error
|
26
|
+
end
|
23
27
|
|
24
28
|
describe 'with hash' do
|
25
29
|
Card::RANKS.each do |rank|
|
@@ -57,6 +61,16 @@ module TheGambler
|
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
64
|
+
describe 'with string' do
|
65
|
+
(0..51).each do |i|
|
66
|
+
it "should correctly parse #{i}" do
|
67
|
+
card = Card.new i
|
68
|
+
card.rank.should eq(Card::RANKS[i.zero? ? -1 : i % 13])
|
69
|
+
card.suit.should eq(Card::SUIT_SYMBOLS[i / 13])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
60
74
|
end
|
61
75
|
|
62
76
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module TheGambler
|
4
|
+
describe Hand do
|
5
|
+
context 'nine-card hands' do
|
6
|
+
it 'should recognize a royal flush within a nine-card hand' do
|
7
|
+
Hand.new(*%w{2D QH 3D KH 4C JH QD 10H AH}).royal_flush?.should be_true
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should recognize a straight flush within a nine-card hand' do
|
11
|
+
Hand.new(*%w{9H QH KH 4C JH 10H 2D 4H 7D}).straight_flush?.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'with four of a kind' do
|
15
|
+
it 'should recognize a four of a kind within a nine-card hand' do
|
16
|
+
Hand.new(*%w{9D 9H 9C 4C 9S 10H 5D QD 7D}).four_of_a_kind?.should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should recognize four of a kind and three of a kind at the same time as four of a kind only' do
|
20
|
+
Hand.new(*%w{9D 9H 9C 4C 9S QD 5H 4H 4D}).four_of_a_kind?.should be_true
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with a full house' do
|
25
|
+
it 'should recognize a full house within a nine-card hand' do
|
26
|
+
Hand.new(*%w{9D 3D 2C 9H 9H 4C 4H 10H 7D}).full_house?.should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should recognize a full house within a nine-card hand' do
|
30
|
+
Hand.new(*%w{9D 9H 9H 4C 4H 2D JH 4H 7D}).full_house?.should be_true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should recognize a flush within a nine-card hand' do
|
35
|
+
Hand.new(*%w{9H QH KH 3D 10H 4C 6H 10H 7D}).flush?.should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'with a straight' do
|
39
|
+
it 'should recognize a straight within a nine-card hand' do
|
40
|
+
Hand.new(*%w{9D 3C AD QH KH JC 6H 10H 7D}).straight?.should be_true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should recognize a three of a kind within a nine-card hand' do
|
45
|
+
Hand.new(*%w{QD QH QC 4C 3D 6C JH 10H 7D}).three_of_a_kind?.should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should recognize a two pair within a nine-card hand' do
|
49
|
+
Hand.new(*%w{9D QH 9H 2D QC 4C 4H 10H QD}).two_pair?.should be_true
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should recognize a pair within a nine-card hand' do
|
53
|
+
Hand.new(*%w{KD QH KH 2C 5D 4C JH 10H 7D}).one_pair?.should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should recognize a high card within a nine-card hand' do
|
57
|
+
Hand.new(*%w{9D QH 3C 6D KH 4C 2H 10H 7D}).high_card?.should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
data/the_gambler.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "the_gambler"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["max thom stahl"]
|
12
|
-
s.date = "2012-11-
|
12
|
+
s.date = "2012-11-24"
|
13
13
|
s.description = "\n Really common tasks in programs that use playing cards are a pain to implement. This is my\n implementation. Use it. Or don't.\n "
|
14
14
|
s.email = "max@villainousindustries.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -33,7 +33,6 @@ Gem::Specification.new do |s|
|
|
33
33
|
"lib/the_gambler/hand.rb",
|
34
34
|
"lib/the_gambler/poker.rb",
|
35
35
|
"spec/cards/initialization_spec.rb",
|
36
|
-
"spec/decks/deck_spec.rb",
|
37
36
|
"spec/hands/hands_spec.rb",
|
38
37
|
"spec/hands/poker/hand_flush_spec.rb",
|
39
38
|
"spec/hands/poker/hand_four_of_a_kind_spec.rb",
|
@@ -46,8 +45,8 @@ Gem::Specification.new do |s|
|
|
46
45
|
"spec/hands/poker/hand_three_of_a_kind_spec.rb",
|
47
46
|
"spec/hands/poker/hand_two_pair_spec.rb",
|
48
47
|
"spec/hands/poker/kicker_cards_spec.rb",
|
49
|
-
"spec/hands/
|
50
|
-
"spec/
|
48
|
+
"spec/hands/poker/nine_card_hands_spec.rb",
|
49
|
+
"spec/hands/poker/seven_card_hands_spec.rb",
|
51
50
|
"spec/spec_helper.rb",
|
52
51
|
"spec/support/shared_examples/shared_poker_hands.rb",
|
53
52
|
"spec/the_gambler_spec.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_gambler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -132,7 +132,6 @@ files:
|
|
132
132
|
- lib/the_gambler/hand.rb
|
133
133
|
- lib/the_gambler/poker.rb
|
134
134
|
- spec/cards/initialization_spec.rb
|
135
|
-
- spec/decks/deck_spec.rb
|
136
135
|
- spec/hands/hands_spec.rb
|
137
136
|
- spec/hands/poker/hand_flush_spec.rb
|
138
137
|
- spec/hands/poker/hand_four_of_a_kind_spec.rb
|
@@ -145,8 +144,8 @@ files:
|
|
145
144
|
- spec/hands/poker/hand_three_of_a_kind_spec.rb
|
146
145
|
- spec/hands/poker/hand_two_pair_spec.rb
|
147
146
|
- spec/hands/poker/kicker_cards_spec.rb
|
148
|
-
- spec/hands/
|
149
|
-
- spec/
|
147
|
+
- spec/hands/poker/nine_card_hands_spec.rb
|
148
|
+
- spec/hands/poker/seven_card_hands_spec.rb
|
150
149
|
- spec/spec_helper.rb
|
151
150
|
- spec/support/shared_examples/shared_poker_hands.rb
|
152
151
|
- spec/the_gambler_spec.rb
|
@@ -166,7 +165,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
166
165
|
version: '0'
|
167
166
|
segments:
|
168
167
|
- 0
|
169
|
-
hash:
|
168
|
+
hash: -448834733471940909
|
170
169
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
170
|
none: false
|
172
171
|
requirements:
|
data/spec/decks/deck_spec.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module TheGambler
|
4
|
-
|
5
|
-
describe Deck do
|
6
|
-
let(:deck) { Deck.new }
|
7
|
-
|
8
|
-
describe '#initialize' do
|
9
|
-
it 'should accept cards as arguments' do
|
10
|
-
pending
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should accept an array of cards as arguments' do
|
14
|
-
pending
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
describe '#deal' do
|
20
|
-
it 'should deal the first card in the deck' do
|
21
|
-
pending
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#shuffle' do
|
26
|
-
it 'should change the ordering of the deck in a random manner' do
|
27
|
-
pending
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
data/spec/players/player_spec.rb
DELETED
File without changes
|