diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2020-09-04 17:42:47 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2020-09-04 17:42:47 +0200 |
commit | 7ea6b836d1f7a8ac7a1a53b6beacf214cdfbddf0 (patch) | |
tree | 2e1f00d925ea6d1b3548246fe44f315ba066366e /lib | |
parent | a503d93dbc45062abaa27f8f5d394001dc2e25e2 (diff) | |
download | colonial-twilight-7ea6b836d1f7a8ac7a1a53b6beacf214cdfbddf0.zip colonial-twilight-7ea6b836d1f7a8ac7a1a53b6beacf214cdfbddf0.tar.gz |
Game : improve save, clean up
Diffstat (limited to 'lib')
-rw-r--r-- | lib/colonial_twilight/cards.rb | 3 | ||||
-rw-r--r-- | lib/colonial_twilight/cli.rb | 2 | ||||
-rw-r--r-- | lib/colonial_twilight/game.rb | 110 |
3 files changed, 74 insertions, 41 deletions
diff --git a/lib/colonial_twilight/cards.rb b/lib/colonial_twilight/cards.rb index 0f98dc4..4ffcff5 100644 --- a/lib/colonial_twilight/cards.rb +++ b/lib/colonial_twilight/cards.rb @@ -49,10 +49,11 @@ module ColonialTwilight attr_reader :cards def initialize @cards = {} + add_card 0, 'None', nil, nil add_card 1, 'Quadrillage', 0, CardAction.new('Place up to all French Police in Available in up to 3 spaces', {:what=>:french_police,:from=>:available}) end - def pull n; @cards[n] end + def pull n; @cards[n > 0 ? 1 : 0] end # FIXME private diff --git a/lib/colonial_twilight/cli.rb b/lib/colonial_twilight/cli.rb index 97700ea..8618508 100644 --- a/lib/colonial_twilight/cli.rb +++ b/lib/colonial_twilight/cli.rb @@ -73,7 +73,7 @@ module ColonialTwilight ret << chose('Choose a ruleset', @game.rules) { |s| a = s.split('-'); a[0] = a[0].yellow; a.join('-') } exit(0) if ret[-1] < 0 @game.start self, *ret - # @game.start self, 0, 0 + @game.play end def logo diff --git a/lib/colonial_twilight/game.rb b/lib/colonial_twilight/game.rb index 75c4534..3b1dd99 100644 --- a/lib/colonial_twilight/game.rb +++ b/lib/colonial_twilight/game.rb @@ -13,6 +13,8 @@ module ColonialTwilight class Game + MAX_CARD = 71 + @scenarios = ['Short: 1960-1962: The End Game', 'Medium: 1957-1962: Midgame Development', 'Full: 1955-1962: Algérie Francaise!'].freeze @@ -27,11 +29,11 @@ module ColonialTwilight }.freeze @swap_actions= [:ope_special, :ope_limited] class << self - attr_reader :scenarios, :rules, :actions, :cards + attr_reader :scenarios, :rules, :actions end def rules; Game.rules end def scenarios; Game.scenarios end - def possible_actions used=nil + def possible_actions used ks = Game.actions.keys if not used.nil? if used == :event @@ -65,62 +67,92 @@ module ColonialTwilight @ruleset = rs @scenario = s @board.load [:short, :medium, :long][s] - @max_card = 71 @turn = 0 - @cards = [] + @cards = { + :current => @deck.pull(0), + :prev => @deck.pull(0) + } @actions = [] - @players = [FLNBot.new(self, :FLN), GOVPlayer.new(self, :GOV)] - play + @players = { + :fln => FLNBot.new(self, :FLN), + :gov => GOVPlayer.new(self, :GOV), + :first => :fln, + :second => :gov + } + end + + def current_card + @cards[:current] + end + + def first + @players[@players[:first]] + end + + def second + @players[@players[:second]] + end + + def eligibility_swap + @players[:first], @players[:second] = @players[:second], @players[:first] end - def current_card; @cards[-1] end + def eligibility_swap? + Game.swap_actions.include? @actions[0] + end def play - save_board 0 while true @turn += 1 - ui.turn_start @turn, *@players - c = ui.pull_card @max_card - c = 1 # FIXME - @cards << @deck.pull(c) - ui.show_card @cards[-1] - File.open("actions-#{@turn}.json",'w') { |f| f << JSON.generate([]) } - - ui.continue? @players[0].instance_of? FLNBot - ui.player @players[0], true - @actions[0] = @players[0].play possible_actions - @ui.adjust_track @board.compute_victory_points - save_board 0 + ui.turn_start @turn, first, second + _pull_card - ui.continue? @players[1].instance_of? FLNBot - ui.player @players[1], false - @actions[1] = @players[1].play possible_actions @actions[0] - @ui.adjust_track @board.compute_victory_points - save_board 1 + act = _play first, nil + @ui.adjust_track @board.compute_victory_points + act = _play second, act + @ui.adjust_track @board.compute_victory_points - @cards.shift if @cards.length > 2 - @players[0], @players[1] = @players[1], @players[0] if eligibility_swap? + eligibility_swap if eligibility_swap? @actions.clear end end - def eligibility_swap? - Game.swap_actions.include? @actions[0] + def action_done player, action + File.open("actions-#{@turn}.json",'r+') do |f| + data = JSON.load f + data << action + f.seek 0 + f << JSON.generate(data) + end + @ui.show_player_action player, action end - def save_board phase - json = JSON.generate @board.data - File.open("turn-#{@turn}-#{phase}.json",'w') { |f| f << json } + private + + def _play player, prev_act + _save prev_act.nil? + ui.continue? player.instance_of? FLNBot + ui.player player, prev_act.nil? + player.play possible_actions prev_act end - def action_done player, action - data = nil - File.open("actions-#{@turn}.json",'r') { |f| data = JSON.load f } - data << action + def _pull_card + @cards[:prev] = @cards[:current] + @cards[:current] = @deck.pull ui.pull_card(MAX_CARD) + ui.show_card @cards[:current] + end - json = JSON.generate data - File.open("actions-#{@turn}.json",'w') { |f| f << json } - @ui.show_player_action player, action + def _save first + h = { + :ruleset => @ruleset, + :scenario => @scenario, + :turn => @turn, + :cards => {:current => @cards[:current].num, :prev => @cards[:prev].num}, + :players => {:first => @players[:first], :second => @players[:second]}, + :board => @board.data + } + File.open("turn-#{@turn}-#{first ? 0 : 1}.json",'w') { |f| f << JSON.generate(h) } + File.open("actions-#{@turn}.json",'w') { |f| f << JSON.generate([]) } end end |