DEV Community

Cover image for Building an Abalone Game Using Amazon Q CLI & Pygame
Tahmid Fayaz
Tahmid Fayaz

Posted on

Building an Abalone Game Using Amazon Q CLI & Pygame

πŸš€ Introduction
I’ve always been fascinated by board games that combine elegant mechanics with deep strategy, and Abalone is the perfect example. In this post, I’ll walk you through how I used the Amazon Q Developer CLI alongside Pygame to create my own digital version of Abaloneβ€”from scaffolding the project to polishing the UI.

✨Amazon Q Developer CLI

To install the Amazon Q Developer CLI on my Windows machine, I first set up the Windows Subsystem for Linux (WSL). With a Linux distribution like Ubuntu installed, I open the WSL terminal to run the installer script using the command: curl -o- https://s3.amazonaws.com/amazon-q-cli/install.sh | bash. Once the installation is complete, I just need to restart my WSL terminal. After that, I'm all set to start using the q command.

Image description

Prerequisites πŸ› οΈ

Before we dive in, make sure you have:
1.Python 3.7+ installed 🐍
2.Pygame library (pip install pygame) 🎨

Folder Structure πŸ“‚

Image description

Step 1: Initializing Pygame and Defining Constants πŸ’‘
In abalone_game.py, I start by importing modules and initializing Pygame:

Image description

Next, I define window dimensions, hexagon sizing, and colors for the board, marbles, and UI elements:

Image description

and so on

These constants make it easy to tweak the look and feel of the board later. 🎨

Step 2: Representing Hexagonal Positions πŸ”’
Abalone uses a hex grid, so I created a HexPosition class that stores cube coordinates (q, r, s) and implements equality, hashing, and a string method:

Step 3: Initializing the Board βš™οΈ
Inside the AbaloneGame class, I set up a dictionary self.board mapping every valid HexPosition (from r = -4 to 4 and appropriate q ranges) to an integer:

Image description

0 = empty βž–

1 = black marble ⚫

2 = white marble βšͺ

I then fill initial positions for both players

Step 4: Coordinate Conversion πŸ”„
To render and detect clicks, I convert between hex coordinates and pixel coordinates:

Image description

Step 5: Selection and Move Validation βœ…
Abalone rules allow selecting 1–3 marbles in a line and either sidestep or push opponent marbles. I implemented:

is_valid_selection() to ensure up to 3 marbles belong to the current player and are in line.get_valid_moves() which loops through six directions and calls either can_sidestep_to_positions() or can_push_in_direction().

This logic checks board bounds, empty target spaces, and the β€œsumito” push rules (only if you have a larger chain than your opponent’s). πŸ€œπŸ€›

Step 6: Making a Move ⏩

Once the player clicks a highlighted green circle, make_move():
Clears original marble positions βž–
Shifts marbles (or opponent chain) in the chosen direction πŸ”„
Increments score if an opponent marble is pushed off πŸ“ˆ
Checks for win (first to push off 6 marbles πŸ†)
Switches the current player πŸ”

Everything happens smoothly in one method! πŸ‘

Step 7: Drawing the Board and UI 🎨

In draw_board(), I:
Draw a large hexagon border and fill for the wooden board
Loop over all hex positions, draw holes, shadows, and marbles
Highlight selected marbles in yellow
Mark valid destinations with small green circles

In draw_ui(), I render scores, current player, and simple instructions. If the game is over, I display a β€œGame Over” banner with the winner. πŸ–₯️

Final Step: Game Loop πŸ”

The run() method handles Pygame’s event loop:

Image description

Play and Extend πŸš€
I’m thrilled with how the Abalone game turned out! You can:

Customize colors or board size by tweaking constants 🎨

Add sound effects when marbles move or are pushed off πŸ”Š

Implement AI: a simple minimax over valid moves would be a fun next step πŸ€–

Save and load game states to disk πŸ’Ύ

Grab the full source on GitHub and let me know how you’d improve it! πŸ› οΈ
https://github.com/TahmidFayaz/Abalone

Conclusion 🏁
Implementing Abalone in Pygame was a rewarding dive into hex-grid math, move validation, and UI polish. I hope this walkthrough inspires you to build your own board gamesβ€”feel free to fork the code, file issues, or submit pull requests. Happy coding! πŸ’»πŸŽ‰

Top comments (0)