def add_points(self, points, can_acquire_angels): logging.info("Legion.add_points %s %s %s", self, points, can_acquire_angels) ARCHANGEL_POINTS = Creature.Creature("Archangel").acquirable_every ANGEL_POINTS = Creature.Creature("Angel").acquirable_every player = self.player score0 = player.score score1 = score0 + points player.score = score1 logging.info("%s now has score %s", player, player.score) if can_acquire_angels: height = len(self) archangels = 0 if (height < 7 and score1 // ARCHANGEL_POINTS > score0 // ARCHANGEL_POINTS): archangels += 1 score1 -= ANGEL_POINTS logging.info("archangels %d" % archangels) angels = 0 while (height + archangels + angels < 7 and score1 // ANGEL_POINTS > score0 // ANGEL_POINTS): angels += 1 score1 -= ANGEL_POINTS logging.info("angels %d" % angels) self._angels_pending = angels self._archangels_pending = archangels if angels + archangels > 0: action = Action.CanAcquireAngels(self.player.game.name, self.player.name, self.markerid, angels, archangels) self.notify(action)
def cmp_helper(tup1, tup2): ii = 0 while True: if len(tup1) < ii + 1: return -1 if len(tup2) < ii + 1: return 1 if tup1[ii] != tup2[ii]: c1 = Creature.Creature(tup1[ii]) c2 = Creature.Creature(tup2[ii]) diff = 100 * (c1.sort_value - c2.sort_value) if diff != 0: return int(diff) ii += 1
def reveal_creatures(self, creature_names): """Reveal the creatures from creature_names, if they're not already known to be in this legion.""" if self.any_unknown: bag1 = bag(self.creature_names) bag2 = bag(creature_names) for creature_name, count2 in bag2.iteritems(): count1 = bag1[creature_name] while count2 > count1 and self.any_unknown: self.creatures.remove(Creature.Creature("Unknown")) creature = Creature.Creature(creature_name) self.creatures.append(creature) creature.legion = self count2 -= 1
def test_non_existent_creature(): try: Creature.Creature("Jackalope") except KeyError: pass else: assert False, "Should have raised"
def kill_one(self, creature_name): """If creature_name is mortal, put it in the graveyard. Otherwise put it back onto the stack.""" creature = Creature.Creature(creature_name) if creature.is_creature: self.graveyard[creature_name] += 1 else: self.put_one_back(creature_name)
def _max_creatures_of_one_type(self): """Return the maximum number of creatures (not lords or demi-lords) of the same type in this legion.""" counts = bag(self.creature_names) maximum = 0 for name, num in counts.iteritems(): if (num > maximum and Creature.Creature(name).is_creature): maximum = num return maximum
def __init__(self): self.counts = {} self.max_counts = {} self.graveyard = {} for creature_name in creaturedata.data: creature = Creature.Creature(creature_name) if not creature.is_unknown: self.counts[creature.name] = creature.max_count self.max_counts[creature.name] = creature.max_count self.graveyard[creature.name] = 0
def put_one_back(self, creature_name): """Put one of creature_name back onto the stack.""" creature = Creature.Creature(creature_name) if creature.is_unknown: return if self.counts[creature_name] >= creature.max_count: logging.info("Tried to put too many %s back" % creature_name) self.counts[creature_name] = self.max_counts[creature_name] else: self.counts[creature_name] += 1
def test_native(): ogre = Creature.Creature("Ogre") assert ogre.is_native("Bog") assert ogre.is_native("Slope") assert ogre.is_native("Plain") assert ogre.is_native("Tree") assert ogre.is_native("Tower") assert not ogre.is_native("Bramble") assert not ogre.is_native("Volcano") assert not ogre.is_native("Sand") assert not ogre.is_native("Dune") assert not ogre.is_native("Cliff") cyclops = Creature.Creature("Cyclops") assert cyclops.is_native("Bramble") assert not cyclops.is_native("Bog") warlock = Creature.Creature("Warlock") assert warlock.is_native("Wall") assert not warlock.is_native("Bog") titan = Creature.Creature("Titan") assert not titan.is_native("Wall") assert not titan.is_native("Tower") assert not titan.is_native("Bog") assert not titan.is_native("Bramble") lion = Creature.Creature("Lion") assert not lion.is_native("Bog") assert lion.is_native("Slope") assert lion.is_native("Plain") assert not lion.is_native("Tree") assert not lion.is_native("Tower") assert not lion.is_native("Bramble") assert lion.is_native("Sand") assert lion.is_native("Dune") assert lion.is_native("Cliff") assert not lion.is_native("Volcano") dragon = Creature.Creature("Dragon") assert not dragon.is_native("Bog") assert dragon.is_native("Slope") assert dragon.is_native("Plain") assert not dragon.is_native("Tree") assert not dragon.is_native("Tower") assert not dragon.is_native("Bramble") assert not dragon.is_native("Sand") assert not dragon.is_native("Dune") assert dragon.is_native("Cliff") assert dragon.is_native("Volcano") unicorn = Creature.Creature("Unicorn") assert unicorn.is_native("Slope")
def find_angel_combos(num_archangels, num_angels, archangels_left, angels_left): """Return a list of tuples of Creatures, corresponding to each possible group of angels and archangels.""" set1 = set() for archangels in xrange(min(num_archangels, archangels_left) + 1): for angels in xrange(min((num_angels + num_archangels), angels_left) + 1): if 0 < archangels + angels <= num_archangels + num_angels: lst = [] for unused in xrange(archangels): lst.append(Creature.Creature("Archangel")) for unused in xrange(angels): lst.append(Creature.Creature("Angel")) combo = tuple(lst) set1.add(combo) sorter = [] for combo in set1: score = sum((creature.score for creature in combo)) sorter.append((score, combo)) sorter.sort(reverse=True) combos = [combo for (unused, combo) in sorter] return combos
def show_recruit_tree(self, terrain, playercolor): """Show the recruit tree for terrain.""" self.legion_name.set_text(terrain) for hbox in [self.marker_hbox, self.chits_hbox]: for child in hbox.get_children(): hbox.remove(child) tuples = recruitdata.data[terrain] for tup in tuples: if len(tup) == 2 and tup[1] != 0: creature_name, count = tup if count >= 2: label = gtk.Label(str(count)) self.chits_hbox.pack_start(label, expand=False) creature = Creature.Creature(creature_name) chit = Chit.Chit(creature, playercolor, scale=15) self.chits_hbox.pack_start(chit.event_box, expand=False) self.show_all()
def test_init(): creature = Creature.Creature("Ogre") assert creature.name == "Ogre" assert creature.plural_name == "Ogres" assert creature.power == 6 assert creature.skill == 2 assert not creature.flies assert not creature.rangestrikes assert not creature.magicmissile assert creature.character_type == "creature" assert creature.is_creature assert not creature.is_lord assert not creature.is_demilord assert not creature.is_unknown assert not creature.summonable assert not creature.acquirable assert not creature.is_titan assert creature.max_count == 25 assert creature.color_name == "ogre_red"
def __init__(self, game, playername): self.playername = playername self.caretaker = game.caretaker gtk.EventBox.__init__(self) self.vbox = gtk.VBox() self.add(self.vbox) table = gtk.Table(rows=4, columns=6) table.set_row_spacings(9) table.set_col_spacings(9) self.vbox.pack_start(table, expand=False) self.max_count_labels = {} self.counts_labels = {} self.chits = {} for ii, (creature_name, left_count) in enumerate(sorted( self.caretaker.counts.iteritems())): creature = Creature.Creature(creature_name) max_count = self.caretaker.max_counts[creature_name] dead_count = self.caretaker.graveyard[creature_name] game_count = max_count - left_count - dead_count vbox = gtk.VBox() col = (ii % 6) + 1 row = (ii // 6) + 1 table.attach(vbox, col, col + 1, row, row + 1) label = self.max_count_labels[creature_name] = gtk.Label() vbox.pack_start(label, expand=False) chit = self.chits[creature_name] = Chit.Chit(creature, "Black", scale=15, dead=(not left_count)) vbox.pack_start(chit.event_box, expand=False) label = self.counts_labels[creature_name] = gtk.Label() vbox.pack_start(label, expand=False) self.update_max_count_label(creature_name, max_count) self.update_counts_label(creature_name, left_count, game_count, dead_count) self.show_all()
def create_starting_legion(self): markerid = self.selected_markerid if markerid is None: raise AssertionError("create_starting_legion without marker") if markerid not in self.markerids_left: raise AssertionError("create_starting_legion with bad marker") if self.markerid_to_legion: raise AssertionError("create_starting_legion but have a legion") creatures = [ Creature.Creature(name) for name in creaturedata.starting_creature_names ] legion = Legion.Legion(self, self.take_marker(markerid), creatures, self.starting_tower) self.markerid_to_legion[markerid] = legion legion.add_observer(self.game) action = Action.CreateStartingLegion(self.game.name, self.name, markerid) caretaker = self.game.caretaker for creature in creatures: caretaker.take_one(creature.name) self.created_starting_legion = True self.notify(action)
layout.set_text(str(self.creature.hits)) size = surface.get_width() layout.set_width(size) pctx.set_source_rgb(1, 0, 0) x = 0.5 * size y = 0.2 * size pctx.set_source_rgb(1, 1, 1) pctx.set_line_width(1) width, height = layout.get_pixel_size() pctx.rectangle(x - 0.5 * width, y, 0.9 * width, 0.8 * height) pctx.fill() pctx.set_source_rgb(1, 0, 0) pctx.move_to(x, y) pctx.show_layout(layout) if __name__ == "__main__": from slugathon.game import Creature creature = Creature.Creature("Ogre") creature.hits = 3 chit = Chit(creature, "Red", scale=45, rotate=90) window = gtk.Window() window.connect("destroy", gtk.main_quit) window.add(chit.event_box) window.show() chit.show() gtk.main()
def cb_cancel(self, widget, response_id): self.deferred.callback((self.legion, None, None)) self.destroy() if __name__ == "__main__": import time from slugathon.game import Creature, Legion, Player, Game from slugathon.util import guiutils now = time.time() playername = "test" game = Game.Game("g1", playername, now, now, 2, 6) player = Player.Player(playername, game, 0) player.color = "Red" creatures1 = [Creature.Creature(name) for name in ["Titan", "Ogre", "Troll", "Ranger"]] creatures2 = [Creature.Creature(name) for name in ["Angel", "Ogre", "Troll", "Ranger"]] creatures3 = [Creature.Creature(name) for name in ["Archangel", "Centaur", "Lion", "Ranger"]] creatures4 = [Creature.Creature(name) for name in ["Gargoyle", "Cyclops", "Gorgon", "Behemoth"]] creatures5 = [Creature.Creature(name) for name in ["Angel", "Angel", "Warlock", "Guardian"]] legion1 = Legion.Legion(player, "Rd01", creatures1, 1) legion2 = Legion.Legion(player, "Rd02", creatures2, 2) legion3 = Legion.Legion(player, "Rd03", creatures3, 3) legion4 = Legion.Legion(player, "Rd04", creatures4, 4) legion5 = Legion.Legion(player, "Rd05", creatures5, 4) for legion in [legion1, legion2, legion3, legion4, legion5]:
def cb_response(self, widget, response_id): self.destroy() if response_id == gtk.RESPONSE_OK: self.deferred.callback((self.old_legion, self.new_legion1, self.new_legion2)) else: self.deferred.callback((None, None, None)) if __name__ == "__main__": import time from slugathon.data import creaturedata from slugathon.game import Creature, Player, Game from slugathon.util import guiutils now = time.time() creatures = [Creature.Creature(name) for name in creaturedata.starting_creature_names] playername = "test" game = Game.Game("g1", playername, now, now, 2, 6) player = Player.Player(playername, game, 0) player.color = "Red" legion = Legion.Legion(player, "Rd01", creatures, 1) player.selected_markerid = "Rd02" splitlegion, def1 = new(playername, legion, None) def1.addCallback(guiutils.exit) splitlegion.connect("destroy", guiutils.exit) gtk.main()
def test_score_move_scary_pursuer(): now = time.time() game = Game.Game("g1", "p0", now, now, 2, 6) game.add_player("p1") player0 = game.players[0] player1 = game.players[1] cleverbot = CleverBot.CleverBot("p1", 5) player0.assign_starting_tower(200) player1.assign_starting_tower(100) game.sort_players() game.started = True game.assign_color("p1", "Blue") game.assign_color("p0", "Red") game.assign_first_marker("p0", "Rd01") game.assign_first_marker("p1", "Bu01") player0.pick_marker("Rd02") player0.split_legion("Rd01", "Rd02", ["Titan", "Centaur", "Ogre", "Gargoyle"], ["Angel", "Centaur", "Ogre", "Gargoyle"]) player0.done_with_splits() rd01 = player0.markerid_to_legion["Rd01"] player1.pick_marker("Bu02") player1.split_legion("Bu01", "Bu02", ["Titan", "Centaur", "Ogre", "Gargoyle"], ["Angel", "Centaur", "Ogre", "Gargoyle"]) bu01 = player1.markerid_to_legion["Bu01"] bu02 = player1.markerid_to_legion["Bu02"] player0.done_with_splits() bu01.creatures.append(Creature.Creature("Ogre")) rd01.creatures.append(Creature.Creature("Colossus")) rd01.creatures.append(Creature.Creature("Colossus")) rd01.creatures.append(Creature.Creature("Colossus")) bu01.hexlabel = 41 bu02.hexlabel = 400 rd01.hexlabel = 100 # We ignore fear on turn 1, so set the turn to 2 to test. game.turn = 2 # staying in 41 gives us range 1 # moving to 42 gives us range 2 # moving to 1 gives us range 3 # moving to 2 gives us range 4 # moving to 3 gives us range 1 # moving to 4 gives us range 2 # moving to 5 gives us range 3 hexlabel_to_score = {} for hexlabel in [41, 42, 1, 2, 3, 4, 5]: hexlabel_to_score[hexlabel] = cleverbot._score_move( bu01, hexlabel, hexlabel != 41) print hexlabel_to_score assert hexlabel_to_score[42] > hexlabel_to_score[41] assert hexlabel_to_score[42] > hexlabel_to_score[3] assert hexlabel_to_score[1] > hexlabel_to_score[42] assert hexlabel_to_score[1] > hexlabel_to_score[4] assert hexlabel_to_score[2] > hexlabel_to_score[1] assert hexlabel_to_score[2] > hexlabel_to_score[5] assert hexlabel_to_score[4] > hexlabel_to_score[41] assert hexlabel_to_score[4] > hexlabel_to_score[3] assert hexlabel_to_score[5] > hexlabel_to_score[42] assert hexlabel_to_score[5] > hexlabel_to_score[4]
from slugathon.data import creaturedata, playercolordata from slugathon.game import Legion, Player from slugathon.util import guiutils def cb_destroy(confirmed): print "destroy" graveyard.destroy() gtk.main_quit() def cb_click(self, widget, event): if self.legion.living_creatures: random.choice(self.legion.living_creatures).kill() graveyard.update_gui() creatures = [ Creature.Creature(name) for name in creaturedata.starting_creature_names ] playername = "test" player = Player.Player(playername, None, None) player.color = random.choice(playercolordata.colors) abbrev = player.color_abbrev index = random.randrange(1, 12 + 1) legion = Legion.Legion(player, "%s%02d" % (abbrev, index), creatures, 1) graveyard = Graveyard(legion) graveyard.connect("destroy", guiutils.exit) graveyard.connect("button-press-event", cb_click, graveyard) window = gtk.Window() window.add(graveyard)
def test_score(): creature = Creature.Creature("Ogre") assert creature.score == 12 creature = Creature.Creature("Colossus") assert creature.score == 40
def add_creature_by_name(self, creature_name): if len(self) >= 7: raise ValueError("no room to add another creature") creature = Creature.Creature(creature_name) creature.legion = self self.creatures.append(creature)
def available_recruits_and_recruiters(self, mterrain, caretaker): """Return a list of tuples with creature names and recruiters that this legion could recruit in a masterhex with terrain type mterrain, if it moved there. Each tuple will contain the recruit as its first element, and the recruiters (if any) as its remaining elements. The list is sorted in the same order as within recruitdata. """ result_list = [] counts = bag(self.living_creature_names) recruits = recruitdata.data[mterrain] for sublist in self._gen_sublists(recruits): names = [tup[0] for tup in sublist] nums = [tup[1] for tup in sublist] for ii in xrange(len(sublist)): name = names[ii] num = nums[ii] if ii >= 1: prev = names[ii - 1] else: prev = None if prev == recruitdata.ANYTHING: # basic tower creature for jj in xrange(ii + 1): if nums[jj] and caretaker.counts.get(names[jj]): result_list.append((names[jj], )) else: if (prev == recruitdata.CREATURE and self._max_creatures_of_one_type() >= num): # guardian recruiters = [] for name2, num2 in counts.iteritems(): if (num2 >= num and Creature.Creature(name2).is_creature): recruiters.append(name2) for jj in xrange(ii + 1): if nums[jj] and caretaker.counts.get(names[jj]): for recruiter in recruiters: li = [names[jj]] for kk in xrange(num): li.append(recruiter) tup = tuple(li) result_list.append(tup) if counts[prev] >= num: # recruit up if num and caretaker.counts.get(name): li = [name] for kk in xrange(num): li.append(prev) tup = tuple(li) result_list.append(tup) if counts[name] and num: # recruit same or down for jj in xrange(ii + 1): if nums[jj] and caretaker.counts.get(names[jj]): result_list.append((names[jj], name)) def cmp_helper(tup1, tup2): ii = 0 while True: if len(tup1) < ii + 1: return -1 if len(tup2) < ii + 1: return 1 if tup1[ii] != tup2[ii]: c1 = Creature.Creature(tup1[ii]) c2 = Creature.Creature(tup2[ii]) diff = 100 * (c1.sort_value - c2.sort_value) if diff != 0: return int(diff) ii += 1 result_list.sort(cmp=cmp_helper) return result_list
def test_score_legion_move_swamp(): now = time.time() game = Game.Game("g1", "p0", now, now, 2, 6) game.add_player("p1") player0 = game.players[0] player1 = game.players[1] player0.assign_starting_tower(200) player1.assign_starting_tower(100) game.sort_players() game.started = True game.assign_color("p1", "Blue") game.assign_color("p0", "Red") game.assign_first_marker("p0", "Rd01") game.assign_first_marker("p1", "Bu01") player0.pick_marker("Rd02") player0.split_legion("Rd01", "Rd02", ["Titan", "Ogre", "Ogre", "Gargoyle"], ["Angel", "Centaur", "Centaur", "Gargoyle"]) rd01 = player0.markerid_to_legion["Rd01"] player1.pick_marker("Bu02") player1.split_legion("Bu01", "Bu02", ["Titan", "Centaur", "Gargoyle", "Gargoyle"], ["Angel", "Centaur", "Ogre", "Ogre"]) bu01 = player1.markerid_to_legion["Bu01"] rd01.creatures.append(Creature.Creature("Troll")) rd01.creatures.append(Creature.Creature("Troll")) rd01.creatures.append(Creature.Creature("Troll")) bu01.creatures.remove(Creature.Creature("Centaur")) bu01.creatures.append(Creature.Creature("Warlock")) bu01.creatures.append(Creature.Creature("Warlock")) bu01.creatures.append(Creature.Creature("Cyclops")) bu01.creatures.append(Creature.Creature("Cyclops")) rd01.move(132, False, None, 1) bu01.move(132, False, None, 1) game._init_battle(bu01, rd01) defender = game.defender_legion attacker = game.attacker_legion d_titan = defender.creatures[0] d_ogre1 = defender.creatures[1] d_ogre2 = defender.creatures[2] d_gargoyle = defender.creatures[3] d_troll1 = defender.creatures[4] d_troll2 = defender.creatures[5] d_troll3 = defender.creatures[6] for creature in defender.creatures: creature.legion = defender for creature in attacker.creatures: creature.legion = attacker cleverbot_d = CleverBot.CleverBot("p0", 1) hexlabel = d_ogre1.hexlabel move_to_score = {} moves = game.find_battle_moves(d_ogre1) for move in moves: d_ogre1.move(move) score = cleverbot_d._score_legion_move(game, [d_ogre1]) move_to_score[move] = score d_ogre1.move(hexlabel) # Should prefer back rank to second rank for hexlabel1 in ["A1", "A1", "A3"]: for hexlabel2 in ["B1", "B2", "B3", "B4"]: assert move_to_score[hexlabel1] > move_to_score[hexlabel2] hexlabel = d_troll1.hexlabel move_to_score = {} moves = game.find_battle_moves(d_troll1) for move in moves: d_troll1.move(move) score = cleverbot_d._score_legion_move(game, [d_troll1]) move_to_score[move] = score d_troll1.move(hexlabel) # Should prefer back rank to second rank for hexlabel1 in ["A1", "A1", "A3"]: for hexlabel2 in ["B1", "B2", "B3", "B4"]: assert move_to_score[hexlabel1] > move_to_score[hexlabel2] hexlabel = d_gargoyle.hexlabel move_to_score = {} moves = game.find_battle_moves(d_gargoyle) for move in moves: d_gargoyle.move(move) score = cleverbot_d._score_legion_move(game, [d_gargoyle]) move_to_score[move] = score d_gargoyle.move(hexlabel) # Should prefer back rank to second rank to third rank for hexlabel1 in ["A1", "A1", "A3"]: for hexlabel2 in ["B1", "B3", "B4"]: for hexlabel3 in ["C1", "C3"]: assert (move_to_score[hexlabel1] > move_to_score[hexlabel2] > move_to_score[hexlabel3]) hexlabel = d_titan.hexlabel move_to_score = {} moves = game.find_battle_moves(d_titan) for move in moves: d_titan.move(move) score = cleverbot_d._score_legion_move(game, [d_titan]) move_to_score[move] = score d_titan.move(hexlabel) # Should prefer back rank to second rank to third rank to fourth rank for hexlabel1 in ["A1", "A1", "A3"]: for hexlabel2 in ["B1", "B3", "B4"]: for hexlabel3 in ["C1", "C3"]: for hexlabel4 in ["D2", "D4"]: assert (move_to_score[hexlabel1] > move_to_score[hexlabel2] > move_to_score[hexlabel3] > move_to_score[hexlabel4]) d_troll1.move("B2") d_troll2.move("B4") d_ogre1.move("B1") d_gargoyle.move("B3") d_titan.move("A3") d_troll3.move("A2") d_ogre2.move("A1") cleverbot_a = CleverBot.CleverBot("p1", 1) game.battle_active_legion = attacker game.battle_phase = Phase.STRIKE a_titan = attacker.creatures[0] a_gargoyle1 = attacker.creatures[1] a_gargoyle2 = attacker.creatures[2] a_warlock1 = attacker.creatures[3] a_warlock2 = attacker.creatures[4] a_cyclops1 = attacker.creatures[5] a_cyclops2 = attacker.creatures[6] hexlabel = a_titan.hexlabel move_to_score = {} moves = game.find_battle_moves(a_titan) for move in moves: a_titan.move(move) score = cleverbot_a._score_legion_move(game, [a_titan]) move_to_score[move] = score a_titan.move(hexlabel) # Should prefer back rank to second rank to third rank to fourth rank for hexlabel1 in ["F1", "F3", "F4"]: for hexlabel2 in ["E1", "E2", "E3", "E5"]: for hexlabel3 in ["D2", "D4", "D5", "D6"]: for hexlabel4 in ["C1", "C3"]: assert (move_to_score[hexlabel1] > move_to_score[hexlabel2] > move_to_score[hexlabel3] > move_to_score[hexlabel4]) hexlabel = a_gargoyle1.hexlabel move_to_score = {} moves = game.find_battle_moves(a_gargoyle1) for move in moves: a_gargoyle1.move(move) score = cleverbot_a._score_legion_move(game, [a_gargoyle1]) move_to_score[move] = score a_gargoyle1.move(hexlabel) # Should prefer third rank to second rank to back rank for hexlabel1 in ["F1", "F3", "F4"]: for hexlabel2 in ["E1", "E2", "E3", "E5"]: for hexlabel3 in ["D2", "D4", "D5", "D6"]: assert (move_to_score[hexlabel3] > move_to_score[hexlabel2] > move_to_score[hexlabel1]) hexlabel = a_cyclops1.hexlabel move_to_score = {} moves = game.find_battle_moves(a_cyclops1) for move in moves: a_cyclops1.move(move) score = cleverbot_a._score_legion_move(game, [a_cyclops1]) move_to_score[move] = score a_cyclops1.move(hexlabel) # Should prefer third rank to second rank to back rank for hexlabel1 in ["F1", "F3", "F4"]: for hexlabel2 in ["E1", "E2", "E3", "E5"]: assert move_to_score[hexlabel2] > move_to_score[hexlabel1] hexlabel = a_warlock1.hexlabel move_to_score = {} moves = game.find_battle_moves(a_warlock1) for move in moves: a_warlock1.move(move) score = cleverbot_a._score_legion_move(game, [a_warlock1]) move_to_score[move] = score a_warlock1.move(hexlabel) # Should prefer third rank to second rank to back rank for hexlabel1 in ["F1", "F3", "F4"]: for hexlabel2 in ["E1", "E2", "E3", "E5"]: assert move_to_score[hexlabel2] > move_to_score[hexlabel1] # Should prefer rangestrike to 1:2 melee assert move_to_score["D4"] > move_to_score["C3"] # Should prefer rangestrike to 1:1 melee assert move_to_score["D2"] > move_to_score["C1"] a_warlock1.move("C1") a_warlock2.move("C3") a_gargoyle1.move("D4") a_gargoyle2.move("D5") a_cyclops1.move("E5") a_cyclops2.move("E2") a_titan.move("F4")
def __init__(self, playername, legion, mterrain, caretaker, def1, parent): gtk.Dialog.__init__(self, "PickRecruit - %s" % playername, parent) self.legion = legion player = legion.player self.deferred = def1 self.set_icon(icon.pixbuf) self.set_transient_for(parent) self.set_destroy_with_parent(True) self.vbox.set_spacing(9) legion_name = gtk.Label( "Pick recruit for legion %s (%s) in hex %s" % (legion.markerid, legion.picname, legion.hexlabel)) self.vbox.pack_start(legion_name) legion_hbox = gtk.HBox(spacing=15) self.vbox.pack_start(legion_hbox) marker_hbox = gtk.HBox() legion_hbox.pack_start(marker_hbox, expand=False) chits_hbox = gtk.HBox(spacing=3) legion_hbox.pack_start(chits_hbox, expand=False) marker = Marker.Marker(legion, True, scale=20) marker_hbox.pack_start(marker.event_box, expand=False, fill=False) for creature in legion.sorted_living_creatures: chit = Chit.Chit(creature, player.color, scale=20) chits_hbox.pack_start(chit.event_box, expand=False) recruit_tups = legion.available_recruits_and_recruiters( mterrain, caretaker) max_len = max(len(tup) for tup in recruit_tups) for tup in recruit_tups: hbox = gtk.HBox() self.vbox.pack_start(hbox) recruit_name = tup[0] recruit = Creature.Creature(recruit_name) recruiter_names = tup[1:] li = list(tup) li.reverse() while len(li) < max_len: li.insert(-1, "Nothing") li.insert(-1, "RightArrow") for ii, name in enumerate(li): if name in ["RightArrow", "Nothing"]: chit = Chit.Chit(None, player.color, scale=20, name=name) else: creature = Creature.Creature(name) chit = Chit.Chit(creature, player.color, scale=20) chit.recruit = recruit chit.recruiter_names = recruiter_names if ii < len(li) - 2: hbox.pack_start(chit.event_box, expand=False) elif ii == len(li) - 2: hbox.pack_start(chit.event_box, expand=True) else: hbox.pack_end(chit.event_box, expand=False) chit.connect("button-press-event", self.cb_click) label = gtk.Label(caretaker.num_left(creature.name)) hbox.pack_end(label, expand=False) self.add_button("gtk-cancel", gtk.RESPONSE_CANCEL) self.connect("response", self.cb_cancel) self.show_all()
def test_score_legion_move_plain(): now = time.time() game = Game.Game("g1", "p0", now, now, 2, 6) game.add_player("p1") player0 = game.players[0] player1 = game.players[1] player0.assign_starting_tower(200) player1.assign_starting_tower(100) game.sort_players() game.started = True game.assign_color("p1", "Blue") game.assign_color("p0", "Red") game.assign_first_marker("p0", "Rd01") game.assign_first_marker("p1", "Bu01") player0.pick_marker("Rd02") player0.split_legion("Rd01", "Rd02", ["Titan", "Centaur", "Ogre", "Gargoyle"], ["Angel", "Centaur", "Ogre", "Gargoyle"]) rd01 = player0.markerid_to_legion["Rd01"] player1.pick_marker("Bu02") player1.split_legion("Bu01", "Bu02", ["Titan", "Centaur", "Ogre", "Gargoyle"], ["Angel", "Centaur", "Ogre", "Gargoyle"]) bu01 = player1.markerid_to_legion["Bu01"] rd01.creatures.append(Creature.Creature("Ranger")) rd01.creatures.append(Creature.Creature("Gorgon")) bu01.creatures.append(Creature.Creature("Ranger")) bu01.creatures.append(Creature.Creature("Gorgon")) rd01.move(101, False, None, 5) bu01.move(101, False, None, 5) game._init_battle(bu01, rd01) defender = game.defender_legion attacker = game.attacker_legion titan1 = defender.creatures[0] ogre1 = defender.creatures[1] centaur1 = defender.creatures[2] gargoyle1 = defender.creatures[3] ranger1 = defender.creatures[4] gorgon1 = defender.creatures[5] for creature in defender.creatures: creature.legion = defender for creature in attacker.creatures: creature.legion = attacker cleverbot_d = CleverBot.CleverBot("p0", 1) hexlabel = ogre1.hexlabel move_to_score = {} moves = game.find_battle_moves(ogre1) for move in moves: ogre1.move(move) score = cleverbot_d._score_legion_move(game, [ogre1]) move_to_score[move] = score ogre1.move(hexlabel) # Should prefer back rank to second rank assert move_to_score["E5"] > move_to_score["C5"] assert move_to_score["E5"] > move_to_score["E4"] hexlabel = gargoyle1.hexlabel move_to_score = {} moves = game.find_battle_moves(gargoyle1) print "ogre1 moves", moves for move in moves: gargoyle1.move(move) score = cleverbot_d._score_legion_move(game, [gargoyle1]) move_to_score[move] = score gargoyle1.move(hexlabel) # Should prefer back rank to second rank assert move_to_score["E5"] > move_to_score["C5"] assert move_to_score["F4"] > move_to_score["D5"] # Should prefer second rank to third rank assert move_to_score["E4"] > move_to_score["D4"] assert move_to_score["D5"] > move_to_score["C4"] ranger1.move("C5") gorgon1.move("D5") ogre1.move("E4") titan1.move("D6") centaur1.move("E5") gargoyle1.move("F4") cleverbot_a = CleverBot.CleverBot("p1", 1) game.battle_active_legion = attacker game.battle_phase = Phase.STRIKE titan2 = attacker.sorted_creatures[0] gorgon2 = attacker.sorted_creatures[1] ranger2 = attacker.sorted_creatures[2] gargoyle2 = attacker.sorted_creatures[3] centaur2 = attacker.sorted_creatures[4] ogre2 = attacker.sorted_creatures[5] hexlabel = titan2.hexlabel move_to_score = {} moves = game.find_battle_moves(titan2) for move in moves: titan2.move(move) score = cleverbot_a._score_legion_move(game, [titan2]) move_to_score[move] = score titan2.move(hexlabel) # Should prefer back rank to second rank assert move_to_score["A1"] > move_to_score["A2"] assert move_to_score["D1"] > move_to_score["D2"] # Should prefer second rank to third rank assert move_to_score["C2"] > move_to_score["D3"] # Should prefer third rank to fourth rank assert move_to_score["D3"] > move_to_score["B4"] hexlabel = ranger2.hexlabel move_to_score = {} moves = game.find_battle_moves(ranger2) for move in moves: ranger2.move(move) score = cleverbot_a._score_legion_move(game, [ranger2]) move_to_score[move] = score ranger2.move(hexlabel) # Should prefer second rank to back rank assert move_to_score["A2"] > move_to_score["A1"] assert move_to_score["D2"] > move_to_score["D1"] # Should prefer rangestrike to melee assert move_to_score["A3"] > move_to_score["B4"] assert move_to_score["B3"] > move_to_score["B4"] assert move_to_score["C3"] > move_to_score["B4"] assert move_to_score["D3"] > move_to_score["B4"] assert move_to_score["E2"] > move_to_score["B4"] # Should prefer 1-on-1 to 1-on-2 assert move_to_score["B4"] > move_to_score["C4"] assert move_to_score["E3"] > move_to_score["C4"] assert move_to_score["E3"] > move_to_score["D4"] # Should prefer 1-on-1 with advantage to even 1-on-1. assert move_to_score["E3"] > move_to_score["B4"] hexlabel = gorgon2.hexlabel move_to_score = {} moves = game.find_battle_moves(gorgon2) for move in moves: gorgon2.move(move) score = cleverbot_a._score_legion_move(game, [gorgon2]) move_to_score[move] = score gorgon2.move(hexlabel) # Should prefer second rank to back rank assert move_to_score["D2"] > move_to_score["D1"] # Should prefer third rank to second rank assert move_to_score["D3"] > move_to_score["C2"] # Should prefer rangestrike opportunity to none assert move_to_score["A3"] > move_to_score["F1"] assert move_to_score["B3"] > move_to_score["F1"] assert move_to_score["C3"] > move_to_score["F1"] assert move_to_score["D3"] > move_to_score["F1"] assert move_to_score["E2"] > move_to_score["F1"] gorgon2.move("A3") ranger2.move("B3") hexlabel = centaur2.hexlabel move_to_score = {} moves = game.find_battle_moves(centaur2) for move in moves: centaur2.move(move) score = cleverbot_a._score_legion_move(game, [centaur2]) move_to_score[move] = score centaur2.move(hexlabel) # Should prefer being next to allies assert move_to_score["C3"] > move_to_score["D3"] centaur2.move("C3") hexlabel = gargoyle2.hexlabel move_to_score = {} moves = game.find_battle_moves(gargoyle2) for move in moves: gargoyle2.move(move) score = cleverbot_a._score_legion_move(game, [gargoyle2]) move_to_score[move] = score gargoyle2.move("D3") hexlabel = ogre2.hexlabel move_to_score = {} moves = game.find_battle_moves(ogre2) for move in moves: ogre2.move(move) score = cleverbot_a._score_legion_move(game, [ogre2]) move_to_score[move] = score ogre2.move(hexlabel) # Should prefer being up next to allies assert move_to_score["A2"] == max(move_to_score.itervalues()) ogre2.move("A2") hexlabel = titan2.hexlabel titan2.moved = False move_to_score = {} moves = game.find_battle_moves(titan2) for move in moves: titan2.move(move) score = cleverbot_a._score_legion_move(game, [titan2]) move_to_score[move] = score titan2.move(hexlabel) # Should prefer hiding in back behind the ogre print move_to_score assert move_to_score["A1"] == max(move_to_score.itervalues()) titan2.move("A1")