Untitled
unknown
plain_text
2 years ago
2.7 kB
7
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) % 80
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(" +---+---+---+---+---+---+---+---+")
def generate_random_string(length=16):
characters = 'udlrn'
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
if __name__ == "__main__":
random.seed(10)
try:
pos1_x = random.randrange(8)#int(input("Enter the x-coordinate for the starting position (0-7): "))
pos1_y = random.randrange(8)#int(input("Enter the y-coordinate for the starting position (0-7): "))
pos2_x = random.randrange(8)#int(input("Enter the x-coordinate for the destination position (0-7): "))
pos2_y = random.randrange(8)#int(input("Enter the y-coordinate for the destination position (0-7): "))
position1 = (pos1_x, pos1_y)
position2 = (pos2_x, pos2_y)
chessboard_rand = create_chessboard(position1, position2, "")
display_chessboard(chessboard_rand)
movements_str = generate_random_string()#input("Enter the movement directions (u, d, l, r, n): ")
print(movements_str)
chessboard = create_chessboard(position1, position2, movements_str)
display_chessboard(chessboard)
except ValueError as e:
print(f"Error: {e}")
Editor is loading...