def test_rotate_map(): s = TileMap() center = Tile('center') outer = Tile('outer') s[HexCoords(0, 0)] = center s[HexCoords(1, 0)] = outer rotation_and_expected_coords = [(0, [(0, 0), (1, 0)]), (1, [(0, 0), (1, -1)]), (2, [(0, 0), (0, -1)]), (3, [(0, 0), (-1, 0)]), (4, [(0, 0), (-1, 1)]), (5, [(0, 0), (0, 1)]), (6, [(0, 0), (1, 0)]), (-6, [(0, 0), (1, 0)]), (-5, [(0, 0), (1, -1)]), (-4, [(0, 0), (0, -1)]), (-3, [(0, 0), (-1, 0)]), (-2, [(0, 0), (-1, 1)]), (-1, [(0, 0), (0, 1)])] for rot, expected in rotation_and_expected_coords: rotated = s.rotated(rot) assert len(rotated) == 2 # rotated tiles must not lose their identity assert rotated[HexCoords(*expected[0])] is center assert rotated[HexCoords(*expected[1])] is outer m = TileMap() for c in [(0, 0), (0, 1), (1, 0), (-1, 1), (2, 0), (3, -1)]: m[HexCoords(*c)] = Tile()
def test_instantiate(): a = HexCoords(0, 0, 0) b = HexCoords(+1, 0, -1) c = HexCoords(-3, +1) assert a.q == a.r == a.s == b.r == 0 assert b.q == -b.s == c.r == 1 assert c.s == +2 with pytest.raises(ValueError): HexCoords(+3, -1, 0) # wrong!
def test_distance(): for repetition in range(200): coords_a = [randint(-5, 5) for i in range(2)] coords_b = [randint(-5, 10) for i in range(2)] a, b = HexCoords(*coords_a), HexCoords(*coords_b) delta = abs(a.q - b.q), abs(a.r - b.r), abs(a.s - b.s) maxdelta = max(delta) assert a.distance(b) == maxdelta if maxdelta == 1: assert a in b.neighbors assert b in a.neighbors
def test_add_tilemap(): a = TileMap() b = TileMap() for q in range(-1, 2): for r in range(-1, 2): if q == r == 0: b[HexCoords(q, r)] = Tile('center of galaxy') else: b[HexCoords(q, r)] = Tile() assert len(a) == 0 a.add_tilemap(b) assert len(a) == len(b) for key, val in a.items(): assert b[key] is val
def __init__(self, radius, fill_element=None): super().__init__() for q in range(-radius, radius + 1): rmin = max(-radius, -radius - q) # since s = -q-r we must ensure abs(s) <= radius rmax = min(radius, -q + radius) for r in range(rmin, rmax + 1): self[HexCoords(q, r)] = fill_element
def test_no_set_coordinates(): a = HexCoords(0, 0, 0) b = HexCoords(+1, 0, -1) c = HexCoords(-3, +1) with pytest.raises(AttributeError): a.q += 1 with pytest.raises(AttributeError): b.r = 5 with pytest.raises(AttributeError): c.s = 0
def test_neighbors(): a = HexCoords(0, 0, 0) b = HexCoords(+1, 0, -1) c = HexCoords(-3, +1) assert a in b.neighbors assert b in a.neighbors for coords in [(-2, 1, 1), (-4, 1, 3), (-3, 2, 1), (-3, 0, 3), (-2, 0, 2), (-4, 2, 2)]: assert HexCoords(*coords) in c.neighbors assert c.distance(HexCoords(*coords)) == 1
def test_hexshape(): h0 = HexShapedTileMap(0) h1 = HexShapedTileMap(1) h2 = HexShapedTileMap(2) assert len(h0) == 1 assert HexCoords(0, 0, 0) in h0 assert len(h1) == 7 for coords in h1.keys(): assert len(coords) <= 1 assert coords in h2.keys() assert len(h2) == 19 for coords in h2.keys(): assert len(coords) <= 2
def test_add(): a = HexCoords(0, 0, 0) b = HexCoords(+1, 0, -1) c = HexCoords(-3, +1) assert a + b == b assert a + b + c == HexCoords(-2, +1, +1) assert a + b + c - b == c assert a + (+1, 0) == a + (+1, 0, -1) == b b += c assert b == HexCoords(-2, +1, +1) b -= c assert b == HexCoords(+1, 0, -1) b -= b assert b == a == 0
def test_rotate(): hc = HexCoords(+2, +1, -3) assert hc.rotate(0) == hc == hc.rotate(6) == hc.rotate(-6) assert hc.rotate(1) == hc.rotate(-5) == HexCoords(+3, -2, -1) assert hc.rotate(2) == hc.rotate(-4) == HexCoords(+1, -3, +2) assert hc.rotate(3) == hc.rotate(-3) == HexCoords(-2, -1, +3) == -hc assert hc.rotate(4) == hc.rotate(-2) == HexCoords(-3, +2, +1) == -hc.rotate(1) assert hc.rotate(5) == hc.rotate(-1) == HexCoords(-1, +3, -2) == -hc.rotate(2)
def test_iter(): hc = HexCoords(-4, 1, 3) for number, coord in zip([-4, 1, 3], hc): assert number == coord
def pixel_to_hex(self, coords): """ :param coords: (x,y) screen coordinates""" px, py = coords[0] - self.x, coords[1] - self.y qfloat = 2. / 3 * px / self._tile_radius rfloat = (-1. / 3 * px - _sqrt3 / 3 * py) / self._tile_radius return HexCoords.round(qfloat, rfloat)