Skip to content

Commit

Permalink
Create KnightsTour.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewK84 authored Dec 31, 2024
1 parent 6ecafa5 commit 89201f5
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions KnightsTour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#Start By Defining Valid Move
def is_valid_move(x, y, board):
return 0 <= x < 8 and 0 <= y < 8 and board[x][y] == -1

#Get All Possible Moves
def get_possible_moves(x, y, board):
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)
]
return [(x, y) for x, y in moves if is_valid_move(x, y, board)]

def get_degree(x, y, board):
return len(get_possible_moves(x, y, board))

#Start the Sequence of Solving Knights Tour
def solve_knights_tour(start_x=0, start_y=0):
# Initialize Board with -1
board = [[-1 for _ in range(8)] for _ in range(8)]
moves = [(start_x, start_y)]
board[start_x][start_y] = 0

# Current Position
pos = 1
curr_x, curr_y = start_x, start_y

# Start Tour Completion
while pos < 64:
# All Possible Moves from Current Position
possible_moves = get_possible_moves(curr_x, curr_y, board)

if not possible_moves:
return None # No solution found

# Warnsdorff's Heuristic: Choose Move with the Minimum Number of Onward Moves
next_moves = [(x, y, get_degree(x, y, board)) for x, y in possible_moves]
next_x, next_y, _ = min(next_moves, key=lambda x: x[2])

# Next Move
board[next_x][next_y] = pos
moves.append((next_x, next_y))
curr_x, curr_y = next_x, next_y
pos += 1

return moves

#Print Output in Readable and Verifiable Format
def print_tour(moves):
board = [[-1 for _ in range(8)] for _ in range(8)]
for i, (x, y) in enumerate(moves):
board[x][y] = i

print("\nKnight's Tour Sequence:")
for row in board:
print(" ".join(f"{x:2d}" if x != -1 else "##" for x in row))

# Function Implementation
if __name__ == "__main__":
# Find Tour Starting from Position (0,0)
solution = solve_knights_tour(0, 0)

if solution:
print("Solution Found!")
print("\nMove Sequence:")
for i, (x, y) in enumerate(solution):
print(f"Move {i}: ({x}, {y})")
print_tour(solution)
else:
print("No Solution Found!")

0 comments on commit 89201f5

Please sign in to comment.