def main(): """Executes the main application. Keyword arguments: <None> """ # Instantiate the x & y position variables. x = 9 y = 9 # Take x & y initial inputs. Loop while invalid. while (x < 1 or x > BOARD_SIZE) or (y < 1 or y > BOARD_SIZE): x, y = input("Enter x & y (1-" + str(BOARD_SIZE) + ") separated by a space (e.g. 4 4): ").split() x, y = [int(x), int(y)] if x < 1 or x > BOARD_SIZE: print("ERROR:: x must be 1-" + str(BOARD_SIZE)) if y < 1 or y > BOARD_SIZE: print("ERROR:: y must be 1-" + str(BOARD_SIZE)) print() # Create a Knight object to move around the board. knight = Knight(x, y) # Instantiate a board and set the initial Knight position with move 1. board = Board(BOARD_SIZE) board.place_knight(1, knight.x, knight.y) # Test special cases for all the moves. for current_move in range(2, MAX_MOVES + 1): num_possibilities, next_x, next_y = get_num_possibilities(knight, board) min_exits_idx = 0 # If there are no possibilities left, then end the tour prematurely. if num_possibilities == 0: print("The knight's tour ended prematurely at (" + str(knight.x + 1) + "," + str(knight.y + 1) + ") during move #" + str(current_move - 1) + ".") print() break # If there is more than 1 possibility, then find the next squares with the # minimum number of exits. elif num_possibilities > 1: exits = find_min_exits(board, num_possibilities, next_x, next_y) min_exits_idx = get_idx_smallest_num_exits(num_possibilities, exits) # Move the knight and mark its position on the board. knight.move(next_x[min_exits_idx], next_y[min_exits_idx]) board.place_knight(current_move, knight.x, knight.y) # Print out the board. board.print_board()
def main(): """Executes the main application.""" # Instantiate the x & y co-ordinates. Note that 9 and 9 are 'off the board' and therefore invalid. x = 9 y = 9 # Take x & y User input. For error tolerance, Loop until we receive a valid input (i.e. cannot start the Knight 'off' the board). while (x < 1 or x > BOARD_SIZE) or (y < 1 or y > BOARD_SIZE): x, y = input( "Enter the Knight's starting position as X & Y co-ordinates (1-" + str(BOARD_SIZE) + ") separated by a space (e.g. 4 4): ").split() x, y = [int(x), int(y)] if x < 1 or x > BOARD_SIZE: print("ERROR::Invalid input: x must be 1-" + str(BOARD_SIZE)) if y < 1 or y > BOARD_SIZE: print("ERROR::Invalid input: y must be 1-" + str(BOARD_SIZE)) print() # Create a Knight object at the given X,Y input. knight = Knight(x, y) # Instantiate the board. The Knight's initial position (input) is given with move 1. board = Board(BOARD_SIZE) board.place_knight(1, knight.x, knight.y) # Test for all valid possible moves and special cases. for current_move in range(2, MAX_MOVES + 1): num_possibilities, next_x, next_y = get_num_possibilities( knight, board) min_exits_idx = 0 #index of minimum number of exits # If there are no possibilities left, then end the tour prematurely. Special case that doesn't come up often. if num_possibilities == 0: print("The knight's tour ended prematurely at (" + str(knight.x + 1) + "," + str(knight.y + 1) + ") during move #" + str(current_move - 1) + ".") print() break # If there's more than 1 move possible, find next tile with the # fewest number of exits. This is the core of Warndorff's Rule. elif num_possibilities > 1: exits = find_min_exits(board, num_possibilities, next_x, next_y) min_exits_idx = get_idx_smallest_num_exits(num_possibilities, exits) # Move the knight, marking its location on the board. knight.move(next_x[min_exits_idx], next_y[min_exits_idx]) board.place_knight(current_move, knight.x, knight.y) # Print the board to the console. The board is represented as a move map. board.print_board()