diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2023-10-23 21:44:50 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2023-10-23 21:44:50 +0200 |
commit | 40f6759106bc429ff49cdaefdf8901ebb68688c1 (patch) | |
tree | 4243fabe7092a7c604bdb77716d3060ef8263322 /lib/colonial_twilight/fln_bot_rules.rb | |
parent | 8b34966151c15a4fb022a6205c8143b90f38f625 (diff) | |
download | colonial-twilight-40f6759106bc429ff49cdaefdf8901ebb68688c1.zip colonial-twilight-40f6759106bc429ff49cdaefdf8901ebb68688c1.tar.gz |
FLNBotRules : major rewrite + specs
Diffstat (limited to 'lib/colonial_twilight/fln_bot_rules.rb')
-rw-r--r-- | lib/colonial_twilight/fln_bot_rules.rb | 181 |
1 files changed, 119 insertions, 62 deletions
diff --git a/lib/colonial_twilight/fln_bot_rules.rb b/lib/colonial_twilight/fln_bot_rules.rb index e9b9e79..cbe2145 100644 --- a/lib/colonial_twilight/fln_bot_rules.rb +++ b/lib/colonial_twilight/fln_bot_rules.rb @@ -12,44 +12,40 @@ module ColonialTwilight end end - def pass? + def pass?(board = @board) # if resources = 0 && Op Limited as only choice - r = @board.fln_resources.zero? && limited_op_only? + r = board.fln_resources.zero? && limited_op_only? dbg 'PASS', r r end - def terror1? - # unless any FLN base is (pop 0 && 0 FLN underground or pop 1+ && 1- FLN underground) - r = !@board.has do |s| - s.fln_bases.positive? && - ((s.pop.zero? && s.fln_underground.zero?) || (!s.pop.zero? && s.fln_underground < 2)) + def terror1?(board = @board) + # if no FLN base is (pop 0 && 0 FLN underground or pop 1+ && 1- FLN underground) + r = !board.has do |s| + s.fln_bases.positive? && ((s.pop.zero? && s.fln_underground.zero?) || (!s.pop.zero? && s.fln_underground < 2)) end dbg 'TERROR 1', r r end - def terror2? + def terror2?(_board = nil) # if GOV is first eligible && will be second eligible r = !first_eligible? && will_be_next_first_eligible? dbg 'TERROR 2', r r end - def rally1? - # rally would place bases (first 2 bullets) - # 4+ or (3+ FLN and no GOV (unless limited_op_only)) - r = @board.available_fln_bases.positive? && @board.has do |s| - may_add_base_in?(s) && (rally_2_in?(s) || rally_1_in?(s)) - end + def rally1?(board = @board) + # rally would place a base (rally 1 or 2) + r = board.available_fln_bases.positive? && board.has { |s| (may_rally_1_in?(s) || may_rally_2_in?(s)) } dbg 'RALLY 1', r r end - def rally2? + def rally2?(board = @board) # if #FLN bases * 2 > #FLN at FLN bases + 1d6/2 - a = @board.count(&:fln_bases) * 2 - b = @board.count { |s| s.fln_bases.zero? ? 0 : s.fln_cubes } + a = board.count(&:fln_bases) * 2 + b = board.count { |s| s.fln_bases.zero? ? 0 : s.guerrillas } r = a > (b + d6 / 2) dbg 'RALLY 2', r r @@ -57,87 +53,148 @@ module ColonialTwilight # Rally - def may_add_base_in?(space) - r = space.fln_cubes > 2 && (space.fln_bases < (space.country? ? space.max_bases : 1)) - dbg " may_add_base : #{space.name}", r - r - end - - def rally_1_in?(space) + def may_rally_1_in?(space) # 3+ FLN and no GOV (unless limited_op_only)) - r = space.fln_cubes >= 3 && (limited_op_only? ? true : space.gov_cubes.zero?) - dbg " rally_1_in : #{space.name}", r + r = may_rally_in?(space) && may_add_base_in?(space) && space.guerrillas >= 3 && + (limited_op_only? ? true : space.gov_cubes.zero?) + dbg " may_rally_1_in : #{space.name}", r r end - def rally_2_in?(space) + def may_rally_2_in?(space) # 4+ FLN - r = space.fln_cubes >= 4 - dbg " rally_2_in : #{space.name}", r + r = may_rally_in?(space) && may_add_base_in?(space) && space.guerrillas >= 4 + dbg " may_rally_2_in : #{space.name}", r r end - def rally_3_in?(space) + def may_rally_3_in?(space) # at FLN bases, with 2- FLN underground or 0 fln_undergroud in country or 0 pop - r = !space.fln_bases.zero? && + r = may_rally_in?(space) && !space.fln_bases.zero? && (space.country? || space.pop.zero? ? space.fln_underground.zero? : space.fln_underground < 2) - dbg " rally_3_in : #{space.name}", r + dbg " may_rally_3_in : #{space.name}", r r end def rally_3_priority(spaces) # Algeria -> with cubes -> pop 1+ -> least FLN underground - l0 = (l0 = spaces.reject(&:country?)).empty? ? spaces : l0 - l1 = (l1 = l0.select { |s| s.gov_cubes.positive? }).empty? ? l0 : l1 - l2 = (l2 = l1.select { |s| s.pop.positive? }).empty? ? l1 : l2 - l2.min { |a, b| a.fln_underground <=> b.fln_underground } + f = _filter(spaces) { |s| !s.country? } + f = _filter(f) { |s| s.gov_cubes.positive? } + f = _filter(f) { |s| s.pop.positive? } + _min(f, :fln_underground).shuffle end - def rally_5_in?(space) + def may_rally_5_in?(space) # non-city at support with 0 FLN underground - r = !space.city? && space.support? && space.fln_underground.zero? - dbg " rally_5_in : #{space.name}", r + r = may_rally_in?(space) && !space.city? && space.support? && space.fln_underground.zero? + dbg " may_rally_5_in : #{space.name}", r r end - def rally_6_in?(space) - # 2+ pop to agitate - r = space.pop > 1 && may_agitate_in?(space) - dbg " rally_6_in : #{space.name}", r + def rally_5_priority(spaces) + # highest population + _max(spaces, :pop).shuffle + end + + def may_rally_6_in?(space, already_rallied) + # 2+ pop to agitate after rally + r = (already_rallied || may_rally_in?(space)) && space.pop > 1 + dbg " may_rally_6_in : #{space.name}", r r end - def rally_8_in?(space) - r = !space.fln_cubes.zero? && space.fln_bases.zero? - dbg " rally_8_in : #{space.name}", r + def rally_6_priority(spaces) + # max pop, min terror, support : reference ? + f = _max(spaces, :pop) + f = _min(f, :terror) + f = _filter(f, &:support?) + # FIXME: maybe already selected first, or not + f.shuffle + end + + def may_rally_7_in?(space) + r = may_rally_in?(space) + dbg " may_rally_7_in : #{space.name}", r r end + def rally_7_priority(spaces) + # highest population + _max(spaces, :pop).shuffle + end + + def rally_7_priority_after(spaces) + # in city, least terror + f = _filter(spaces, &:city?) + _min(f, :terror).shuffle + end + + def may_rally_8_in?(space) + r = may_rally_in?(space) && !space.guerrillas.zero? && space.fln_bases.zero? + dbg " may_rally_8_in : #{space.name}", r + r + end + + def rally_8_priority(spaces) + # Algeria -> most Guerrillas -> no gov cubes + f = _filter(spaces) { |s| !s.country? } + f = _max(f, :guerrillas) + _filter(f) { |s| s.gov_cubes.zero? }.shuffle + end + # 8.1.2 - Procedure Guidelines - def fln_to_place? - @board.available_fln_underground.positive? || !place_from(@board.spaces).nil? + + def _filter(spaces, &block) + (f = spaces.select(&block)).empty? ? spaces : f + end + + def _max(spaces, sym) + v = spaces.max { |a, b| a.send(sym) <=> b.send(sym) }.send(sym) + spaces.select { |s| s.send(sym) == v } + end + + def _min(spaces, sym) + v = spaces.min { |a, b| a.send(sym) <=> b.send(sym) }.send(sym) + spaces.select { |s| s.send(sym) == v } + end + + def may_add_base_in?(space) + space.guerrillas > 2 && (space.fln_bases < (space.country? ? space.max_bases : 1)) + end + + def max_placable_guerrillas_in?(space) + n = max_placable_guerrillas(space) + n = n.clamp(0, space.pop + 1 - space.guerrillas) if space.fln_bases.positive? + n end - # 1) place: outofplay -> available | bases -> cubes if choice - # 2) place: underground first unless from map then place active first flipped as underground - # 3) march: underground -> active, unless march would activate then move active first + # 1) place: outofplay -> available | bases -> guerrillas if choice + # 2) place: underground first unless from map then place active first flipped as underground + # 3) march: underground -> active, unless march would activate then move active first - def place_in(spaces) + def place_guerrillas(spaces) # 4) support -> with friendly pieces -> random l0 = (l0 = spaces.select(&:support?)).empty? ? spaces : l0 - l1 = (l1 = l0.select { |s| s.fln_cubes.positive? }).empty? ? l0 : l1 - (l1.empty? ? @board.spaces : l1).sample + l1 = (l1 = l0.select { |s| s.guerrillas.positive? }).empty? ? l0 : l1 + l1.shuffle end - def place_from(spaces) - # 5) active only, leave 2 cubes at base or support, most cubes first - l = spaces.select do |s| - s.fln_active.positive? && (s.support? || s.fln_bases.positive? ? s.fln_cubes > 2 : true) - end - return nil if l.empty? + def removable_guerrillas(space) + # 5) active only, leave 2 guerrillas at base or support + a = (a = space.fln_underground) > 2 ? 2 : a + n = space.fln_active - (space.support? || space.fln_bases.positive? ? (2 - a) : 0) + n.positive? ? n : 0 + end + + def not_selected(spaces, steps) + spaces.reject { |s| steps.key?(s) } + end + + def remove_guerrillas_priority(spaces, steps) + # 5) #removable_guerrillas then most guerrillas first + return [] if (l = not_selected(spaces, steps).select { |s| removable_guerrillas(s).positive? }).empty? - v = l.max { |a, b| a.fln_cubes <=> b.fln_cubes }.fln_cubes - l.select { |s| s.fln_cubes == v }.sample + _max(l, :guerrillas).shuffle end def remove_from(space, num = 1) |