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)