Ejemplo n.º 1
0
 def test_hex_at_coordinates(self):
     hg = hexutil.HexGrid(32)
     data = [((0, 0), hexutil.Hex(0, 0)), ((33, 16), hexutil.Hex(2, 0)),
             ((30, 20), hexutil.Hex(1, 1))]
     for fx in (-1, 1):
         for fy in (-1, 1):
             for pixel, hex in data:
                 x, y = pixel
                 pixel = (fx * x, fy * y)
                 x, y = hex
                 hex = hexutil.Hex(fx * x, fy * y)
                 self.assertEqual(hg.hex_at_coordinate(*pixel), hex, pixel)
Ejemplo n.º 2
0
 def get_map(self, max_distance=None, path=frozenset()):
     if max_distance is not None:
         fov = self.field_of_view(max_distance)
         if self.lights:
             light_fov = {}
             for light in self.lights:
                 light.field_of_view(transparent=self.is_transparent,
                                     max_distance=max_distance,
                                     visible=light_fov)
         else:
             light_fov = fov
     else:
         fov = light_fov = None
     lines = []
     for y, line_length in enumerate(self.line_lengths):
         line = []
         for x in range(line_length):
             if (x + y) % 2 != 0:
                 ch = ' '
             else:
                 pos = hexutil.Hex(x, y)
                 if pos == self.player:
                     ch = '@'
                 elif fov is None or (fov.get(pos, 0)
                                      & light_fov.get(pos, 0)):
                     if pos in path:
                         ch = '%'
                     else:
                         ch = self.tiles.get(pos, ' ')
                 else:
                     ch = ' '
             line.append(ch)
         lines.append("".join(line).rstrip())
     return "\n".join(lines)
Ejemplo n.º 3
0
 def test_distance(self):
     h = hexutil.Hex(-1, -3)
     for nb in h.neighbours():
         self.assertEqual(nb.distance(nb), 0)
         self.assertEqual(nb.distance(h), 1)
         self.assertEqual(max(nb2.distance(h) for nb2 in nb.neighbours()),
                          2)
Ejemplo n.º 4
0
 def __init__(self, str):
     self.source = str
     player = None
     target = None
     tiles = {}
     line_lengths = []
     lights = []
     for y, line in enumerate(str.split("\n")):
         line_lengths.append(len(line))
         for x, ch in enumerate(line):
             if ch.isspace():
                 continue
             position = hexutil.Hex(x, y)
             if ch == '@':
                 player = position
                 ch = '.'
             elif ch == '%':
                 ch = '.'
             elif ch == 'T':
                 target = position
             elif ch == '*':
                 lights.append(position)
             tiles[position] = ch
     self.player = player
     self.target = target
     self.tiles = tiles
     self.line_lengths = line_lengths
     self.lights = lights
Ejemplo n.º 5
0
 def test_hexes_in_rectangle(self):
     hg = hexutil.HexGrid(32)
     self.assertEqual(
         list(hg.hexes_in_rectangle(hg.bounding_box(hexutil.origin))), [
             hexutil.Hex(-1, -1),
             hexutil.Hex(1, -1),
             hexutil.Hex(-2, 0),
             hexutil.Hex(0, 0),
             hexutil.Hex(-1, 1),
             hexutil.Hex(1, 1)
         ])
Ejemplo n.º 6
0
    def random_hex(self):
        """Get random Hex from the board

        Returns
        -------
        hexutil.Hex
            Random hex from within the game board
        """
        y = randint(self.min_y, self.max_y)
        while True:
            if y % 2 == 0:
                x = randint(self.min_x, self.max_x)
            else:
                x = randint(self.min_x + 1, self.max_x + 1)
            if (x + y) % 2 == 0:
                break
        return hexutil.Hex(x, y)
Ejemplo n.º 7
0
 def __start_area(self, area):
     """Add first Hex to an area
     """
     shuffle(self.coordinates)
     for coord in self.coordinates:
         x = coord[0]
         y = coord[1]
         if self.a[y][x] == 0:
             h = hexutil.Hex(x, y)
             for n in h.neighbours():
                 if n.y in self.a and n.x in self.a[n.y] and self.a[n.y][
                         n.x] == 1:
                     self.h = h
                     self.possible_hexes.append(h)
                     self.a[h.y][h.x] = 2
                     self.__retag_neighbouring_hexes(h)
                     self.areas[area] = {'hexes': [h], 'neighbours': []}
                     return True
Ejemplo n.º 8
0
 def test_bounding_box(self):
     hg = hexutil.HexGrid(32)
     self.assertEqual(hg.bounding_box(hexutil.Hex(0, 2)),
                      hexutil.Rectangle(-32, 72, 64, 72))
Ejemplo n.º 9
0
 def test_center(self):
     hg = hexutil.HexGrid(32)
     self.assertEqual(hg.center(hexutil.Hex(1, 1)), (32, 54))
Ejemplo n.º 10
0
 def test_corners(self):
     self.assertEqual(
         hexutil.HexGrid(32).corners(hexutil.Hex(1, 1)), [(64, 72),
                                                          (32, 90), (0, 72),
                                                          (0, 36), (32, 18),
                                                          (64, 36)])
Ejemplo n.º 11
0
    hy.append(c[1])
    hxy.append(str(round(h.x)) + ',' + str(round(h.y)))

# covering by hexagons
i = 0
for country_id in ids_list:
    neighbours = _hneighbours(hexagons[country_id])
    random.shuffle(neighbours)
    best_neighbour = None
    for neighbour in neighbours:
        if neighbour not in h_all:
            best_neighbour = neighbour
            break
    if not best_neighbour:
        print('Oh mine, no more place for: ' + country_id)
        best_neighbour = hexutil.Hex(1000, 1000)
    else:
        best_loss = _loss(best_neighbour, countries[country_id]['multipolygon'])
        for neighbour in neighbours:
            if neighbour not in h_all:
                loss = _loss(neighbour, countries[country_id]['multipolygon'])
                loss_overlap = _loss_overlap(neighbour, countries, country_id)
                neighbour_loss = loss + 7 * loss_overlap
                if neighbour_loss == 0:
                    best_neighbour = neighbour
                    best_loss = neighbour_loss
                    break
                if neighbour_loss < best_loss:
                    best_neighbour = neighbour
                    best_loss = neighbour_loss
        hexagons[country_id].append(best_neighbour)
Ejemplo n.º 12
0
 def test_add(self):
     self.assertEqual(
         hexutil.Hex(2, 4) + hexutil.Hex(4, 6), hexutil.Hex(6, 10))
Ejemplo n.º 13
0
 def test_rotations(self):
     hex = hexutil.Hex(2, 0)
     self.assertEqual([r(hex) for r in hexutil.Hex.rotations],
                      hexutil.origin.neighbours())
Ejemplo n.º 14
0
 def test_is_valid(self):
     hexutil.Hex(-1, -3)
     self.assertRaises(hexutil.InvalidHex, hexutil.Hex, 2, -3)
Ejemplo n.º 15
0
def ij_to_hex(i,j):
    return hexutil.Hex(2*j+(i%2),i)
Ejemplo n.º 16
0
 def test_sub(self):
     self.assertEqual(
         hexutil.Hex(2, 4) - hexutil.Hex(3, 7), hexutil.Hex(-1, -3))
Ejemplo n.º 17
0
 def test_neg(self):
     self.assertEqual(-hexutil.Hex(2, 4), hexutil.Hex(-2, -4))
     self.assertEqual(-hexutil.origin, hexutil.origin)
Ejemplo n.º 18
0
 def get_hexes(self):
     """Return Hex objects of the Area
     """
     return [hexutil.Hex(h[0], h[1]) for h in self.hexes]
Ejemplo n.º 19
0
#!/usr/bin/env python
""" run as `python -O level_11.py` to disable debug garbage """

import gc

import hexutil

with open('in.txt', 'r') as f:
    for line in f:
        progPath = line.strip().split(",")
        if __debug__: print(progPath)

xcoord = 0
ycoord = 0
maxPathLen = 0
origin = hexutil.Hex(0, 0)
nstep = 0
totalPathLen = len(progPath)

for direction in progPath:
    nstep += 1
    if direction == "n":
        xcoord += 2
    elif direction == "s":
        xcoord -= 2
    elif direction == "nw":
        xcoord += 1
        ycoord += 1
    elif direction == "ne":
        xcoord += 1
        ycoord -= 1