def test_distance(self): h0 = hx.to_hex(2, 2) self.assertEqual(0, hx.distance(h0, h0)) h1 = hx.to_hex(1, 3) self.assertEqual(1, hx.distance(h0, h1)) h2 = hx.to_hex(4, 5) self.assertEqual(4, hx.distance(h0, h2))
def test_move(self): origin = hx.to_hex(2, 1) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_E)) self.assertEqual(3, col) self.assertEqual(1, row) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_W)) self.assertEqual(1, col) self.assertEqual(1, row) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_SE)) self.assertEqual(3, col) self.assertEqual(2, row) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_SW)) self.assertEqual(2, col) self.assertEqual(2, row) origin = hx.to_hex(2, 2) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_E)) self.assertEqual(3, col) self.assertEqual(2, row) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_W)) self.assertEqual(1, col) self.assertEqual(2, row) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_SE)) self.assertEqual(2, col) self.assertEqual(3, row) col, row = hx.to_offset(hx.move(origin, hx.DIRECTION_SW)) self.assertEqual(1, col) self.assertEqual(3, row)
def test_rotate(self): pivot = hx.to_hex(1, 5) h = hx.to_hex(4, 4) col, row = hx.to_offset(hx.rotate(pivot, h, hx.TURN_CW)) self.assertEqual(col, 3) self.assertEqual(row, 7) col, row = hx.to_offset(hx.rotate(pivot, h, hx.TURN_CCW)) self.assertEqual(col, 2) self.assertEqual(row, 2)
def test_hex(): def g(n1, n2): return 1 def nf(g): def neighbours(n): nb = [] for d in [hx.DIRECTION_E, hx.DIRECTION_W, hx.DIRECTION_SE, hx.DIRECTION_SW]: new_n = hx.hex_add(n, d) col, row = hx.to_offset(new_n) if 0 <= col < 10 and 0 <= row < 10: nb.append(new_n) return nb return neighbours def hf(goal): def h(n): return hx.distance(goal, n) return h start = hx.to_hex(0, 0) goal = hx.to_hex(6, 7) f, p = astar.astar(start, goal, g, hf(goal), nf(None)) print f, [hx.to_offset(x) for x in p]
def h(n): gc, gr = goal.pivot nc, nr = n.pivot return hx.distance(hx.to_hex(gc, gr), hx.to_hex(nc, nr)) + goal.abs_rotation_distance(n)