from turtle import *
import random
import time  # Import the time module for delays

# Setup turtle screen
bgcolor('black')

# Define Markov chain for movement
states = ['left', 'right']
transition_prob = {
    'left': {'left': 0.5, 'right': 0.5},
    'right': {'left': 0.5, 'right': 0.5}
}

# Initialize starting state
current_state = random.choice(states)

# Function to determine next state
def get_next_state(current_state):
    probabilities = transition_prob[current_state]
    next_state = random.choices(list(probabilities.keys()), weights=probabilities.values())[0]
    return next_state

# Create turtle
t = Turtle()
t.speed(1)  # Set a reasonable drawing speed

# Define text turtle
text_turtle = Turtle()
text_turtle.hideturtle()
text_turtle.penup()
text_turtle.color("white")

# List of configurations for the zigzag movement
zigzag_configurations = [
    ('green', 2, 50),    # Right diagonal (north-east)
    ('red', 2, 50),      # Left diagonal (north-west)
    ('blue', 2, 70),     # Right diagonal (north-east)
    ('yellow', 2, 70),   # Left diagonal (north-west)
    ('orange', 2, 90),   # Right diagonal (north-east)
    ('purple', 2, 90)    # Left diagonal (north-west)
]

# Simulate sailing upwind with Markov chain
for _ in range(50):  # Number of zigzag steps
    # Display the current direction
    text_turtle.clear()
    text_turtle.goto(0, 200)  # Position text at the top
    text_turtle.write(f"Moving {current_state}", align="center", font=("Arial", 16, "normal"))
    
    # Get configuration based on the current state
    if current_state == 'left':
        pen_color = random.choice([cfg[0] for cfg in zigzag_configurations])
        pensize_value = random.choice([cfg[1] for cfg in zigzag_configurations])
        forward_distance = random.choice([cfg[2] for cfg in zigzag_configurations])
        t.pensize(pensize_value)
        t.color(pen_color)
        t.forward(forward_distance)
        t.right(45)  # Move diagonally to the right (north-east)
    else:
        pen_color = random.choice([cfg[0] for cfg in zigzag_configurations])
        pensize_value = random.choice([cfg[1] for cfg in zigzag_configurations])
        forward_distance = random.choice([cfg[2] for cfg in zigzag_configurations])
        t.pensize(pensize_value)
        t.color(pen_color)
        t.forward(forward_distance)
        t.right(-45)  # Move diagonally to the left (north-west)

    # Delay for 0.5 seconds
    time.sleep(0.5)

    # Determine the next state
    current_state = get_next_state(current_state)

done()
