diff options
| author | Jérémy Zurcher <jeremy@asynk.ch> | 2026-03-11 14:07:37 +0100 |
|---|---|---|
| committer | Jérémy Zurcher <jeremy@asynk.ch> | 2026-03-11 14:07:37 +0100 |
| commit | 9a87cf512f52b7994253104425161892be7d67d0 (patch) | |
| tree | 526ebf09a53b282715be3f2c597feea8d0709b30 /lib | |
| parent | c046d01e85710377a220b43c5c802f70aabb61f7 (diff) | |
| download | colonial-twilight-9a87cf512f52b7994253104425161892be7d67d0.zip colonial-twilight-9a87cf512f52b7994253104425161892be7d67d0.tar.gz | |
update game
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/colonial_twilight/game.rb | 241 |
1 files changed, 119 insertions, 122 deletions
diff --git a/lib/colonial_twilight/game.rb b/lib/colonial_twilight/game.rb index 1a639fb..65b6b82 100644 --- a/lib/colonial_twilight/game.rb +++ b/lib/colonial_twilight/game.rb @@ -2,85 +2,69 @@ require 'json' -require 'colonial_twilight/board' -require 'colonial_twilight/cards' -require 'colonial_twilight/player' -require 'colonial_twilight/fln_bot' -require 'colonial_twilight/gov_player' +require_relative 'board' +require_relative 'deck' +require_relative 'fln_bot' +require_relative 'gov_player' module ColonialTwilight - class Game + module GameDefinitions + SCENARIOS = { + short: 'Short: 1960-1962: The End Game', + medium: 'Medium: 1957-1962: Midgame Development', + full: 'Full: 1955-1962: Algérie Francaise!' + }.freeze - MAX_CARD = 71 - - @scenarios = ['Short: 1960-1962: The End Game', - 'Medium: 1957-1962: Midgame Development', - 'Full: 1955-1962: Algérie Francaise!'].freeze - @rules = ['Standard Rules - No Support Phase in final Propaganda round', - 'Optional Rule 8.5.1 - Conduct Support Phase in final Propaganda round'].freeze - @actions = { - :event => 'Event: execute the Event card', - :ope_special => 'Operation & Special Activity: conduct an Operation in any number of spaces with a Special Activity', - :ope_only => 'Operation Only: conduct an Operation in any number of spaces without a Special Activity', - :ope_limited => 'Limited Operation: conduct an Operation in 1 space without a Special Activity', - :pass => 'Pass: increase your Resources' + RULES = { + std: 'Standard Rules - No Support Phase in final Propaganda round', + optional: 'Optional Rule 8.5.1 - Conduct Support Phase in final Propaganda round' }.freeze - @swap_actions= [:ope_special, :ope_limited] - class << self - attr_reader :scenarios, :rules, :actions - end - def rules; Game.rules end - def scenarios; Game.scenarios end - def possible_actions used - ks = Game.actions.keys - if not used.nil? - if used == :event - ks.delete :event - ks.delete :ope_only - ks.delete :ope_limited - elsif used == :ope_special - ks.delete :ope_special - ks.delete :ope_only - elsif used == :ope_limited - ks.delete :ope_limited - ks.delete :event - elsif used == :ope_only - ks.delete :ope_only - ks.delete :event - ks.delete :ope_special - end - end - Game.actions.select { |k,v| ks.include? k } - end - attr_reader :options, :scenario, :ruleset, :board, :ui, :cards, :actions - def initialize options + SWAP_ACTIONS = %i[op_special op_limited].freeze + + POSSIBLE_ACTIONS = { + event: %i[pass op_special], + op_special: %i[pass event op_limited], + op_limited: %i[pass op_special op_only], + op_only: %i[pass op_limited], + pass: %i[pass event op_special op_limited op_only] + }.freeze + end + + class Game + include GameDefinitions + + attr_reader :options, :board + + def initialize(options) @options = options + @turn = 0 + @card = nil @board = ColonialTwilight::Board.new @deck = ColonialTwilight::Deck.new + _set_ui + _set_players end - def start ui, s, rs - @ui = ui - @ruleset = rs - @scenario = s - @board.load [:short, :medium, :full][s] - @turn = 0 - @cards = { - :current => @deck.pull(0), - :prev => @deck.pull(0) - } - @actions = [] - @players = { - :fln => FLNBot.new(self, :FLN), - :gov => GOVPlayer.new(self, :GOV), - :first => :fln, - :second => :gov - } + def inspect + 'Game' end - def current_card - @cards[:current] + def d6 + @ui.d6 + end + + def launch + @ui.welcome + + s = @ui.chose_scenario SCENARIOS + @scenario = SCENARIOS.keys[s] + r = @ui.chose_rules RULES + @ruleset = RULES.keys[r] + + @board.load @scenario + # FIXME: do something with selected ruleset + _play end def first @@ -91,79 +75,92 @@ module ColonialTwilight @players[@players[:second]] end - def eligibility_swap - @players[:first], @players[:second] = @players[:second], @players[:first] + def current_card + @card end - def eligibility_swap? - Game.swap_actions.include? @actions[0] + def apply(faction, action) + _save_action(action) + @board.apply(faction, action) + @ui.show_action(action) + true end - def play - while true - @turn += 1 - ui.turn_start @turn, first, second - _pull_card - - act = _play first, nil - @ui.adjust_track @board.compute_victory_points - act = _play second, act - @ui.adjust_track @board.compute_victory_points + private - eligibility_swap if eligibility_swap? - @actions.clear + def _set_ui + if @options.ui != :cli + puts "Gui mode '#{@options.ui}' is not implemented" + exit(1) end + require_relative 'cli' + @ui = ColonialTwilight::Cli.new @options end - 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 + def _set_players + @players = { + FLN: FLNBot.new(self, :FLN), + GOV: GOVPlayer.new(self, :GOV), + first: :FLN, + second: :GOV + } end - 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 + def _play + loop do + @turn += 1 + @ui.turn_start @turn, first, second + _pull_card + first_action = _play_turn first + # FIXME : maybe it's not need to recompute and ask to display + # @ui.adjust_track @board.compute_victory_points + _play_turn second, first_action + # @ui.adjust_track @board.compute_victory_points + _swap_eligibility first_action + end end def _pull_card - @cards[:prev] = @cards[:current] - @cards[:current] = @deck.pull ui.pull_card(MAX_CARD) - ui.show_card @cards[:current] - end - - 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([]) } + @card = @deck.pull @ui.pull_card(ColonialTwilight::Card::MAX_CARD_NUM) + @ui.show_card @card end - end + def _swap_eligibility(first_action) + return unless SWAP_ACTIONS.include? first_action - class Sector - def to_json *args - name.to_json args + @players[:first], @players[:second] = @players[:second], @players[:first] end - end - class Box - def to_json *args - name.to_json args + def _play_turn(player, prev_act = nil) + _save prev_act.nil? + @ui.continue? player + @ui.show_player player, prev_act.nil? + player.play_turn prev_act, POSSIBLE_ACTIONS[prev_act || :pass] + end + + # TEST + def _save_action(action) + # puts 'FIXME : Board::_save_action' + # File.open("actions-#{@turn}.json", 'r+') do |f| + # data = JSON.parse f + # data << action + # f.seek 0 + # f << JSON.generate(data) + # end + end + + def _save(first) + # puts 'FIXME : Board::_save' + # h = { + # ruleset: @ruleset, + # scenario: @scenario, + # turn: @turn, + # card: @card, + # 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 end |
