Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
"Find Enemy" with Cube coordinates solution in Clear category for Find Enemy by Vitaly_Titkov
alphabet = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11, 'L': 12, 'M': 13,
'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25,
'Z': 26}
def direction_of_travel(hex):
if hex[1] > 0 and hex[2] < 0:
return "F"
elif hex[0] > 0 and hex[1] < 1 and hex[2] < 1:
return "R"
elif hex[1] < 0 and hex[2] > 0:
return "B"
elif hex[0] < 0 and hex[1] > -1 and hex[2] > -1:
return "L"
def coordinates_after_rotation(dir, enemy):
if dir == "N":
return enemy
elif dir == "S":
return (-enemy[0], -enemy[1], -enemy[2])
elif dir == 'NE':
return (-enemy[1], -enemy[2], -enemy[0])
elif dir == 'SE':
return (enemy[2], enemy[0], enemy[1])
elif dir == 'NW':
return (-enemy[2], -enemy[0], -enemy[1])
elif dir == 'SW':
return (enemy[1], enemy[2], enemy[0])
def rotation_for_determining_the_direction_of_movement(you, dir, enemy):
# Convert positions P and C to cube coordinates.
center = axial_to_cube(you)
position = axial_to_cube(enemy)
# Calculate a vector by subtracting the center: P_from_C = position - center
position_from_center = (position[0] - center[0], position[1] - center[1], position[2] - center[2])
# Rotate the vector position_from_center as described in coordinates_after_rotation,
# and call the resulting vector R_from_C.
rotate_from_center = coordinates_after_rotation(dir, position_from_center)
# Convert the vector back to a position by adding the center: R = R_from_C + C
#rotate = (rotate_from_center[0] + center[0], rotate_from_center[1] + center[1], rotate_from_center[2] + center[2])
# find direction
travel = direction_of_travel(rotate_from_center)
return travel
def axial_to_cube(hex):
x = hex[0]
z = hex[1]
y = -x - z
return (x, y, z)
def cube_distance(a, b):
return (abs(a[0] - b[0]) + abs(a[1] - b[1]) + abs(a[2] - b[2])) / 2
def hex_distance(a, b):
ac = axial_to_cube(a)
bc = axial_to_cube(b)
return cube_distance(ac, bc)
def coordinate_conversion(old_coordinate):
new_coordinate = list(old_coordinate)
new_coordinate[0] = alphabet[new_coordinate[0]] - 1
new_coordinate[1] = int(new_coordinate[1]) - new_coordinate[0] // 2 - 1
return new_coordinate
def find_enemy(you, dir, enemy):
"""
I used very interesting article:
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
thanks to kurosawa4434
:param you:
:param dir:
:param enemy:
:return:
"""
you = coordinate_conversion(you)
enemy = coordinate_conversion(enemy)
distance = int(hex_distance(you, enemy))
direction = rotation_for_determining_the_direction_of_movement(you, dir, enemy)
return [direction, distance]
if __name__ == '__main__':
assert find_enemy('A1', 'N', 'A3') == ['B', 2], "F-2"
assert find_enemy('B2', 'N', 'B1') == ['F', 1], "F-1"
assert find_enemy('G5', 'N', 'G4') == ['F', 1], "F-1"
assert find_enemy('G5', 'N', 'J6') == ['R', 3], "SE-3"
assert find_enemy('G5', 'N', 'B7') == ['L', 5], "SW-5"
assert find_enemy('G5', 'N', 'A2') == ['L', 6], "NW-6"
assert find_enemy('A2', 'N', 'B3') == ['B', 2], "B-1"
assert find_enemy('B3', 'N', 'D2') == ['R', 2], "R-1"
assert find_enemy('B1', 'N', 'C1') == ['R', 1], "R-1"
assert find_enemy('G5', 'N', 'I4') == ['R', 2], "NE-2"
assert find_enemy('G5', 'N', 'G9') == ['B', 4], "S-4"
assert find_enemy('G3', 'NE', 'C5') == ['B', 4], "[watch your six!]"
assert find_enemy('H3', 'SW', 'E2') == ['R', 3], "right"
assert find_enemy('A4', 'S', 'M4') == ['L', 12], "true left"
print("You are good to go!")
May 9, 2021
Comments: