poker_hands 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,18 @@
1
+ require 'deck/rank_selector'
1
2
  module Deck
2
3
  class Hand
3
4
  include ::Deck::RankSelector
4
5
  include Comparable
5
6
 
6
- attr_reader :hand, :rank
7
+ attr_reader :cards, :rank
7
8
 
8
- def initialize(hand)
9
- @hand = hand
9
+ def initialize(hand_cards)
10
+ @cards = hand_cards
10
11
  @rank = select_rank
11
12
  end
12
13
 
13
14
  def only_values
14
- @hand.map { |card| card.value}
15
+ @cards.map { |card| card.value}
15
16
  end
16
17
 
17
18
  private
@@ -1,12 +1,13 @@
1
- module Deck::RankSelector
1
+ module Deck
2
+ module RankSelector
2
3
 
3
4
  def is_pair?
4
- only_value = @hand.map { |card| card.value}
5
+ only_value = @cards.map { |card| card.value}
5
6
  only_value.detect { |card| only_value.count(card) > 1}
6
7
  end
7
8
 
8
9
  def is_two_pairs?
9
- only_value = @hand.map { |card| card.value}
10
+ only_value = @cards.map { |card| card.value}
10
11
  first_value = only_value.detect { |card| only_value.count(card) == 2}
11
12
  if first_value
12
13
  only_value.delete(first_value)
@@ -16,23 +17,23 @@ module Deck::RankSelector
16
17
  end
17
18
 
18
19
  def is_three_of_a_kind?
19
- only_value = @hand.map { |card| card.value}
20
+ only_value = @cards.map { |card| card.value}
20
21
  only_value.detect { |card| only_value.count(card) == 3}
21
22
  end
22
23
 
23
24
  def is_straight?
24
- only_value = @hand.map { |card| card.value}
25
+ only_value = @cards.map { |card| card.value}
25
26
  sorted_only_value = only_value.sort
26
27
  only_value === sorted_only_value
27
28
  end
28
29
 
29
30
  def is_flush?
30
- only_suits = @hand.map { |card| card.suit}
31
+ only_suits = @cards.map { |card| card.suit}
31
32
  only_suits.detect { |card| only_suits.count(card) == 5}
32
33
  end
33
34
 
34
35
  def is_full_house?
35
- only_value = @hand.map { |card| card.value}
36
+ only_value = @cards.map { |card| card.value}
36
37
  first_value = only_value.detect { |card| only_value.count(card) == 3}
37
38
  if first_value
38
39
  only_value.delete(first_value)
@@ -42,17 +43,17 @@ module Deck::RankSelector
42
43
  end
43
44
 
44
45
  def is_four_of_a_kind?
45
- only_value = @hand.map { |card| card.value}
46
+ only_value = @cards.map { |card| card.value}
46
47
  only_value.detect { |card| only_value.count(card) == 4}
47
48
  end
48
49
 
49
50
  def is_straight_flush?
50
- only_suits = @hand.map { |card| card.suit}
51
+ only_suits = @cards.map { |card| card.suit}
51
52
  all_same_suit = only_suits.detect { |card| only_suits.count(card) == 5}
52
53
 
53
- only_value = @hand.map { |card| card.value}
54
+ only_value = @cards.map { |card| card.value}
54
55
  sorted_only_value = only_value.sort
55
56
  all_same_suit && only_value === sorted_only_value
56
57
  end
57
-
58
+ end
58
59
  end
@@ -8,7 +8,7 @@ require 'rank/four_of_a_kind'
8
8
  require 'rank/full_house'
9
9
  require 'rank/high_card'
10
10
  require 'rank/pair'
11
- require 'rank/rank'
11
+ require 'rank/hand_rank'
12
12
  require 'rank/straight'
13
13
  require 'rank/straight_flush'
14
14
  require 'rank/three_of_a_kind'
@@ -37,7 +37,7 @@ class PokerHands
37
37
  private
38
38
 
39
39
  def same_best_rank?(rank)
40
- rank.first.rank.class == rank.second.rank.class
40
+ rank[0].rank.class == rank[1].rank.class
41
41
  end
42
42
 
43
43
  def find_winner_if_same_rank(sorted_hands)
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class Flush < Rank
3
+ class Flush < HandRank
3
4
  SCORE = 6
4
5
  end
5
6
  end
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class FourOfAKind < Rank
3
+ class FourOfAKind < HandRank
3
4
  SCORE = 8
4
5
 
5
6
  def <=>(hand)
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class FullHouse < Rank
3
+ class FullHouse < HandRank
3
4
  SCORE = 7
4
5
 
5
6
  def <=>(hand)
@@ -1,12 +1,12 @@
1
1
  module Rank
2
- class Rank
2
+ class HandRank
3
3
  include Comparable
4
4
  def initialize(hand)
5
5
  @hand = hand
6
6
  end
7
7
 
8
8
  def only_values
9
- @hand.hand.map { |card| card.value}
9
+ @hand.cards.map { |card| card.value}
10
10
  end
11
11
 
12
12
  def <=>(hand)
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class HighCard < Rank
3
+ class HighCard < HandRank
3
4
  SCORE = 1
4
5
  end
5
6
  end
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class Pair < Rank
3
+ class Pair < HandRank
3
4
  SCORE = 2
4
5
 
5
6
  def <=>(hand)
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class Straight < Rank
3
+ class Straight < HandRank
3
4
  SCORE = 5
4
5
  end
5
6
  end
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class StraightFlush < Rank
3
+ class StraightFlush < HandRank
3
4
  SCORE = 9
4
5
  end
5
6
  end
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class ThreeOfAKind < Rank
3
+ class ThreeOfAKind < HandRank
3
4
  SCORE = 4
4
5
 
5
6
  def <=>(hand)
@@ -1,5 +1,6 @@
1
+ require 'rank/hand_rank'
1
2
  module Rank
2
- class TwoPairs < Rank
3
+ class TwoPairs < HandRank
3
4
  SCORE = 3
4
5
 
5
6
  def <=>(hand)
@@ -9,10 +9,10 @@ Gem::Specification.new do |s|
9
9
  s.description = "Sample app to play poker"
10
10
  s.authors = ["Cosimo Ranieri"]
11
11
  s.email = 'co.ranieri@gmail.com'
12
- s.files = %w(README.md Rakefile poker-hands.gemspec)
12
+ s.files = %w(README.md Rakefile poker_hands.gemspec)
13
13
  s.files += Dir.glob("lib/**/*")
14
14
  s.files += Dir.glob("spec/**/*")
15
- s.version = '0.0.4'
15
+ s.version = '0.0.5'
16
16
  s.homepage = 'https://rubygems.org/gems/poker-hands'
17
17
  s.license = 'MIT'
18
18
  end
@@ -1,68 +1,67 @@
1
1
  require 'spec_helper'
2
-
3
- describe Hand do
2
+ describe Deck::Hand do
4
3
 
5
4
  describe ".new" do
6
5
  context "the hand is a high card" do
7
- let(:hand) { Hand.new(high_card)}
6
+ let(:hand) { Deck::Hand.new(high_card)}
8
7
  it "creates a high card hand" do
9
- expect(hand.rank.class).to eq HighCard
8
+ expect(hand.rank.class).to eq Rank::HighCard
10
9
  end
11
10
  end
12
11
 
13
12
  context "the hand is a pair" do
14
- let(:hand) { Hand.new(pair)}
13
+ let(:hand) { Deck::Hand.new(pair)}
15
14
  it "creates a high card hand" do
16
- expect(hand.rank.class).to eq Pair
15
+ expect(hand.rank.class).to eq Rank::Pair
17
16
  end
18
17
  end
19
18
 
20
19
  context "the hand is two pairs" do
21
- let(:hand) { Hand.new(two_pairs)}
20
+ let(:hand) { Deck::Hand.new(two_pairs)}
22
21
  it "creates a high card hand" do
23
- expect(hand.rank.class).to eq TwoPairs
22
+ expect(hand.rank.class).to eq Rank::TwoPairs
24
23
  end
25
24
  end
26
25
 
27
26
  context "the hand is three of a kind" do
28
- let(:hand) { Hand.new(three_of_a_kind)}
27
+ let(:hand) { Deck::Hand.new(three_of_a_kind)}
29
28
  it "creates a high card hand" do
30
- expect(hand.rank.class).to eq ThreeOfAKind
29
+ expect(hand.rank.class).to eq Rank::ThreeOfAKind
31
30
  end
32
31
  end
33
32
 
34
33
  context "the hand is straight" do
35
- let(:hand) { Hand.new(straight)}
34
+ let(:hand) { Deck::Hand.new(straight)}
36
35
  it "creates a high card hand" do
37
- expect(hand.rank.class).to eq Straight
36
+ expect(hand.rank.class).to eq Rank::Straight
38
37
  end
39
38
  end
40
39
 
41
40
  context "the hand is a flush" do
42
- let(:hand) { Hand.new(flush)}
41
+ let(:hand) { Deck::Hand.new(flush)}
43
42
  it "creates a flush hand" do
44
- expect(hand.rank.class).to eq Flush
43
+ expect(hand.rank.class).to eq Rank::Flush
45
44
  end
46
45
  end
47
46
 
48
47
  context "the hand is a full house" do
49
- let(:hand) { Hand.new(full_house)}
48
+ let(:hand) { Deck::Hand.new(full_house)}
50
49
  it "creates a flush full house" do
51
- expect(hand.rank.class).to eq FullHouse
50
+ expect(hand.rank.class).to eq Rank::FullHouse
52
51
  end
53
52
  end
54
53
 
55
54
  context "the hand is a four of a kind" do
56
- let(:hand) { Hand.new(four_of_a_kind)}
55
+ let(:hand) { Deck::Hand.new(four_of_a_kind)}
57
56
  it "creates a four of a kind full house" do
58
- expect(hand.rank.class).to eq FourOfAKind
57
+ expect(hand.rank.class).to eq Rank::FourOfAKind
59
58
  end
60
59
  end
61
60
 
62
61
  context "the hand is a straight flush" do
63
- let(:hand) { Hand.new(straight_flush)}
62
+ let(:hand) { Deck::Hand.new(straight_flush)}
64
63
  it "creates a four of a straight flush" do
65
- expect(hand.rank.class).to eq StraightFlush
64
+ expect(hand.rank.class).to eq Rank::StraightFlush
66
65
  end
67
66
  end
68
67
 
@@ -1,187 +1,186 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Table do
3
+ describe PokerHands do
4
4
  describe "#winner" do
5
- let(:table) { Table.new}
6
- context "the table contains a straight flush as best hand" do
5
+ let(:poker) { PokerHands.new}
6
+ context "the game contains a straight flush as best hand" do
7
7
  context "there is only one straight flush hand" do
8
8
  before do
9
- table.hands = hand_straight_flush
9
+ poker.hands = hand_straight_flush
10
10
  end
11
11
  it "return an array where the last element is a straight flush " do
12
- expect(table.winner.rank.class).to eq StraightFlush
12
+ expect(poker.winner.rank.class).to eq Rank::StraightFlush
13
13
  end
14
14
  end
15
15
 
16
16
  context "there are two straight flush hands" do
17
17
  before do
18
- table.hands = hand_two_straight_flush
18
+ poker.hands = hand_two_straight_flush
19
19
  end
20
20
  it "return an array where the last element is the highest straight flush " do
21
- only_val = Hand.new(table.winner.hand).only_values
21
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
22
22
  expect(only_val.max).to eq "8"
23
23
  end
24
24
  end
25
25
 
26
26
  end
27
27
 
28
- context "the table contains a four of a kind as best hand" do
28
+ context "the game contains a four of a kind as best hand" do
29
29
  context "there is one four of a kind hand" do
30
30
  before do
31
- table.hands = hand_four_of_a_kind
31
+ poker.hands = hand_four_of_a_kind
32
32
  end
33
33
  it "return an array where the last element is a four of a kind " do
34
- expect(table.winner.rank.class).to eq FourOfAKind
34
+ expect(poker.winner.rank.class).to eq Rank::FourOfAKind
35
35
  end
36
36
  end
37
37
 
38
38
  context "there are two four of a kind hands" do
39
39
  before do
40
- table.hands = hand_same_four_of_a_kind
40
+ poker.hands = hand_same_four_of_a_kind
41
41
  end
42
42
  it "return an array where the last element is a four of a kind " do
43
- only_val = Hand.new(table.winner.hand).only_values
43
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
44
44
  expect(only_val.max).to eq "5"
45
45
  end
46
46
  end
47
47
  end
48
48
 
49
- context "the table contains a full house as best hand" do
49
+ context "the game contains a full house as best hand" do
50
50
  context "there is only one full house" do
51
51
  before do
52
- table.hands = hand_full_house
52
+ poker.hands = hand_full_house
53
53
  end
54
54
  it "return an array where the last element is a full house hand " do
55
- expect(table.winner.rank.class).to eq FullHouse
55
+ expect(poker.winner.rank.class).to eq Rank::FullHouse
56
56
  end
57
57
  end
58
58
 
59
59
  context "there are two full house hands" do
60
60
  before do
61
- table.hands = hand_same_full_house
61
+ poker.hands = hand_same_full_house
62
62
  end
63
63
  it "return an array where the last element is highest full house " do
64
- only_val = Hand.new(table.winner.hand).only_values
65
- p only_val
64
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
66
65
  expect(only_val.max).to eq "5"
67
66
  end
68
67
  end
69
68
 
70
69
  end
71
70
 
72
- context "the table contains a flush as best hand" do
71
+ context "the game contains a flush as best hand" do
73
72
  context "there is only one flush hand" do
74
73
  before do
75
- table.hands = hand_flush
74
+ poker.hands = hand_flush
76
75
  end
77
76
  it "return an array where the last element is a flush " do
78
- expect(table.winner.rank.class).to eq Flush
77
+ expect(poker.winner.rank.class).to eq Rank::Flush
79
78
  end
80
79
  end
81
80
 
82
81
  context "there is two one flush hand" do
83
82
  before do
84
- table.hands = hand_two_flush
83
+ poker.hands = hand_two_flush
85
84
  end
86
85
  it "return an array where the last element is the highest flush " do
87
- only_val = Hand.new(table.winner.hand).only_values
86
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
88
87
  expect(only_val.max).to eq "8"
89
88
  end
90
89
  end
91
90
 
92
91
  end
93
92
 
94
- context "the table contains a straight as best hand" do
93
+ context "the game contains a straight as best hand" do
95
94
  context "there is only one straight hand" do
96
95
  before do
97
- table.hands = hand_straight
96
+ poker.hands = hand_straight
98
97
  end
99
98
  it "return an array where the last element is a straight " do
100
- expect(table.winner.rank.class).to eq Straight
99
+ expect(poker.winner.rank.class).to eq Rank::Straight
101
100
  end
102
101
  end
103
102
 
104
103
  context "there are two straight hands" do
105
104
  before do
106
- table.hands = hand_same_straight
105
+ poker.hands = hand_same_straight
107
106
  end
108
107
  it "return an array where the last element the highest straight" do
109
- only_val = Hand.new(table.winner.hand).only_values
108
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
110
109
  expect(only_val.last).to eq "8"
111
110
  end
112
111
  end
113
112
 
114
113
  end
115
114
 
116
- context "the table contains a three of a kind as best hand" do
115
+ context "the game contains a three of a kind as best hand" do
117
116
  context "there is only one three of a kind hand" do
118
117
  before do
119
- table.hands = hand_three_of_a_kind
118
+ poker.hands = hand_three_of_a_kind
120
119
  end
121
120
  it "return an array where the last element is a three of a kind " do
122
- expect(table.winner.rank.class).to eq ThreeOfAKind
121
+ expect(poker.winner.rank.class).to eq Rank::ThreeOfAKind
123
122
  end
124
123
  end
125
124
 
126
125
  context "there are two three of a kind hands" do
127
126
  before do
128
- table.hands = hand_some_three_of_a_kind
127
+ poker.hands = hand_some_three_of_a_kind
129
128
  end
130
129
  it "return an array where the last element is a three of a kind " do
131
- only_val = Hand.new(table.winner.hand).only_values
130
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
132
131
  expect(only_val.last).to eq "8"
133
132
  end
134
133
  end
135
134
  end
136
135
 
137
- context "the table contains a two pairs as best hand" do
136
+ context "the game contains a two pairs as best hand" do
138
137
  before do
139
- table.hands = hand_two_pairs
138
+ poker.hands = hand_two_pairs
140
139
  end
141
140
  it "return an array where the last element is a two pairs " do
142
- expect(table.winner.rank.class).to eq TwoPairs
141
+ expect(poker.winner.rank.class).to eq Rank::TwoPairs
143
142
  end
144
143
 
145
- context "the tables contains thw hands with same two pairs" do
144
+ context "the game contains thw hands with same two pairs" do
146
145
  before do
147
- table.hands = hand_same_two_pairs
146
+ poker.hands = hand_same_two_pairs
148
147
  end
149
148
  it "return an array where the last element is the two pairs with the highest remaining card" do
150
- only_val = Hand.new(table.winner.hand).only_values
149
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
151
150
  expect(only_val.last).to eq "8"
152
151
  end
153
152
  end
154
153
  end
155
154
 
156
- context "the table contains a pair as best hand" do
155
+ context "the game contains a pair as best hand" do
157
156
  context "the tables contains a unique pair hand" do
158
157
  before do
159
- table.hands = hand_pair
158
+ poker.hands = hand_pair
160
159
  end
161
160
  it "return an array where the last element is a pair " do
162
- expect(table.winner.rank.class).to eq Pair
161
+ expect(poker.winner.rank.class).to eq Rank::Pair
163
162
  end
164
163
  end
165
164
 
166
- context "the tables contains the hands with same pairs" do
165
+ context "the game contains the hands with same pairs" do
167
166
  before do
168
- table.hands = hand_same_pair
167
+ poker.hands = hand_same_pair
169
168
  end
170
169
  it "return an array where the last element is the pair with the highest remaining card" do
171
- only_val = Hand.new(table.winner.hand).only_values
170
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
172
171
  expect(only_val.last).to eq "8"
173
172
  end
174
173
  end
175
174
 
176
175
  end
177
176
 
178
- context "the table contains a high card as best hand" do
177
+ context "the game contains a high card as best hand" do
179
178
  context "the highest cards have the same value" do
180
179
  before do
181
- table.hands = hand_high_card_same_highest
180
+ poker.hands = hand_high_card_same_highest
182
181
  end
183
182
  it "return an array where the last element is a high card with the next highest value" do
184
- only_val = Hand.new(table.winner.hand).only_values
183
+ only_val = Deck::Hand.new(poker.winner.hand).only_values
185
184
  only_val.sort!
186
185
  expect(only_val[only_val.length - 2]).to eq "7"
187
186
  end
@@ -189,10 +188,10 @@ describe Table do
189
188
 
190
189
  context "the highest card is unique" do
191
190
  before do
192
- table.hands = hand_high_card
191
+ poker.hands = hand_high_card
193
192
  end
194
193
  it "return an array where the last element is a high card " do
195
- expect(table.winner.rank.class).to eq HighCard
194
+ expect(poker.winner.rank.class).to eq Rank::HighCard
196
195
  end
197
196
  end
198
197