Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding cucumber to the guard file, making cards polymorphically "dea…

…lable" adjusting player and hand to become dealable and updating tests to support
  • Loading branch information...
commit 76ae28afca8666795003c2d3ae4c20a4a1ba47d6 1 parent e55f26c
Ben Feigin authored
View
6 Guardfile
@@ -21,3 +21,9 @@ end
guard :bundler do
watch('Gemfile')
end
+
+guard 'cucumber' do
+ watch(%r{^features/.+\.feature$})
+ watch(%r{^features/support/.+$}) { 'features' }
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
+end
View
9 db/schema.rb
@@ -59,11 +59,12 @@
end
create_table "cards", :force => true do |t|
- t.string "value"
- t.string "suit"
- t.integer "player_id"
- t.integer "hand_id"
+ t.string :value, :null => false
+ t.string :suit, :null => false
+ t.references :dealable, :polymorphic => true, :null => false
+ t.integer :hand_id, :null => false
end
+ add_index "cards", [:value, :suit, :hand_id], :name => 'non_duplicate_cards_per_hand', :unique => true
create_table "accounts", :force => true do |t|
t.string "name"
View
4 features/betting_rounds.feature
@@ -26,3 +26,7 @@ Feature: Betting rounds
And Alice folds
Then the round should be over
When the hand plays out
+ Then the hand should have 5 community cards
+ Then each player should have 2 pocket cards
+ Then the hand should have community and pocket cards
+
View
14 features/step_definitions/game_steps.rb
@@ -50,6 +50,20 @@
@hand.expects(:next_turn).in_sequence(@seq)
end
+Then /^the hand should have (\d+) community cards$/ do |card_count|
+ @hand.community_cards.size.should == card_count.to_i
+end
+
+Then /^each player should have (\d+) pocket cards$/ do |card_count|
+ @players.each do |name, player|
+ player.cards.where(:hand_id => @hand).size.should == card_count.to_i
+ end
+end
+
+Then /^the hand should have community and pocket cards$/ do
+ @hand.cards.size.should == (@players.size * 2) + 5
+end
+
# This will end the scenario!
Then /the round should be over/ do
@hand.expects(:close_hand!).in_sequence(@seq)
View
5 models/cards.rb
@@ -1,6 +1,7 @@
class Card < ActiveRecord::Base
- belongs_to :player
- belongs_to :hand
+ belongs_to :dealable, :polymorphic => true
+ belongs_to :hand, :foreign_key => 'hand_id'
+
def ace?
face_value == 14
View
41 models/hand.rb
@@ -1,6 +1,9 @@
class Hand < ActiveRecord::Base
belongs_to :game_table
+ has_many :cards
+ has_many :community_cards, :as => :dealable, :class_name => 'Card'
+
has_many :rounds do
def currently_open
where(:open => true).first
@@ -14,26 +17,50 @@ def deck
def play!
save
- deal_pocket_cards!
+ deal_pocket_cards
while next_round do
rounds.currently_open.play!
+ deal_community_cards
end
close_hand!
end
- def deal_pocket_cards!
+ def next_round
+ if next_betting_phase
+ rounds.create(:betting_phase => next_betting_phase, :open => true, :hand => self)
+ else
+ false
+ end
+ end
+
+ def deal_pocket_cards
2.times do
active_players.each do |player|
- Card.create(deck.card!.merge(:player => player, :hand => self))
+ deal_card(player)
end
end
end
- def next_round
- if next_betting_phase
- rounds.create(:betting_phase => next_betting_phase, :open => true, :hand => self)
+ def add_community_card
+ puts "adding community card"
+ puts deal_card(self)
+ end
+
+ def deal_card(dealable)
+ Card.create(deck.card!.merge(:dealable => dealable, :hand_id => id))
+ end
+
+ def deal_community_cards
+ deck.burn!
+ case rounds.last.betting_phase
+ when 'pre_flop'
+ 3.times do
+ add_community_card
+ end
+ when 'river'
+ return
else
- false
+ add_community_card
end
end
View
1  models/player.rb
@@ -4,6 +4,7 @@ class Player < ActiveRecord::Base
belongs_to :tournament
has_one :seating
has_many :actions
+ has_many :cards, :as => :dealable
has_and_belongs_to_many :game_tables, :join_table => :seatings do
def current_table
View
1  spec/factories.rb
@@ -43,5 +43,4 @@
betting_phase 'flop'
hand
end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.