Add auto play for bots
This commit is contained in:
parent
3d46e5b806
commit
b646eeb48b
@ -39,6 +39,10 @@ class GamesController < ApplicationController
|
||||
if params[:hand_type] == 'Pass'
|
||||
@game.set_controlling_player 'next'
|
||||
flash[:notice] = 'Successfully passed.'
|
||||
# Clear play to beat if play returns to owner
|
||||
if @game.play_to_beat.try(:player).try(:id) == @game.controlling_player_id
|
||||
@game.play_to_beat_id = nil
|
||||
end
|
||||
@game.save
|
||||
# Player action: play_hand
|
||||
elsif params[:player_card_ids]
|
||||
|
||||
@ -31,19 +31,13 @@ class TablesController < ApplicationController
|
||||
# GET /tables/1.json
|
||||
def show
|
||||
# TODO refactor to ensure play for 2nd and 3rd place
|
||||
|
||||
|
||||
if @game.controlling_player.try("is_bot?")
|
||||
flash[:notice] = "Bot is passes"
|
||||
@game.controlling_player.pass
|
||||
@game.controlling_player.auto_play
|
||||
@game.save
|
||||
@game.reload
|
||||
end
|
||||
|
||||
# Clear play to beat if play returns to owner
|
||||
if @game.play_to_beat.try(:player).try(:id) == @game.controlling_player_id
|
||||
@game.play_to_beat_id = nil
|
||||
@game.save
|
||||
end
|
||||
end
|
||||
|
||||
def new_game
|
||||
|
||||
@ -65,8 +65,8 @@ class Play < ActiveRecord::Base
|
||||
@truths << true
|
||||
return @truths
|
||||
else
|
||||
# hand_type match?
|
||||
@truths << (self.hand_type == game.play_to_beat.try("play").try("hand_type")) if game.play_to_beat.try("play").try(:hand_type)
|
||||
# hand_type match? allows partial match i.e. "triple run (suited)"
|
||||
@truths << (self.hand_type.match game.play_to_beat.try("play").try("hand_type")) if game.play_to_beat.try("play").try(:hand_type)
|
||||
# beats?
|
||||
@truths << (self.beats? self.game.play_to_beat.try(:play))
|
||||
end
|
||||
|
||||
@ -31,7 +31,65 @@ class Player < ActiveRecord::Base
|
||||
return false unless self.game.controlling_player == self
|
||||
|
||||
self.game.set_controlling_player 'next'
|
||||
# Clear play to beat if play returns to owner
|
||||
if self.game.play_to_beat.try(:player).try(:id) == self.game.controlling_player_id
|
||||
self.game.play_to_beat_id = nil
|
||||
end
|
||||
self.game.save
|
||||
end
|
||||
|
||||
def runs
|
||||
@runs = self.inventory.chunk_while {|i, j| i.rank.to_i+1 == j.rank.to_i }
|
||||
end
|
||||
|
||||
def triple_runs
|
||||
@triple_runs = self.runs.map{|r| r.length > 2 ? r : nil}.compact
|
||||
end
|
||||
|
||||
def auto_play
|
||||
# TODO not sure this return clause works
|
||||
return false unless self.game.controlling_player == self
|
||||
|
||||
# If first play of game, play lowest card
|
||||
if self.game.plays.empty?
|
||||
@cards_to_play = [self.inventory[0]]
|
||||
return self.play_hand @cards_to_play
|
||||
end
|
||||
|
||||
# If open table
|
||||
if !self.game.play_to_beat
|
||||
@cards_to_play = self.inventory[0..0]
|
||||
else
|
||||
@cards_to_beat = self.game.play_to_beat.play.player_cards
|
||||
case self.game.play_to_beat.play.hand_type
|
||||
when "single"
|
||||
@cards_to_play = self.inventory.where("value > #{@cards_to_beat.min.value}")[0..0]
|
||||
when "double"
|
||||
@doubles = self.inventory.group_by {|card| card.rank}.map{|c| c if c[1].length > 1}.compact
|
||||
if @doubles.present?
|
||||
if @doubles[0][1][0..1].max.value > @cards_to_beat.max.value
|
||||
@cards_to_play = @doubles[0][1][0..1]
|
||||
end
|
||||
end
|
||||
when "triple run"
|
||||
@runs = self.inventory.chunk_while {|i, j| i.rank.to_i+1 == j.rank.to_i }
|
||||
@triple_runs = @runs.map{|r| r.length > 2 ? r : nil}.compact
|
||||
if @triple_runs.present?
|
||||
@cards_to_play = @triple_runs[0][0..2]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if @cards_to_play.present?
|
||||
if @cards_to_beat.present?
|
||||
if (@cards_to_play.max_by{|c| c.value}.value > @cards_to_beat.max_by{|c| c.value}.value)
|
||||
return self.play_hand @cards_to_play
|
||||
end
|
||||
else
|
||||
return self.play_hand @cards_to_play
|
||||
end
|
||||
end
|
||||
self.pass
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@ -49,8 +49,7 @@
|
||||
= image_tag('cards/png/' + (card.rank.gsub('J','jack').gsub('Q','queen').gsub('K','king').gsub('A','ace') + '_of_' + card.suit.downcase + 's.png').gsub('11','jack') .gsub('12','queen') .gsub('13','king') .gsub('14','ace') .gsub('15','2'))
|
||||
- disabled = @game.controlling_player != seat.player
|
||||
%input{type: "submit", value: "Play Hand", name: "hand_type", disabled: disabled}
|
||||
%input{type: "submit", value: "Pass", name: "hand_type", disabled: disabled,
|
||||
:data => { :confirm => 'Are you sure you want to pass?' }}
|
||||
%input{type: "submit", value: "Pass", name: "hand_type", disabled: disabled}
|
||||
|
||||
- else
|
||||
.inventory
|
||||
|
||||
@ -12,8 +12,8 @@
|
||||
- if @game.startable?
|
||||
= button_to 'Start', start_game_path(@game)
|
||||
|
||||
- if @game.winner_player_id || @game.nil?
|
||||
= button_to 'New Game', new_game_table_path(@table)
|
||||
-# - if @game.winner_player_id || @game.nil?
|
||||
-# = button_to 'New Game', new_game_table_path(@table)
|
||||
|
||||
-# - if @game && !@game.startable?
|
||||
= button_to 'New Game', new_game_table_path(@table)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user