Skip to main content

Visualize solving the tower of Hanoi problem

Project description

🗼 Towers of Hanoi

A small Python project that visualizes the Towers of Hanoi puzzle using pygame for animation and rich for colorful CLI output.

Towers of Hanoi is a fun game consisting of three pegs on a board with a stack of different sized discs. The game starts with the discs stacked from largest to smallest on the first peg. The objective is to move the entire stack from the first peg to the third peg.

The rules are simple:

  1. You can only move a single disk at a time.
  2. You can only move the top disc of a stack to any other stack or empty peg.
  3. You can't place a larger disc on top of a smaller one.

Hanoi Animation

The game quickly increases in difficulty as the number of discs used to play grows. In fact, the game can be played with an arbitrary amount of discs.


✨ Features

  • Smooth animated visualization of the Towers of Hanoi algorithm
  • Generator-based Hanoi solver (clean separation of logic vs rendering)
  • Colorful, styled CLI output using rich
  • Adjustable number of disks and animation speed
  • Clean shutdown handling (window close, Ctrl+C)

📦 Requirements

  • Python 3.8+
  • uv
  • pygame
  • rich

All dependencies are managed via uv.


🚀 Getting Started

1️⃣ Clone the repository

git clone https://github.com/eytanohana/Hanoi.git
cd Hanoi

2️⃣ Run the game

uv run hanoi-viz

🎮 Usage

uv run hanoi-viz [n_disks] [--speed SPEED]

Arguments

uv run hanoi-viz --help
Argument Description Default
n_disks Number of disks (1–15) 3
--speed Pixels moved per frame (animation speed, >= 10) 15

Examples

uv run hanoi-viz
uv run hanoi-viz 5
uv run hanoi-viz 6 --speed 25

🧠 How It Works

  • The Hanoi solver is implemented as a Python generator (hanoi()).
  • Each yielded move (disc, from_peg, to_peg) is animated in pygame.
  • Disks are lifted, slid horizontally, and dropped with smooth per-frame motion.
  • The game loop remains responsive at all times.

🛑 Exiting the Game

  • Close the pygame window, or
  • Press Ctrl+C in the terminal

Both exit paths are handled cleanly.


🛠 Development Notes

  • The project intentionally avoids over-packaging --- it's a focused script-based app.
  • Rich is used for readable CLI output without interfering with pygame.
  • The code is structured to be easy to refactor into a package if desired.

📜 License

MIT License --- feel free to use, modify, and learn from it.


🙌 Credits

Built with: - 🐍 Python - 🎮 pygame - 🌈 rich - ⚡ uv

Enjoy watching recursion come to life!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hanoi_viz-0.3.6.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hanoi_viz-0.3.6-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file hanoi_viz-0.3.6.tar.gz.

File metadata

  • Download URL: hanoi_viz-0.3.6.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hanoi_viz-0.3.6.tar.gz
Algorithm Hash digest
SHA256 791335a62097d8bf26f639d2bc7589d486fed63b7f4c871c74c1c8d7f1ad9301
MD5 d799cdbb8ca41d13e706a79038cf3aa9
BLAKE2b-256 3009ea95373bbc6f3842afcfd62d62247323f637d1df0c22242eed600dffe7db

See more details on using hashes here.

File details

Details for the file hanoi_viz-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: hanoi_viz-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for hanoi_viz-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 53504fa9c7032f874c12a4c9d645f7a90929dddac57bd0ea211a5a0c0ce2d5a5
MD5 7fde154871478b88b5d21ad72670f8cf
BLAKE2b-256 0768f71a8e7268a641aceea544c659b70625863528253c242c003fe8f49fbbdb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page