Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Chess Knight by wo.tomasz
def get_one_level_moves(x,y):
all_moves = [(x+2, y+1), (x+2, y-1), (x-2, y+1), (x-2, y-1), (x+1, y+2), (x-1, y+2), (x+1, y-2), (x-1, y-2)]
posible_moves = []
for xy in all_moves:
if xy[0] >= ord('a') and xy[0] <= ord('h') and xy[1] >= 1 and xy[1] <= 8:
posible_moves.append(xy)
return posible_moves
def translate(moves_list):
translate_ml = []
for xy in moves_list:
translate_ml.append(chr(xy[0]) + str(xy[1]))
return translate_ml
def chess_knight(start, moves):
moves_list = [(ord(start[0]), int(start[1]))]
while moves > 0:
tem_list = []
for pos in moves_list:
tem_list.extend(get_one_level_moves(pos[0], pos[1]))
moves_list.extend(tem_list)
moves -= 1
moves_list.pop(0)
moves_list = list(dict.fromkeys(moves_list))
moves_list.sort(key=lambda xy: (xy[0], xy[1]))
return translate(moves_list)
if __name__ == '__main__':
print("Example:")
print(chess_knight('a1', 1))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert chess_knight('a1', 1) == ['b3', 'c2']
assert chess_knight('h8', 2) == ['d6', 'd8', 'e5', 'e7', 'f4', 'f7', 'f8', 'g5', 'g6', 'h4', 'h6', 'h8']
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 12, 2020