DEV Community

Cover image for ๐ŸŽฎ Building a Mini Tic-Tac-Toe Game Using Amazon Q Developer and Python
Naami Ahmed
Naami Ahmed

Posted on

๐ŸŽฎ Building a Mini Tic-Tac-Toe Game Using Amazon Q Developer and Python

Introduction

Artificial Intelligence (AI) is becoming more accessible for developers to integrate into their projects. Amazon Q Developer is a powerful AI coding assistant from AWS that helps developers generate, debug, and improve code directly inside their IDE.

In this article, Iโ€™ll walk you through a practical example: creating a mini Tic-Tac-Toe game in Python using Amazon Q Developer. This will give you hands-on experience with AI-assisted coding while exploring how AWSโ€™s developer tools work.

๐Ÿง  What is Amazon Q Developer?

Amazon Q Developer is an AI-powered coding assistant integrated into IDEs like VS Code.
It helps developers by:

  • Generating or explaining code.
  • Suggesting fixes or optimizations.
  • Improving documentation and refactoring logic.

Itโ€™s part of AWSโ€™s effort to make development more intelligent and cloud-ready.
You can also use Amazon Q Business (for enterprise workflows) or Q Developer (for coding).

Free Usage: The Developer (Free) tier gives you full AI assistance inside VS Code without any extra cost โ€” perfect for personal learning or experiments.

โš™๏ธ Step 1: Environment Setup

  1. Install Python 3.10+ on your computer.
  2. Install VS Code.
  3. Add the Amazon Q Developer extension in VS Code.
  4. Sign in using your AWS Builder ID.
  5. Create a project folder:
TicTacToe-Q
Enter fullscreen mode Exit fullscreen mode

Create a file named:

main.py
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ป Step 2: Writing a Simple Tic-Tac-Toe Game

Start with a simple console-based Python version (If you want, you can start the code generation using Amazon Q):

import tkinter as tk
import random

# --- Basic Setup ---
root = tk.Tk()
root.title("Tic Tac Toe - Amazon Q AI")
root.configure(bg='#2c3e50')
root.geometry('400x500')

# Game variables
board = [""] * 9
player = "X"
game_active = True

# --- AI Functions ---
def ai_move():
    # Try to win
    for i in range(9):
        if board[i] == "":
            board[i] = "O"
            if check_winner() == "O":
                return i
            board[i] = ""

    # Block player from winning
    for i in range(9):
        if board[i] == "":
            board[i] = "X"
            if check_winner() == "X":
                board[i] = ""
                return i
            board[i] = ""

    # Take center if available
    if board[4] == "":
        return 4

    # Take corners
    corners = [0, 2, 6, 8]
    available_corners = [i for i in corners if board[i] == ""]
    if available_corners:
        return random.choice(available_corners)

    # Take any available spot
    available = [i for i in range(9) if board[i] == ""]
    return random.choice(available) if available else None

# --- Functions ---
def check_winner():
    wins = [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)]
    for a,b,c in wins:
        if board[a] == board[b] == board[c] != "":
            return board[a]
    if "" not in board:
        return "Draw"
    return None

def show_winner_overlay(winner):
    if winner == "Draw":
        winner_overlay.config(text="๐Ÿค It's a Draw!", fg='#95a5a6')
    else:
        symbol = "๐ŸŽ‰" if winner == "X" else "๐Ÿค–"
        player_name = "You" if winner == "X" else "AI"
        color = '#e74c3c' if winner == 'X' else '#f39c12'
        winner_overlay.config(text=f"{symbol} {player_name} ({winner}) Wins!", fg=color)
    winner_overlay.place(relx=0.5, rely=0.4, anchor='center')
    restart_overlay_btn.place(relx=0.5, rely=0.55, anchor='center')
    reset_button.grid_remove()

def reset_game():
    global board, player, game_active
    board = [""] * 9
    player = "X"
    game_active = True
    winner_overlay.place_forget()
    restart_overlay_btn.place_forget()
    reset_button.grid(row=5, column=0, columnspan=3, pady=15)
    for btn in buttons:
        btn.config(text="", bg='#34495e', fg='white')
    status_label.config(text="Your Turn (X)", fg='#3498db')

def update_button_color(btn, symbol):
    if symbol == "X":
        btn.config(fg='#e74c3c', bg='#ecf0f1')
    else:
        btn.config(fg='#f39c12', bg='#ecf0f1')

def click_button(i):
    global player, game_active
    if board[i] == "" and game_active and player == "X":
        board[i] = player
        buttons[i].config(text=player)
        update_button_color(buttons[i], player)

        winner = check_winner()
        if winner:
            game_active = False
            show_winner_overlay(winner)
            return

        # AI turn
        player = "O"
        status_label.config(text="AI is thinking...", fg='#f39c12')
        root.after(500, ai_turn)

def ai_turn():
    global player, game_active
    if game_active:
        move = ai_move()
        if move is not None:
            board[move] = "O"
            buttons[move].config(text="O")
            update_button_color(buttons[move], "O")

            winner = check_winner()
            if winner:
                game_active = False
                show_winner_overlay(winner)
                return

            player = "X"
            status_label.config(text="Your Turn (X)", fg='#3498db')

# --- Create UI ---
title_label = tk.Label(root, text="๐ŸŽฎ Tic Tac Toe AI", font=("Arial", 18, "bold"), 
                      bg='#2c3e50', fg='#ecf0f1')
title_label.grid(row=0, column=0, columnspan=3, pady=10)

buttons = []
for i in range(9):
    btn = tk.Button(root, text="", width=6, height=3, font=("Arial", 24, "bold"),
                    bg='#34495e', fg='white', relief='raised', bd=3,
                    command=lambda i=i: click_button(i))
    btn.grid(row=i//3+1, column=i%3, padx=5, pady=5)
    buttons.append(btn)

status_label = tk.Label(root, text="Your Turn (X)", font=("Arial", 16), 
                       bg='#2c3e50', fg='#3498db')
status_label.grid(row=4, column=0, columnspan=3, pady=10)

reset_button = tk.Button(root, text="๐Ÿ”„ New Game", font=("Arial", 12, "bold"),
                        bg='#27ae60', fg='white', relief='raised', bd=2,
                        command=reset_game)
reset_button.grid(row=5, column=0, columnspan=3, pady=15)

# Winner overlay (hidden initially)
winner_overlay = tk.Label(root, text="", font=("Arial", 20, "bold"),
                         bg='#2c3e50', relief='solid', bd=2, padx=20, pady=10)

# Restart button for winner overlay (hidden initially)
restart_overlay_btn = tk.Button(root, text="๐Ÿ”„ Play Again", font=("Arial", 12, "bold"),
                               bg='#e67e22', fg='white', relief='raised', bd=2,
                               command=reset_game)

root.mainloop()


Enter fullscreen mode Exit fullscreen mode

Run this code (click Run โ–ถ๏ธ at the top or press F5) once to make sure it works fine.

๐Ÿ’ป Step 4: Installing Amazon Q in VS Code

Before starting your project, you need the Amazon Q Developer extension in VS Code:

  1. Open VS Code.
  2. Go to the Extensions panel (click the square icon on the left sidebar or press Ctrl + Shift + X). 3.In the search bar, type:
Amazon Q Developer
Enter fullscreen mode Exit fullscreen mode
  1. Click Install on the extension.
  2. After installation, sign in using your AWS Builder ID (or create one if you donโ€™t have it). 6.Youโ€™ll now see the Amazon Q panel on the side of VS Code. This is where you can:
  • Chat with Amazon Q.
  • Generate or improve code.
  • Ask for explanations of your Python scripts.

โœ… Now your VS Code is ready to use Amazon Q for interactive AI coding.

๐Ÿค– Step 4: Using Amazon Q to Improve the Game

Now that your base version works, letโ€™s make it more advanced using Amazon Q.

  1. Inside VS Code:
    Open Amazon Q Chat (Ctrl + Shift + P โ†’ Amazon Q: Chat).

  2. Ask questions like:

  • โ€œConvert this Tic-Tac-Toe game to GUI using Tkinter.โ€
  • โ€œAdd AI logic so the computer can play automatically.โ€
  • โ€œRefactor this into object-oriented Python code.โ€

Amazon Q will generate improved snippets or even a full new version of your code.
You can directly replace or merge suggestions.

๐Ÿš€ Conclusion

Amazon Q Developer is a promising tool from AWS that brings AI into your coding workflow.
While it may seem a little complex to set up, it offers great educational value โ€” especially for those who plan to grow their skills in the AWS ecosystem.

Starting with a small project like Tic-Tac-Toe is a fun and practical way to understand how AI-assisted coding can help you improve your logic, refactor code, and experiment confidently.

Keep exploring โ€” and maybe your next project could integrate AWS Lambda or DynamoDB with your game! โ˜๏ธ๐ŸŽฎ

Thank You for Reading ๐Ÿ™Œ๐Ÿ™Œ๐Ÿ™Œ

Top comments (0)