Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First: walk around the edges solution in Clear category for Square Board by leggewie
# my first and programmatically certainly not the most elegant solution
# simply algorithmatically recreating the steps around the game board
# another solution is to create a dict translating position to coordinates
# pos2xy then becomes a simple lookup
# yet another possible angle at solving this is that opposite squares' position
# always add up to the same number
## initial code, not used
#from typing import Tuple
#Coordinate = Tuple[int, int]
# the easy part
def square_board(side: int, token: int, steps: int):
newposition = (token + steps + ((side - 1) * 4)) % ((side - 1) * 4)
return pos2xy(side, newposition)
# the hard part (for which I'm sure there is a more elegant solution -> refactor later)
def pos2xy(side: int, pos: int):
edge = side - 1 # how many steps per direction
turns = int(pos/(edge)) # how many turns to reach the new position when starting at origin
corners = [ [edge, edge], [edge, 0], [0, 0], [0, edge] ] # array of corner coords
direction = [ [0, -1], [-1, 0], [0, 1], [1, 0] ] # array of directions after each turn
turns = int(pos/edge)
last_corner = corners[turns]
last_move = [(pos % edge) * i for i in direction[turns]]
return tuple(x + y for x, y in zip(last_corner, last_move))
May 23, 2021