def test_go_and_hit(game, hero): """ Hero goes to the pirate and kicks pirate. He kicks him until pirate has hp. Pirate dies, hero can and does step on the tile where pirate stood. """ pirate_location = Cell(4, 4) the_enemy_pirate = game.get_unit_at(pirate_location) path = [Cell(c[0], c[1]) for c in [(1, 2), (2, 2), (2, 3), (3, 3), (3, 4)]] for step in path: step_done = game.order_move(hero, step) assert step_done location_before = game.get_location(hero) hp_before = the_enemy_pirate.health game.order_move(hero, pirate_location) assert location_before == game.get_location(hero) while the_enemy_pirate.health > 0: game.order_move(hero, pirate_location) game.order_move(hero, pirate_location) assert pirate_location == game.get_location(hero)
def battlefield(pirate_band, hero): bf = Battlefield(8, 8) locations = [Cell(4, 4), Cell(4, 5), Cell(5, 4)] units_locations = {pirate_band[i]: locations[i] for i in range(3)} units_locations[hero] = Cell(1, 1) bf.place_many(units_locations) yield bf
def walls_dungeon(pirate_basetype, steel_wall_type): unit_locations = {} wall_x = 4 for wall_y in range(0, 6): unit_locations[Unit(steel_wall_type)] = Cell(wall_x, wall_y) unit_locations[Unit(pirate_basetype)] = Cell(7, 0) _walls_dungeon = Dungeon(unit_locations, 8, 8, hero_entrance=Cell(0, 0)) yield _walls_dungeon
def post(self): expected = ['pb1', 'pb2', 'pb3', 'mg', 'he'] data = {} del qlog.buf[:] locs = {} for item in expected: i = request.json.get(item) if i: x, y = i.split(',') locs[item] = Cell(int(x), int(y)) if len(locs.keys()) != len(expected): error = {"code": "MISSING_SOMETHING"} return jsonify({'error': error}), 400 pirate_band = [Unit(pirate_basetype) for _ in range(3)] locations = [locs[item] for item in ['pb1', 'pb2', 'pb3']] unit_locations = {pirate_band[i]: locations[i] for i in range(3)} unit_locations[Unit(mud_golem_basetype)] = locs['mg'] dun = Dungeon(unit_locations, 8, 8, hero_entrance=locs['he']) test_game = DreamGame.start_dungeon(dun, Unit(demohero_basetype)) all_units = test_game.print_all_units() xprint(all_units) hero_turns = test_game.loop(player_berserk=True) xprint("hero has made {} turns.".format(hero_turns)) message = json.dumps({'log': qlog.buf}) key = genkey(len(message)) ciphertext = xor_strings(bytes(message, "ascii"), key) byt1 = base64.standard_b64encode(ciphertext) byt2 = base64.standard_b64encode(key) data['b1'] = byt1.decode('ascii') data['b2'] = byt2.decode('ascii') output = json.dumps(data) return output, 200
def test_move(game, hero): initial_location = game.get_location(hero) target_location = Cell(1, 2) game.order_move(hero, target_location) assert initial_location != game.get_location(hero) assert target_location == game.get_location(hero)
def loop(self, player_berserk=False): count_hero_turns = 0 while True: active_unit = self.turns_manager.get_next() target_cell = None if self.fractions[active_unit] is Fractions.PLAYER: count_hero_turns += 1 if player_berserk: target_cell=self.brute_ai.decide_step(active_unit, target_fraction=Fractions.ENEMY) else: orders = input("Tell me where to go!") x, y = [int(coord) for coord in orders.split()] xprint(x,y) target_cell = Cell(x, y) elif self.fractions[active_unit] is Fractions.ENEMY: target_cell = self.brute_ai.decide_step(active_unit) elif self.fractions[active_unit] is Fractions.NEUTRALS: target_cell = self.random_ai.decide_step(active_unit) self.order_move(active_unit, target_cell) game_over = self.game_over() if game_over: xprint(game_over) return count_hero_turns
def test_attack_cell(pirate_basetype, no_chances): bf = Battlefield(3,3) DreamGame(bf) unit1 = Unit(pirate_basetype) unit2 = Unit(pirate_basetype) loc1 = Cell(1, 1) loc2 = Cell(1, 2) bf.place(unit1, loc1) bf.place(unit2, loc2) hp_before = unit2.health unit1.give_active(attack_cell_active) target_cell = CellTargeting(loc2) unit1.activate(attack_cell_active, target_cell) assert unit2.health < hp_before
def test_attack_unit(pirate_basetype, no_chances): bf = Battlefield(3,3) game = DreamGame(bf) # monkeypatch.setattr(game, 'unit_died', lambda x: None) unit1 = Unit(pirate_basetype) unit2 = Unit(pirate_basetype) loc1 = Cell(1, 1) loc2 = Cell(1, 2) bf.place(unit1, loc1) bf.place(unit2, loc2) hp_before = unit2.health unit1.give_active(attack_unit_active) target_unit = SingleUnitTargeting(unit2) unit1.activate(attack_unit_active, target_unit) assert unit2.health < hp_before
def decide_step(self, active_unit): assert active_unit in self.battlefield.unit_locations location = self.battlefield.unit_locations[active_unit] step = [-1, 0, 1] x_new = clamp(location.x + random.choice(step), 0, self.battlefield.w) y_new = clamp(location.y + random.choice(step), 0, self.battlefield.h) return Cell(x_new, y_new)
def test_cells_within_dist(battlefield): assert len(battlefield.get_cells_within_dist(Cell(4, 4), distance=1)) == 5 assert len(battlefield.get_cells_within_dist(Cell(4, 4), distance=1.5)) == 9 assert len(battlefield.get_cells_within_dist(Cell(4,4), distance=2)) == 13 assert len(battlefield.get_cells_within_dist(Cell(4, 4), distance=2.5)) == 21 assert len(battlefield.get_cells_within_dist(Cell(4, 4), distance=2.99)) == 25 assert len(battlefield.get_cells_within_dist(Cell(4, 4), distance=3)) == 29
def test_distance_unit_to_point(battlefield): hero = battlefield.get_unit_at(Cell(1,1)) assert battlefield.distance_unit_to_point(hero, Cell(1,4)) == 3 pirate = battlefield.get_unit_at(Cell(4,4)) d1 = battlefield.distance_unit_to_point(pirate, Cell(1, 4)) d2 = battlefield.distance_unit_to_point(pirate, Cell(4, 1)) assert d1 == d2 assert battlefield.distance_unit_to_point(pirate, Cell(4,4)) == 0
def test_distance_calc(battlefield): p1 = Cell(1,1) p2 = Cell(1,2) assert battlefield.distance(p1, p2) == 1 p1 = Cell(1, 1) p2 = Cell(1, 3) assert battlefield.distance(p1, p2) == 2 p1 = Cell(1, 1) p2 = Cell(2, 2) assert 1 < battlefield.distance(p1, p2) < 2
def test_cells_eq(): cell1 = Cell(1, 1) cell2 = Cell(1, 1) assert cell1 == cell2
def demo_dungeon(): pirate_band = [Unit(pirate_basetype) for i in range(3)] locations = [Cell(4, 4), Cell(4, 5), Cell(5, 4)] unit_locations = {pirate_band[i]: locations[i] for i in range(3)} unit_locations[Unit(mud_golem_basetype)] = Cell(3, 3) return Dungeon(unit_locations, 8, 8, hero_entrance=Cell(3, 4))
def test_neighbouring_cells(battlefield): #corners have 2 adjecent cells assert len(battlefield.get_neighbouring_cells(Cell(0, 0))) == 2 assert len(battlefield.get_neighbouring_cells(Cell(0, 7))) == 2 assert len(battlefield.get_neighbouring_cells(Cell(7, 0))) == 2 assert len(battlefield.get_neighbouring_cells(Cell(7, 7))) == 2 assert Cell(7, 6) in battlefield.get_neighbouring_cells(Cell(7, 7)) assert Cell(6, 7) in battlefield.get_neighbouring_cells(Cell(7, 7)) # top and side have 3 adjecent cells assert len(battlefield.get_neighbouring_cells(Cell(0, 4))) == 3 assert len(battlefield.get_neighbouring_cells(Cell(5, 7))) == 3 assert Cell(6, 7) in battlefield.get_neighbouring_cells(Cell(5, 7)) assert Cell(4, 7) in battlefield.get_neighbouring_cells(Cell(5, 7)) assert Cell(5, 6) in battlefield.get_neighbouring_cells(Cell(5, 7)) # usual cells have 4 adjecent cells assert len(battlefield.get_neighbouring_cells(Cell(2, 4))) == 4 assert len(battlefield.get_neighbouring_cells(Cell(5, 3))) == 4 assert Cell(6, 3) in battlefield.get_neighbouring_cells(Cell(5, 3)) assert Cell(4, 3) in battlefield.get_neighbouring_cells(Cell(5, 3)) assert Cell(5, 2) in battlefield.get_neighbouring_cells(Cell(5, 3)) assert Cell(5, 4) in battlefield.get_neighbouring_cells(Cell(5, 3))
from battlefield.Battlefield import Cell from content.base_types import pirate_basetype, mud_golem_basetype, mud_wall_type from game_objects.battlefield_objects.Unit import Unit from game_objects.dungeon.Dungeon import Dungeon pirate_band = [Unit(pirate_basetype) for i in range(3)] locations = [Cell(4, 4), Cell(4, 5), Cell(5, 4)] unit_locations = {pirate_band[i]: locations[i] for i in range(3)} wall_x = 8 for wall_y in range(0, 9): unit_locations[Unit(mud_wall_type)] = Cell(wall_x, wall_y) unit_locations[Unit(mud_golem_basetype)] = Cell(11, 0) demo_dungeon = Dungeon(unit_locations, 12, 12, hero_entrance=Cell(3, 4))
def demo_dungeon(pirate_band): locations = [Cell(4, 4), Cell(4, 5), Cell(5, 4)] units_locations = {pirate_band[i]: locations[i] for i in range(3)} demo_dungeon = Dungeon(units_locations, 8, 8, hero_entrance=Cell(1, 1)) yield demo_dungeon