Class: RubyHoldem::Round

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby_holdem/round.rb

Constant Summary collapse

STAGES =
%w(pre_flop flop turn river show_down)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(players, small_blinds, big_blinds) ⇒ Round

Returns a new instance of Round.



20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_holdem/round.rb', line 20

def initialize(players, small_blinds, big_blinds)
  @small_blinds, @big_blinds = small_blinds, big_blinds
  @current_stage = STAGES[0]
  @pot_amount = 0
  @action_history = []

  @players = players.map { |player| RoundPlayer.new(player) }
  @dealer = Dealer.new
  @dealer.deal_hole_cards(@players)
end

Instance Attribute Details

#action_historyObject (readonly)

Returns the value of attribute action_history.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def action_history
  @action_history
end

#big_blindsObject (readonly)

Returns the value of attribute big_blinds.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def big_blinds
  @big_blinds
end

#current_stageObject (readonly)

Returns the value of attribute current_stage.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def current_stage
  @current_stage
end

#dealerObject (readonly)

Returns the value of attribute dealer.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def dealer
  @dealer
end

#playersObject (readonly)

Returns the value of attribute players.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def players
  @players
end

#pot_amountObject (readonly)

Returns the value of attribute pot_amount.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def pot_amount
  @pot_amount
end

#small_blindsObject (readonly)

Returns the value of attribute small_blinds.



7
8
9
# File 'lib/ruby_holdem/round.rb', line 7

def small_blinds
  @small_blinds
end

Instance Method Details

#has_winner?Boolean

Returns:



60
61
62
# File 'lib/ruby_holdem/round.rb', line 60

def has_winner?
  (current_stage == 'show_down' || players_still_in_round.count == 1)
end

#highest_bet_placedObject



93
94
95
# File 'lib/ruby_holdem/round.rb', line 93

def highest_bet_placed
  players_still_in_round.max_by(&:current_bet_amount).current_bet_amount
end

#last_moveObject



97
98
99
# File 'lib/ruby_holdem/round.rb', line 97

def last_move
  action_history.last
end

#make_move(move, amount = nil) ⇒ Object

TODO: Extract the code relating to making a move into its own class to separate the logic

behind making a move and the game state methods


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_holdem/round.rb', line 33

def make_move(move, amount=nil)
  if turns_played == 0
    apply_bet(small_blinds)
  elsif turns_played == 1
    apply_bet(big_blinds)
  elsif move == 'bet'
    apply_bet(amount)
  elsif move == 'call'
    apply_call
  elsif move == 'fold'
    apply_fold
  end
end

#next_stageObject

Raises:



47
48
49
50
51
52
# File 'lib/ruby_holdem/round.rb', line 47

def next_stage
  raise StandardError unless ready_for_next_stage? && @current_stage != 'show_down'

  @current_stage = STAGES[STAGES.index(@current_stage)+1]
  @dealer.deal_community_cards(@current_stage)
end

#player_in_turnObject

TODO: Refactor this method to make it more readable



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_holdem/round.rb', line 70

def player_in_turn  #The player whose turn it is to make a move
  return players[0] if action_history.length == 0

  last_player_index = players.index(action_history.last[:player])
  player_found = false
  increment=1

  until player_found
    next_player = players[(last_player_index + increment) % players.length]   #Wrap around the array once end reached
    player_found = true if players_still_in_round.include?(next_player)
    increment += 1
  end

  next_player
end

#players_still_in_roundObject



86
87
88
89
90
91
# File 'lib/ruby_holdem/round.rb', line 86

def players_still_in_round
  players.select do |round_player|
    folds = action_history.select { |action| action[:move] == 'fold' && action[:player] == round_player }
    (folds.length == 0)
  end
end

#ready_for_next_stage?Boolean

Returns:



54
55
56
57
58
# File 'lib/ruby_holdem/round.rb', line 54

def ready_for_next_stage?
  return false unless every_player_has_called? && turns_played_in_stage > 0

  players_still_in_round.map { |player| (player.current_bet_amount == highest_bet_placed) }.all?
end

#winnerObject



64
65
66
67
# File 'lib/ruby_holdem/round.rb', line 64

def winner
  return players_still_in_round[0] if players_still_in_round.count == 1
  return players_still_in_round[2]
end