Skip to main content

🗼 Towers of Hanoi

PyPI - Version PyPI - License CI status PyPI - Python Version

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!

Release files for hanoi-viz 0.3.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hanoi-viz 0.3.8
File Size Uploaded
hanoi_viz-0.3.8.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hanoi-viz 0.3.8
File Interpreter ABI Platform
hanoi_viz-0.3.8-py3-none-any.whl Python 3 none any Details

Total release size: 24.4 kB

Release files / hanoi_viz-0.3.8.tar.gz

Download URL hanoi_viz-0.3.8.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0ad7e4745bd2e5dc45df98deb88bb1e27a9033d4a0d67768d3ff6bae051c896e
BLAKE2b-256 checksum
How to use checksums
93f5b459b150abde8e6f9a318cde048c7ede3f519fa5a6f13c691415638c9216
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / hanoi_viz-0.3.8-py3-none-any.whl

Download URL hanoi_viz-0.3.8-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97d832c5a6598b821b429e8cf7e0229f065c018a23f44e25dc083769e4c048c2
BLAKE2b-256 checksum
How to use checksums
14328e81b5f2aebc9dae293cfbf943e3353898cb440e36e8f736fce374e519ff
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.3.8 This release

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page