def test_pointy(self): grid = Grid(OrientationPointy, Point(10, 20), Point(20, 10)) self.assertHexEqual(Hex(-21, 43), grid.hex_at(Point(13, 666))) self.assertHexEqual(Hex(19, 0), grid.hex_at(Point(666, 13))) self.assertHexEqual(Hex(22, -46), grid.hex_at(Point(-13, -666))) self.assertHexEqual(Hex(-19, -2), grid.hex_at(Point(-666, -13)))
def test_intersecting_ranges(self): """Test intersecting hex ranges.""" grid = Grid(7) grid.create_grid() range1 = [Hex(-1, 1, 0), Hex(0, 0, 0)] range2 = [Hex(1, -1, 0), Hex(0, 0, 0)] result = [Hex(0, 0, 0)] self.assertEqual(grid.intersecting_hex_ranges(range1, range2), result)
def test_hex_interpollating(self): """Test hex interpollating.""" grid = Grid(5) grid.create_grid() hex_a = Hex(-1, 0, 1) hex_b = Hex(1, 0, -1) hex_result = (0, 0, 0) self.assertEqual(grid.hex_interpolate(hex_a, hex_b, 0.5), hex_result)
def test_flat(self): grid = Grid(OrientationFlat, Point(10, 20), Point(20, 10)) self.assertHexEqual(Hex(0, 37), grid.hex_at(Point(13, 666))) self.assertHexEqual(Hex(22, -11), grid.hex_at(Point(666, 13))) self.assertHexEqual(Hex(-1, -39), grid.hex_at(Point(-13, -666))) self.assertHexEqual(Hex(-22, 9), grid.hex_at(Point(-666, -13))) grid = Grid(OrientationFlat, Point(0, 0), Point(5, 5)) self.assertHexEqual(Hex(0, 37), grid.hex_at(Point(0, 320)))
def test_move_unit_to_hex(self): """Test the move_unit_to_hex method.""" civ = Civilisation("myCiv", grid, logger) hextile = Hex(0, 0, 0) hextile2 = Hex(1, 0, -1) archer = Archer(1, 1, hextile, "myCiv") archer.actions = 2 civ.move_unit_to_hex(archer, hextile2) self.assertEqual(archer.position, hextile2) self.assertEqual(archer.actions, 1)
def test_no_unit_tile(self): """Tests the no_unit_tile function""" hextile = Hex(0, 0, 0) hextile2 = Hex(0, 1, -1) city = City(1, hextile, 1) tile1 = city.no_unit_tile() self.assertEqual(tile1, None) city._tiles += [hextile2] tile2 = city.no_unit_tile() self.assertEqual(tile2, hextile2)
def test_attack_unit(self): civ = Civilisation("myCiv", grid, logger) hextile = Hex(0, 0, 0) archer = Archer(1, 1, hextile, "myCiv") hextile2 = Hex(1, 0, -1) archer.actions = 2 swordsman = Swordsman(1, 1, hextile2, "notMyCiv") civ.attack_unit(archer, swordsman) self.assertEqual(swordsman.health, 110) self.assertEqual(round(archer.health), 85) self.assertEqual(archer.actions, 1)
def test_shortest_path(self): """Test shortest path.""" grid = Grid(5) grid.create_grid() start = Hex(-1, 1, 0) end = Hex(1, -1, 0) grid.get_hextile((0, 0, 0)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) grid.get_hextile((0, 1, -1)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) grid.get_hextile((-1, 0, 1)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) result = [Hex(-1, 2, -1), Hex(2, -2, 0), Hex(1, -1, 0)] self.assertEqual(grid.shortest_path(start, end, 3), result)
def test_scaling_of_hex(self): """Test hex scaling.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((0, 1, -1)) hex_result = Hex(0, 2, -2) self.assertEqual(grid.scale(hex_a, 2), hex_result)
def test_getting_neighbour_of_hex_in_direction(self): """Test get neighbour in direction.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, -1, 0)) hex_result = Hex(2, -1, -1) self.assertEqual(grid.get_neighbour_in_direction(hex_a, 1), hex_result)
def test_rotate_left_of_hex(self): """Test rotate left.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, 0, -1)) hex_result = Hex(1, -1, 0) self.assertEqual(grid.rotate_left(hex_a), hex_result)
def test_per_turn(self): # tbc civ = Civilisation("myCiv", grid, logger) hextile = Hex(0, 0, 0) worker = Worker("worker", 1, hextile, "myCiv") civ.units[worker.id] = worker archer = Archer(1, 1, hextile, "myCiv") civ.units[archer.id] = archer civ.reset_unit_actions_and_movement() grid.create_grid() civ.build_city_on_tile(worker, 1) hextile2 = grid.get_neighbour_in_direction(hextile, 2) worker.position = hextile2 civ.build_structure(worker, BuildingType.UNIVERSITY, 1) civ.reset_unit_actions_and_movement() hextile3 = grid.get_neighbour_in_direction(hextile, 4) worker.position = hextile3 civ.build_structure(worker, BuildingType.FARM, 2) civ.reset_unit_actions_and_movement() civ.currency_per_turn() self.assertEqual(archer.actions, 2) self.assertEqual(worker.actions, 2) self.assertEqual(archer.movement, 5) self.assertEqual(worker.movement, 4) self.assertEqual(civ.food, 99) self.assertEqual(civ.gold, 51) self.assertEqual(civ.science, 5)
def receive_damage(self): """Test that the correct amount of damage is taken.""" hextile = Hex(0, 0, 0) unit = Unit(100, 1, 5, {'food': 2, 'gold': 1, 'science': 0}, hextile) unit.receive_damage(25) self.assertEqual(unit._health, 75)
def test_adding_hex_to_hex(self): """Test adding hexs.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, 0, -1)) hex_b = grid.get_hextile((-1, 2, -1)) hex_result = Hex(0, 2, -2) self.assertEqual(grid.add_coords(hex_a, hex_b), hex_result)
def test_subtracting_hex_from_hex(self): """Test subtracting hexs.""" grid = Grid(5) grid.create_grid() hex_a = grid.get_hextile((1, 0, -1)) hex_b = grid.get_hextile((-1, 2, -1)) hex_result = Hex(2, -2, 0) self.assertEqual(grid.sub_coords(hex_a, hex_b), hex_result)
def test_swordsman_attributes(self): """Test that swordsman's attributes are initialised correctly.""" hextile = Hex(0, 0, 0) swordsman = Swordsman("soldier", 1, hextile, 1) self.assertEqual(swordsman._health, 130) self.assertEqual(swordsman._movement_range, 4) self.assertEqual(swordsman._strength, 30) self.assertEqual(swordsman._cost, {'food': 1, 'gold': 0, 'science': 0})
def test_worker_attributes(self): """Test that worker's attributes are initialised correctly.""" hextile = Hex(0, 0, 0) worker = Worker("worker", 2, hextile, 1) self.assertEqual(worker._health, 110) self.assertEqual(worker._movement_range, 5) self.assertEqual(worker._cost, {'food': 2, 'gold': 0, 'science': 0})
def test_worker_level_up(self): """Test that worker unit levels up and attributes increase.""" hextile = Hex(0, 0, 0) hextile2 = Hex(1, 0, -1) worker1 = Worker("worker", 1, hextile, 1) worker2 = Worker("worker", 3, hextile2, 1) worker1.level_up() worker2.level_up() self.assertEqual(worker1._cost, {'food': 2, 'gold': 0, 'science': 0}) self.assertEqual(worker1._health, 120) self.assertEqual(worker1._movement_range, 5) self.assertEqual(worker2._cost, {'food': 3, 'gold': 0, 'science': 0}) self.assertEqual(worker2._health, 120) self.assertEqual(worker2._movement_range, 6)
def test_archer_attributes(self): """Test that archer's attributes are initialised correctly.""" hextile = Hex(0, 0, 0) archer = Archer("archer", 1, hextile, 1) self.assertEqual(archer._health, 110) self.assertEqual(archer._movement_range, 5) self.assertEqual(archer._strength, 20) self.assertEqual(archer._attack_range, 2) self.assertEqual(archer._cost, {'food': 2, 'gold': 0, 'science': 0})
def test_movement_cost_of_path(self): """Test the movement_cost_of_path function.""" civ = Civilisation("myCiv", grid, logger) hextile = Hex(0, 0, 0) hextile2 = Hex(1, 0, -1) hextile3 = Hex(1, 1, -2) hextile._terrain = Terrain(TerrainType.FLAT, BiomeType.GRASSLAND) hextile2._terrain = Terrain(TerrainType.HILL, BiomeType.GRASSLAND) hextile3._terrain = Terrain(TerrainType.HILL, BiomeType.DESERT) # costs are 1, 2 and 3 respectively hexes = [hextile, hextile2, hextile3] archer = Archer(1, 1, hextile, "myCiv") archer.actions = 2 cost = civ.movement_cost_of_path(hexes) self.assertEqual(cost, 6)
def test_polygon_corners(self): """Test polygon corners.""" hexagon = Hex(1, 0, -1) pointy = Layout(10, (200, 200)) self.assertEqual(pointy.polygon_corners(hexagon), [(225.98076211353316, 205.0), (225.98076211353316, 195.0), (217.32050807568876, 190.0), (208.66025403784437, 195.0), (208.66025403784437, 205.0), (217.32050807568876, 210.0)])
def test_building_constructor(self): """Tests the building constructor""" hextile = Hex(0, 0, 0) building = Building(1, BuildingType.UNIVERSITY, hextile, 1, 2) self.assertEqual(building._id, 1) self.assertEqual(building._type, BuildingType.UNIVERSITY) self.assertEqual(building._city_id, 2) self.assertEqual(building._civ_id, 1) self.assertEqual(building._location, hextile)
def test_soldier_attributes(self): """Test that soldier's attributes are initialised correctly.""" hextile = Hex(0, 0, 0) soldier = Soldier("soldier", 120, 1, 5, 6, 4, { 'food': 2, 'gold': 1, 'science': 0 }, 10, hextile, 1) self.assertEqual(soldier._strength, 6) self.assertEqual(soldier._attack_range, 4)
def test_set_up(self): """Test the set_up function.""" hextile = Hex(0, 0, 0) civ = Civilisation("myCiv", grid, logger) civ.set_up(hextile, "worker") worker = civ.units["worker"] self.assertEqual(worker.actions, 2) self.assertEqual(hextile.unit, worker) self.assertEqual(civ.units["worker"], worker)
def test_is_dead(self): civ = Civilisation("myCiv", grid, logger) hextile = Hex(0, 0, 0) archer = Archer(1, 1, hextile, "myCiv") archer.health = 0 civ.units[archer.id] = archer archer._civilisation = civ civ.is_dead(archer) self.assertEqual(archer.health, 0) self.assertNotIn(archer, civ.units)
def test_swordsman_level_up(self): """Test that swordsman levels up and attributes increase.""" hextile = Hex(0, 0, 0) hextile2 = Hex(1, 0, -1) sword1 = Swordsman("sword", 1, hextile, 1) sword2 = Swordsman("sword", 3, hextile2, 1) sword1.level_up() sword2.level_up() self.assertEqual(sword1._cost, {'food': 2, 'gold': 1, 'science': 0}) self.assertEqual(sword1._health, 160) self.assertEqual(sword1._movement_range, 5) self.assertEqual(sword1._strength, 50) self.assertEqual(sword2._cost, {'food': 3, 'gold': 2, 'science': 0}) self.assertEqual(sword2._health, 190) self.assertEqual(sword2._movement_range, 6) self.assertEqual(sword2._strength, 70)
def test_upgrade_unit(self): """Test the upgrade_unit method.""" civ = Civilisation("myCiv", grid, logger) civ.unlock_research(3) hextile = Hex(0, 0, 0) archer = Archer(1, 1, "myCiv", hextile) archer.actions = 2 civ.upgrade_unit(archer) self.assertEqual(archer.level, 2) self.assertEqual(archer.actions, 1)
def get_mirrors(self): """Store each hexgrid mirror layout in a list.""" mirror_centers = self._grid.mirrors layouts = [] for mirror in mirror_centers: layout = Layout( self._layout.size, self._layout.hex_to_pixel(Hex(mirror[0], mirror[1], mirror[2]))) layouts.append(layout) return layouts
def test_city_constructor(self): """Tests the constructor for the city class""" hextile = Hex(0, 0, 0) city = City(1, hextile, 1) self.assertEqual(city._hex, hextile) self.assertEqual(hextile.building, city) self.assertEqual(city._tiles, []) self.assertEqual(city._type, BuildingType.CITY) self.assertEqual(city._id, 1) self.assertEqual(city._civ_id, 1) self.assertEqual(city._buildings, {})
def test_vision(self): """Test vision.""" grid = Grid(5) grid.create_grid() hexagon = Hex(-2, 2, 0) grid.get_hextile((-1, 1, 0)).terrain = Terrain(TerrainType.MOUNTAIN, BiomeType.GRASSLAND) result = [ Hex(-1, 1, 0), Hex(0, 2, -2), Hex(-2, 0, 2), Hex(-2, 1, 1), Hex(-2, 2, 0), Hex(-1, 0, 1), Hex(-1, 2, -1), Hex(0, 1, -1) ] self.assertEqual(set(grid.vision(hexagon, 2)), set(result))