Untitled
unknown
plain_text
2 years ago
2.3 kB
6
Indexable
import random def create_chessboard(position1, position2, movements): # Check if positions are valid if not is_valid_position(position1) or not is_valid_position(position2): raise ValueError("Invalid positions. Each position should be a tuple (x, y) where 0 <= x, y <= 7.") # Create an empty 8x8 chessboard chessboard = [['_ ' for _ in range(8)] for _ in range(8)] # Mark the destination position chessboard[position2[1]][position2[0]] = 'E ' # Initialize position with starting coordinates x, y = position1 # Mark the starting position chessboard[y][x] = 'S ' # Perform the movements and mark the path with the number of moves move_count = 1 for move in movements: if move == "u": y = (y-1) % 8 elif move == "d": y = (y+1) % 8 elif move == "l": x = (x-1) % 8 elif move == "r": x = (x+1) % 8 if move_count < 10: chessboard[y][x] = str(move_count) + " " elif move_count >= 10: chessboard[y][x] = str(move_count) move_count += 1 return chessboard def is_valid_position(position): x, y = position return 0 <= x <= 7 and 0 <= y <= 7 def display_chessboard(chessboard): print(" x 0 1 2 3 4 5 6 7 ") print(" y +---+---+---+---+---+---+---+---+") for i, row in enumerate(chessboard): row_str = f"{i:2d} | {'| '.join(row)}|" print(row_str) print(" +---+---+---+---+---+---+---+---+") if __name__ == "__main__": random.seed(10) try: pos1_x = int(input("Enter the x-coordinate for the starting position (0-7): ")) pos1_y = int(input("Enter the y-coordinate for the starting position (0-7): ")) pos2_x = int(input("Enter the x-coordinate for the destination position (0-7): ")) pos2_y = int(input("Enter the y-coordinate for the destination position (0-7): ")) movements_str = input("Enter the movement directions (u, d, l, r): ") position1 = (pos1_x, pos1_y) position2 = (pos2_x, pos2_y) movements = movements_str.split() chessboard = create_chessboard(position1, position2, movements) display_chessboard(chessboard) except ValueError as e: print(f"Error: {e}")
Editor is loading...