hands 0.0.2 → 0.1.0

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.
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class HandTest < Hands::TestCase
4
+ def test_ordering_hands
5
+ pair = Hands::Hand.new
6
+ pair << Hands::Card[2, :hearts]
7
+ pair << Hands::Card[2, :clubs]
8
+
9
+ flush = Hands::Hand.new
10
+ flush << Hands::Card[6, :hearts]
11
+ flush << Hands::Card[7, :hearts]
12
+ flush << Hands::Card[8, :hearts]
13
+ flush << Hands::Card[2, :hearts]
14
+ flush << Hands::Card[4, :hearts]
15
+ assert flush > pair
16
+ end
17
+
18
+ def test_order_same_hand
19
+ small_pair = Hands::Hand.new
20
+ small_pair << Hands::Card[2, :hearts]
21
+ small_pair << Hands::Card[2, :clubs]
22
+
23
+ large_pair = Hands::Hand.new
24
+ large_pair << Hands::Card['A', :hearts]
25
+ large_pair << Hands::Card['A', :clubs]
26
+ assert large_pair > small_pair
27
+
28
+ small_kicker = Hands::Hand.new
29
+ small_kicker << Hands::Card['A', :spades]
30
+ small_kicker << Hands::Card['A', :diamonds]
31
+ small_kicker << Hands::Card[2, :diamonds]
32
+ small_kicker << Hands::Card[3, :diamonds]
33
+ small_kicker << Hands::Card[4, :diamonds]
34
+
35
+ big_kicker = Hands::Hand.new
36
+ big_kicker << Hands::Card['A', :hearts]
37
+ big_kicker << Hands::Card['A', :clubs]
38
+ big_kicker << Hands::Card[10, :diamonds]
39
+ big_kicker << Hands::Card[9, :diamonds]
40
+ big_kicker << Hands::Card[7, :diamonds]
41
+ assert big_kicker > small_kicker
42
+ end
43
+
44
+ def test_collecting_suits
45
+ hand = Hands::Hand.new
46
+ hand << Hands::Card[2, :hearts]
47
+ hand << Hands::Card[2, :clubs]
48
+ assert_equal [:hearts, :clubs], hand.suits
49
+
50
+ hand << Hands::Card[3, :clubs]
51
+ assert_equal [:hearts, :clubs], hand.suits
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class PlayerTest < Hands::TestCase
4
+ def test_initialization
5
+ player1 = Hands::Player.new
6
+ refute player1.name
7
+
8
+ player2 = Hands::Player.new('Sam')
9
+ assert_equal 'Sam', player2.name
10
+ end
11
+ end
@@ -0,0 +1,75 @@
1
+ require 'test_helper'
2
+
3
+ class TableTest < Hands::TestCase
4
+ def setup
5
+ @sam = Hands::Player.new('Sam')
6
+ @ian = Hands::Player.new('Ian')
7
+ @steve = Hands::Player.new('Steve')
8
+
9
+ @table = Hands::Table.new
10
+ @table.players = [@sam, @ian, @steve]
11
+ end
12
+
13
+ def test_dealing
14
+ @table.deal_player_cards!
15
+ assert_equal 2, @sam.hand.cards.length
16
+ assert_equal 2, @ian.hand.cards.length
17
+ assert_equal 2, @steve.hand.cards.length
18
+ assert_equal 46, @table.deck.cards.length
19
+ end
20
+
21
+ def test_dealing_with_position
22
+ cards = @table.deck.cards.dup
23
+ @table.deal_player_cards!
24
+ assert_equal [cards.slice(-3, 1), cards.slice(-6, 1)].flatten, @sam.hand.cards
25
+ assert_equal [cards.slice(-1, 1), cards.slice(-4, 1)].flatten, @ian.hand.cards
26
+ assert_equal [cards.slice(-2, 1), cards.slice(-5, 1)].flatten, @steve.hand.cards
27
+
28
+ @table.new_hand!
29
+ cards = @table.deck.cards.dup
30
+ @table.deal_player_cards!
31
+ assert_equal [cards.slice(-2, 1), cards.slice(-5, 1)].flatten, @sam.hand.cards
32
+ assert_equal [cards.slice(-3, 1), cards.slice(-6, 1)].flatten, @ian.hand.cards
33
+ assert_equal [cards.slice(-1, 1), cards.slice(-4, 1)].flatten, @steve.hand.cards
34
+
35
+ @table.new_hand!
36
+ cards = @table.deck.cards.dup
37
+ @table.deal_player_cards!
38
+ assert_equal [cards.slice(-1, 1), cards.slice(-4, 1)].flatten, @sam.hand.cards
39
+ assert_equal [cards.slice(-2, 1), cards.slice(-5, 1)].flatten, @ian.hand.cards
40
+ assert_equal [cards.slice(-3, 1), cards.slice(-6, 1)].flatten, @steve.hand.cards
41
+ end
42
+
43
+ def test_dealing_the_flop
44
+ @table.deal_player_cards!
45
+ @table.deal_flop!
46
+ assert_equal 42, @table.deck.cards.length
47
+ end
48
+
49
+ def test_dealing_the_turn
50
+ @table.deal_player_cards!
51
+ @table.deal_flop!
52
+ @table.deal_turn!
53
+ assert_equal 40, @table.deck.cards.length
54
+ end
55
+
56
+ def test_dealing_the_river
57
+ @table.deal_player_cards!
58
+ @table.deal_flop!
59
+ @table.deal_turn!
60
+ @table.deal_river!
61
+ assert_equal 38, @table.deck.cards.length
62
+ end
63
+
64
+ def test_resetting
65
+ @table.deal_player_cards!
66
+ @table.deal_flop!
67
+ @table.deal_turn!
68
+ @table.deal_river!
69
+ @table.new_hand!
70
+ assert_equal 0, @sam.hand.cards.length
71
+ assert_equal 0, @ian.hand.cards.length
72
+ assert_equal 0, @steve.hand.cards.length
73
+ assert_equal 52, @table.deck.cards.length
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hands
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,29 +9,39 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-23 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Simple library for various poker hands calculations.
15
15
  email:
16
- - sam@samsoff.es
16
+ - sam@soff.es
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
+ - .yardopts
22
23
  - Gemfile
23
24
  - LICENSE
24
25
  - Rakefile
25
26
  - Readme.markdown
27
+ - Todo.markdown
26
28
  - hands.gemspec
27
29
  - lib/hands.rb
28
30
  - lib/hands/card.rb
31
+ - lib/hands/deck.rb
29
32
  - lib/hands/hand.rb
33
+ - lib/hands/hand_detection.rb
34
+ - lib/hands/player.rb
35
+ - lib/hands/table.rb
30
36
  - lib/hands/version.rb
31
- - spec/models/card_spec.rb
32
- - spec/models/hand_spec.rb
33
- - spec/spec_helper.rb
34
- homepage: http://github.com/samsoffes/hands
37
+ - test/test_helper.rb
38
+ - test/units/card_test.rb
39
+ - test/units/deck_test.rb
40
+ - test/units/hand_detection_test.rb
41
+ - test/units/hand_test.rb
42
+ - test/units/player_test.rb
43
+ - test/units/table_test.rb
44
+ homepage: http://github.com/soffes/hands
35
45
  licenses: []
36
46
  post_install_message:
37
47
  rdoc_options: []
@@ -43,19 +53,30 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
53
  - - ! '>='
44
54
  - !ruby/object:Gem::Version
45
55
  version: '0'
56
+ segments:
57
+ - 0
58
+ hash: 3489635347020004355
46
59
  required_rubygems_version: !ruby/object:Gem::Requirement
47
60
  none: false
48
61
  requirements:
49
62
  - - ! '>='
50
63
  - !ruby/object:Gem::Version
51
64
  version: '0'
65
+ segments:
66
+ - 0
67
+ hash: 3489635347020004355
52
68
  requirements: []
53
69
  rubyforge_project:
54
- rubygems_version: 1.8.11
70
+ rubygems_version: 1.8.23
55
71
  signing_key:
56
72
  specification_version: 3
57
73
  summary: Simple library for various poker hands calculations.
58
74
  test_files:
59
- - spec/models/card_spec.rb
60
- - spec/models/hand_spec.rb
61
- - spec/spec_helper.rb
75
+ - test/test_helper.rb
76
+ - test/units/card_test.rb
77
+ - test/units/deck_test.rb
78
+ - test/units/hand_detection_test.rb
79
+ - test/units/hand_test.rb
80
+ - test/units/player_test.rb
81
+ - test/units/table_test.rb
82
+ has_rdoc:
@@ -1,74 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hands::Card do
4
- it 'should validate cards' do
5
- card = Hands::Card.new
6
- card.is_valid?.should eq(false)
7
- card.is_invalid?.should eq(true)
8
-
9
- card.suite = :hearts
10
- card.is_valid?.should eq(false)
11
-
12
- card.value = 9
13
- card.is_valid?.should eq(true)
14
-
15
- card.suite = 17
16
- card.is_valid?.should eq(false)
17
- end
18
-
19
- it 'should allow for integers instead of characters for high cards' do
20
- card1 = Hands::Card.new(:value => 11, :suite => :clubs)
21
- card1.is_valid?.should eq(true)
22
- card1.value.should eq('j')
23
-
24
- card2 = Hands::Card.new(:value => 'j', :suite => :clubs)
25
- card2.should eq(card1)
26
- end
27
-
28
- it 'should not allow invalid values' do
29
- card = Hands::Card.new
30
- card.value = 'p'
31
- card.value.should eql(nil)
32
- end
33
-
34
- it 'should be comparable' do
35
- card1 = Hands::Card.new(:value => 2, :suite => :hearts)
36
- card2 = Hands::Card.new(:value => 3, :suite => :clubs)
37
-
38
- (card2 > card1).should eq(true)
39
- (card1 < card2).should eq(true)
40
-
41
- card1.value = 3
42
- (card1 == card2).should eq(true)
43
- (card1.<=>(card2, true)).should eq(1)
44
- (card2.<=>(card1, true)).should eq(-1)
45
- end
46
-
47
- it 'should be sortable' do
48
- c2 = Hands::Card.new(:value => 2, :suite => :hearts)
49
- c3 = Hands::Card.new(:value => 3, :suite => :hearts)
50
- c4 = Hands::Card.new(:value => 4, :suite => :hearts)
51
- c5 = Hands::Card.new(:value => 5, :suite => :hearts)
52
- c6 = Hands::Card.new(:value => 6, :suite => :hearts)
53
- c7 = Hands::Card.new(:value => 7, :suite => :hearts)
54
- c8 = Hands::Card.new(:value => 8, :suite => :hearts)
55
- c9 = Hands::Card.new(:value => 9, :suite => :hearts)
56
- c10 = Hands::Card.new(:value => 10, :suite => :hearts)
57
- cJ = Hands::Card.new(:value => 'j', :suite => :hearts)
58
- cQ = Hands::Card.new(:value => 'q', :suite => :hearts)
59
- cK = Hands::Card.new(:value => 'k', :suite => :hearts)
60
- cA = Hands::Card.new(:value => 'a', :suite => :hearts)
61
-
62
- cards = [c2, c3, c4, c5, c6, c7, c8, c9, c10, cJ, cQ, cK, cA]
63
- cards.sort.should eq(cards)
64
- end
65
-
66
- it 'should include description in inspect' do
67
- card = Hands::Card[2, :hearts]
68
- card.inspect.include?('Two of Hearts').should eql(true)
69
-
70
- card = Hands::Card.new
71
- card.description.should eql('invalid')
72
- card.inspect.include?('invalid').should eql(false)
73
- end
74
- end
@@ -1,181 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Hands::Hand do
4
- it 'should order different types of hands' do
5
- pair = Hands::Hand.new
6
- pair << Hands::Card[2, :hearts]
7
- pair << Hands::Card[2, :clubs]
8
-
9
- flush = Hands::Hand.new
10
- flush << Hands::Card[6, :hearts]
11
- flush << Hands::Card[7, :hearts]
12
- flush << Hands::Card[8, :hearts]
13
- flush << Hands::Card[2, :hearts]
14
- flush << Hands::Card[4, :hearts]
15
- (flush > pair).should eq(true)
16
- end
17
-
18
- it 'should order same types of hands' do
19
- small_pair = Hands::Hand.new
20
- small_pair << Hands::Card[2, :hearts]
21
- small_pair << Hands::Card[2, :clubs]
22
-
23
- large_pair = Hands::Hand.new
24
- large_pair << Hands::Card['A', :hearts]
25
- large_pair << Hands::Card['A', :clubs]
26
- (large_pair > small_pair).should eq(true)
27
-
28
- small_kicker = Hands::Hand.new
29
- small_kicker << Hands::Card['A', :spades]
30
- small_kicker << Hands::Card['A', :diamonds]
31
- small_kicker << Hands::Card[2, :diamonds]
32
- small_kicker << Hands::Card[3, :diamonds]
33
- small_kicker << Hands::Card[4, :diamonds]
34
-
35
- big_kicker = Hands::Hand.new
36
- big_kicker << Hands::Card['A', :hearts]
37
- big_kicker << Hands::Card['A', :clubs]
38
- big_kicker << Hands::Card[10, :diamonds]
39
- big_kicker << Hands::Card[9, :diamonds]
40
- big_kicker << Hands::Card[7, :diamonds]
41
- (big_kicker > small_kicker).should eq(true)
42
- end
43
-
44
- it 'should collect suites' do
45
- hand = Hands::Hand.new
46
- hand << Hands::Card[2, :hearts]
47
- hand << Hands::Card[2, :clubs]
48
- hand.suites.should eq([:hearts, :clubs])
49
-
50
- hand << Hands::Card[3, :clubs]
51
- hand.suites.should eq([:hearts, :clubs])
52
- end
53
-
54
- it 'should recognize high card' do
55
- hand = Hands::Hand.new
56
- hand << Hands::Card[2, :hearts]
57
- hand << Hands::Card[9, :clubs]
58
- hand << Hands::Card[7, :hearts]
59
- hand << Hands::Card['a', :spades]
60
- hand << Hands::Card[4, :diamonds]
61
- hand.high_card.collect(&:value).should eq(['a', 9, 7, 4, 2])
62
- hand.pair.should eql(nil)
63
- hand.two_pair.should eql(nil)
64
- end
65
-
66
- it 'should recognize a pair' do
67
- hand = Hands::Hand.new
68
- hand << Hands::Card[9, :hearts]
69
- hand << Hands::Card[9, :clubs]
70
- hand << Hands::Card[7, :hearts]
71
- hand << Hands::Card[2, :spades]
72
- hand << Hands::Card[4, :diamonds]
73
- hand.pair.collect(&:value).should eq([9, 9, 7, 4, 2])
74
- hand.two_pair.should eql(nil)
75
- end
76
-
77
- it 'should recognize two pair' do
78
- hand = Hands::Hand.new
79
- hand << Hands::Card[7, :hearts]
80
- hand << Hands::Card[7, :spades]
81
- hand << Hands::Card[4, :diamonds]
82
- hand << Hands::Card[9, :hearts]
83
- hand << Hands::Card[9, :clubs]
84
- hand.two_pair.collect(&:value).should eq([9, 9, 7, 7, 4])
85
- hand.two_pair.should eql(hand.pair)
86
- end
87
-
88
- it 'should recognize three of a kind' do
89
- hand = Hands::Hand.new
90
- hand << Hands::Card[7, :hearts]
91
- hand << Hands::Card[7, :spades]
92
- hand << Hands::Card[7, :diamonds]
93
- hand << Hands::Card[3, :hearts]
94
- hand << Hands::Card[9, :clubs]
95
- hand.best_hand[:type].should eq('three_of_a_kind')
96
- end
97
-
98
- it 'should recognize a straight' do
99
- hand1 = Hands::Hand.new
100
- hand1 << Hands::Card[2, :hearts]
101
- hand1 << Hands::Card[3, :spades]
102
- hand1 << Hands::Card[4, :diamonds]
103
- hand1 << Hands::Card[5, :hearts]
104
- hand1 << Hands::Card[6, :clubs]
105
- hand1.best_hand[:type].should eq('straight')
106
-
107
- hand2 = Hands::Hand.new
108
- hand2 << Hands::Card['A', :hearts]
109
- hand2 << Hands::Card[2, :spades]
110
- hand2 << Hands::Card[3, :diamonds]
111
- hand2 << Hands::Card[4, :hearts]
112
- hand2 << Hands::Card[5, :clubs]
113
- hand2.best_hand[:type].should eq('straight')
114
-
115
- hand3 = Hands::Hand.new
116
- hand3 << Hands::Card[10, :hearts]
117
- hand3 << Hands::Card['J', :spades]
118
- hand3 << Hands::Card['Q', :diamonds]
119
- hand3 << Hands::Card['K', :hearts]
120
- hand3 << Hands::Card['A', :clubs]
121
- hand3.best_hand[:type].should eq('straight')
122
-
123
- hand4 = Hands::Hand.new
124
- hand4 << Hands::Card['J', :spades]
125
- hand4 << Hands::Card['Q', :diamonds]
126
- hand4 << Hands::Card['K', :hearts]
127
- hand4 << Hands::Card['A', :clubs]
128
- hand4 << Hands::Card[2, :hearts]
129
- hand4.best_hand[:type].should eq('high_card')
130
- end
131
-
132
- it 'should recognize a flush' do
133
- hand = Hands::Hand.new
134
- hand << Hands::Card[6, :hearts]
135
- hand << Hands::Card[7, :hearts]
136
- hand << Hands::Card[8, :hearts]
137
- hand << Hands::Card[2, :hearts]
138
- hand << Hands::Card[4, :hearts]
139
- hand.best_hand[:type].should eq('flush')
140
- end
141
-
142
- it 'should recognize a full house' do
143
- hand = Hands::Hand.new
144
- hand << Hands::Card[7, :hearts]
145
- hand << Hands::Card[7, :spades]
146
- hand << Hands::Card[7, :diamonds]
147
- hand << Hands::Card[9, :spades]
148
- hand << Hands::Card[9, :clubs]
149
- hand.best_hand[:type].should eq('full_house')
150
- end
151
-
152
- it 'should recognize four of a kind' do
153
- hand = Hands::Hand.new
154
- hand << Hands::Card[7, :hearts]
155
- hand << Hands::Card[7, :spades]
156
- hand << Hands::Card[7, :diamonds]
157
- hand << Hands::Card[7, :clubs]
158
- hand << Hands::Card[9, :clubs]
159
- hand.best_hand[:type].should eq('four_of_a_kind')
160
- end
161
-
162
- it 'should recognize a straight flush' do
163
- hand = Hands::Hand.new
164
- hand << Hands::Card[2, :hearts]
165
- hand << Hands::Card[3, :hearts]
166
- hand << Hands::Card[4, :hearts]
167
- hand << Hands::Card[5, :hearts]
168
- hand << Hands::Card[6, :hearts]
169
- hand.best_hand[:type].should eq('straight_flush')
170
- end
171
-
172
- it 'should recognize the best hand' do
173
- hand = Hands::Hand.new
174
- hand << Hands::Card[7, :hearts]
175
- hand << Hands::Card[7, :spades]
176
- hand << Hands::Card[4, :diamonds]
177
- hand << Hands::Card[9, :hearts]
178
- hand << Hands::Card[9, :clubs]
179
- hand.best_hand[:type].should eq('two_pair')
180
- end
181
- end