Metadata-Version: 2.4
Name: kraken-crossing
Version: 0.1.0
Summary: A dark, stochastic Gymnasium environment for learning tabular Q-learning.
Author: Alex Veerasammy
License-Expression: MIT
Project-URL: Homepage, https://github.com/4l3xv33/kraken-crossing
Project-URL: Repository, https://github.com/4l3xv33/kraken-crossing
Project-URL: Issues, https://github.com/4l3xv33/kraken-crossing/issues
Keywords: reinforcement-learning,gymnasium,q-learning,gridworld
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Education
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: gymnasium[toy-text]<2,>=1.3
Requires-Dist: numpy>=1.26
Requires-Dist: pillow>=10
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == "dev"
Requires-Dist: jupyterlab>=4; extra == "dev"
Requires-Dist: matplotlib>=3.8; extra == "dev"
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: twine>=6; extra == "dev"
Dynamic: license-file

# Kraken Crossing

`Kraken Crossing` is a small, original environment built with the Gymnasium API for learning tabular Q-learning. At night, a pirate ship must cross unpredictable water, avoid six hidden whirlpools, defeat a Kraken when necessary, and find a hidden treasure chest.

![A trained Kraken Crossing episode shown from the AI and omniscient perspectives](docs/kraken-crossing-demo.gif)

The darkness is part of the environment—not just decoration. The ship cannot see what occupies adjacent spaces. Its observation contains only its own position, heading, and whether the Kraken has been defeated. During rendering, only the ship's current tile is visible; a cannon flash briefly illuminates the line of fire.

## Rules

- 8×8 map with one start, six whirlpools, one Kraken, and one treasure chest
- 1,024 observations: 64 positions × 2 Kraken conditions × 8 headings
- 10 actions: eight compass movement attempts, Fire Port, and Fire Starboard
- Each movement resolves with a 20% chance in each of five directions: the attempted direction and the two directions on either side
- The ship faces the direction it actually moves, rather than the direction it attempted
- Port and starboard cannons fire 90° left or right of the ship's heading
- A cannonball is a guaranteed hit when the living Kraken lies anywhere along the chosen row, column, or diagonal ray; otherwise it misses
- Entering a whirlpool ends the episode with a penalty; reaching the treasure ends it with a reward
- Colliding with the living Kraken incurs a penalty but does not end the episode

The environment remains fully tabular. No image pixels are passed to the Q-learning algorithm; the graphics visualize the underlying discrete state and transitions.

### Map legend

| Symbol | Meaning |
| --- | --- |
| `S` | Starting water |
| `.` | Open water |
| `W` | Hidden whirlpool |
| `K` | Kraken |
| `T` | Hidden treasure |

## Rewards

| Event | Reward |
| --- | ---: |
| Find treasure | `+10.00` |
| Enter whirlpool | `-10.00` |
| Collide with Kraken | `-5.00` |
| Ordinary move | `-0.01` |
| Defeat Kraken | `-0.01` |
| Miss with cannon | `-0.25` |

## Setup

```powershell
conda env create -f environment.yml
conda activate kraken-crossing-env
python -m ipykernel install --user --name kraken-crossing-env --display-name "Kraken Crossing"
```

Launch JupyterLab and open [`notebooks/kraken_crossing_q_learning.ipynb`](notebooks/kraken_crossing_q_learning.ipynb). The notebook explains the state and action spaces, trains a Q-table, and uses `IPython.display.display()` to play every movement, cannonball, impact, miss, sinking, and victory frame inline.

## Python API

```python
import gymnasium as gym
import kraken_crossing

env = gym.make("KrakenCrossing-v0", render_mode="rgb_array")
state, info = env.reset(seed=7)
state, reward, terminated, truncated, info = env.step(kraken_crossing.Action.EAST)

# All frames belonging to that one transition:
frames = env.unwrapped.render_transition_frames()
```

Use `render_view="agent"` (the default) for the intended dark experience, or `render_view="omniscient"` for debugging and teaching.

The notebook displays both views simultaneously. The left panel shows only what the AI can see; the right panel reveals the full map to the human observer. Both panels animate the same underlying transition.

## Training configuration

The included training helper defaults to:

- Learning rate (`alpha`): `0.10`
- Discount factor (`gamma`): `0.99`
- Exploration (`epsilon`): decays from `1.00` toward `0.05`
- Epsilon schedule: `0.05 + 0.95 * exp(-0.00005 * episode)`
- Training seed: `7`

The checked-in notebook uses 50,000 training episodes and evaluates the learned table over 2,000 separately seeded episodes. Exact routes can differ because movement remains stochastic.

Train and evaluate from the command line:

```powershell
python examples\train_q_learning.py --episodes 100000
```

Watch an animated Pygame episode:

```powershell
python examples\watch_random_agent.py
```

## Artwork and animation

The editable transparent artwork lives in `kraken_crossing/assets/base/`, and the packaged animation sheets are in `kraken_crossing/assets/runtime/`. The larger chroma-key generation files are intentionally excluded from the repository. Run this after changing the base artwork:

```powershell
python tools\build_spritesheets.py
```

The build script creates deterministic idle, vortex, glow, and defeat animations. Ship rotation, broadside flashes, cannonballs, misses, movement, and sinking are composed by the renderer so all eight headings remain precise.

See [`ASSETS.md`](ASSETS.md) for artwork provenance and [`LICENSE`](LICENSE) for reuse terms.
