from turtle import *
import random

# 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)

# 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
    # 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)

    # Determine the next state
    current_state = get_next_state(current_state)

done()
