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)