"""Basic example: play a random game of Blokus."""
import numpy as np

from blokus_gym import BlokusEnv


def main():
    # Create environment with 4 players
    env = BlokusEnv(num_players=4, board_size=20, render_mode="ansi")

    obs, info = env.reset(seed=42)
    print("=== Initial State ===")
    print(env.render())

    total_reward = 0
    steps = 0
    terminated = False
    truncated = False

    while not (terminated or truncated):
        # Select a random valid action
        valid_actions = np.where(info["action_mask"])[0]
        if len(valid_actions) == 0:
            print("No valid moves!")
            break

        action = np.random.choice(valid_actions)
        obs, reward, terminated, truncated, info = env.step(action)
        total_reward += reward
        steps += 1

        if steps % 10 == 0:
            print(f"\n=== Step {steps} ===")
            print(env.render())
            print(f"Reward: {reward}, Total: {total_reward}")

    print("\n=== Game Over ===")
    print(f"Total steps: {steps}")
    print(f"Total reward: {total_reward}")
    print(f"Terminated: {terminated}, Truncated: {truncated}")

    # Final state
    print("\n=== Final Board ===")
    print(env.render())

    env.close()


if __name__ == "__main__":
    main()
