# Building a Connect4 Game in Pygame with the Amazon Q CLI

## What is Connect 4 ?

Connect Four (also known as Connect 4, Four Up, Plot Four, Find Four, Captain's Mistress, Four in a Row, Drop Four, and in the Soviet Union, Gravitrips) is a game in which the players choose a color and then take turns dropping colored tokens into a six-row, seven-column vertically suspended grid. - [Wiki](https://en.wikipedia.org/wiki/Connect_Four)

![Gameplay of Connect Four](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97aa0x4jgx11xsklr8iz.gif align="left")

Gif by [Silver Spoon](//commons.wikimedia.org/wiki/User:Silver_Spoon) - Own work, [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0), [Link](https://commons.wikimedia.org/w/index.php?curid=17635567)

---

### Hey Bhavesh, What's the motivation to build this game ?

If you’ve ever played Connect 4 (or “Four in a Row”), you know the thrill of lining up four pieces in a row—vertically, horizontally, or diagonally—to claim victory.

![Kids at my home playing Connect4 game](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0482x3trzyenmjol51n.png align="left")

Kids at my home playing Connect4 (on left - Aaradhya | on right - Mantra)

---

In this post, we’ll walk through how to recreate this classic game in Python using Pygame, step-by-step style **but without writing any code.** 😎

Did i hear, Without Writing Code??? 🤯

YESSSSS !! Without Writing Code!

If you are in, lets go. 🚀 Before going ahead we will need Amazon Q Developer CLI installed in our system.

---

### What is Amazon Q Developer CLI ?

Amazon Q Developer CLI (or Amazon Q CLI) is a command-line tool that integrates Amazon Q, a generative AI-powered assistant, into your terminal environment. It allows you to interact with your development environment and ask questions in natural language, receiving responses and even automating actions through commands.

Head over to this official page to [install](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-installing.html) on your system.

I am waiting.... Please go ahead and install. ⏳️

DONE ?

OK, if it is installed you might see following screen on your terminal when you type:

```bash
q chat
```

![Amazon Q CLI on Ubuntu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2khbkhlx9ctm7hvdzy4k.png align="left")

Now lets ask about our game plan building Connect for with Pygame.

*Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. -* [*Wiki*](https://en.wikipedia.org/wiki/Pygame)

For building game I have typed following ( with little bit of [prompt engineering](https://en.wikipedia.org/wiki/Prompt_engineering)..)

```plaintext
hi help me build a connetc4 pyhton game with pygame. 
go step by step. add option to play with bot or person. 
create a folder name "connect4-pygame" and add a specification file. 
in that add what you think will be each step to build this.
```

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umjvh49r7sn2vzfding1.png align="left")

Based on our input the Amazon Q CLI has started working... first it created the specification file... as we have asked for.

Creating specification file help maintain scope of work and avoid generating different code than what is expected. consider it like giving blueprint of doing something.

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v27b9rlf8ptz2gvnu7fn.png align="left")

Once Specification file is created, Amazon Q CLI will start working on the implementation.

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ktl6pbwtyjspoo4jfp8.png align="left")

It will also create the Readme and other files required as per the specification.

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd5iaegv2exhxj3mx943.png align="left")

Once completed you will see what it task has been completed and how to run the application.

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffi5r3u7k0jlhf2qqlkv.png align="left")

Here is the first version of the application it generated.

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/terdlpbtqwa226c5cy6v.png align="left")

![Connect4 Pygame](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9yuwquv87qj9jywzawt.png align="left")

The game runs great but, there are few bugs in first version, like:

* The difficulty levels was not selectable
    
* There was no button for going back once start playing.
    
* UI was okies and player number was not showing.
    

So lets fix this now by giving this information to Amazon Q CLI on the same terminal we were chatting.

I typed:

```plaintext
add some navigation option and dificulty selection. 
Also make ui morden looking.
```

![Amazon Q CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dyh96ovyn6gqrkokhquu.png align="left")

After few iteration... here is the final output.

## Gamplay

![Gameplay](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4cd1rjr2kxqef8oyxb3.gif align="left")

---

## Conclusion

In this article, we've seen how Amazon Q CLI can transform the way we approach game development. Without writing a single line of code ourselves, we were able to create a fully functional Connect 4 game with both single-player and multiplayer options.

The power of generative AI tools like Amazon Q CLI lies in their ability to understand our requirements and translate them into working code. With just a few prompts, we went from a concept to a playable game, and then refined it with additional features like difficulty levels and improved UI.

Give it a try yourself - you might be surprised at what you can build with just a conversation!

If you still have any queries, feel free to reach out to me on [LinkedIn](https://www.linkedin.com/in/bhaveshgohel/).

---

### Resources

* [Amazon Q Developer Documentation](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/what-is.html)
    
* [Pygame Documentation](https://www.pygame.org/docs/)
    
* [GitHub Repository for this Project](https://github.com/bhaveshgohel/connect4-pygame)
