CS 212, AI Programming 1

Building an AI Game Opponent

Using the Dots and Boxes Game Example

Contents

[TOC]

Review: Calling a Chat Completion API

From Calling a Chat-Completion API:

In the example below, the key is in a file named .env in the project folder:

OPENROUTER_API_KEY=your-key-here

Add .env to your .gitignore file so the key never gets committed. Install the packages with pip install openai python-dotenv.

from dotenv import dotenv_values
from openai import OpenAI

# Read the settings in .env into a dictionary.
settings = dotenv_values(".env")

# Point the OpenAI client at OpenRouter instead of OpenAI.
client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key=settings["OPENROUTER_API_KEY"],
)

# Send the model a system message (rules) and a user message (the question).
response = client.chat.completions.create(
    model="baidu/ernie-5.5-flash-preview:free",
    messages=[
        {"role": "system", "content": "You are a helpful tutor. Answer in two sentences."},
        {"role": "user", "content": "What is a token?"},
    ],
)

# The model's reply is the content of the first choice.
print(response.choices[0].message.content)

Introduction

An LLM will serve as the computer opponent in Dots and Boxes. Your program still runs the game: it keeps track of the board, enforces the rules, and keeps score. On the AI’s turn, it sends the board state to the LLM through the OpenRouter API and gets a move back.

Unlike symbolic game AI (GOFAI)1, the LLM doesn’t use a search algorithm to look at all possible moves. It reads the board description and suggests a move, which may be a poor choice or even illegal, so your program must validate every move.

Board used in these notes: 3x3 dots (2x2 boxes), rows and columns numbered 0–2.

The Dots and Boxes Game

Dots and Boxes is a two-player pencil-and-paper game played on a grid of dots. Players take turns drawing a line between two neighboring dots. A player who draws the fourth side of a box claims that box, scores a point, and takes another turn. When all the boxes are claimed, the player with the most boxes wins.

Example code, in the CS212-DotsAndBoxesGame repository on GitHub:

High-Level Implementation Steps

  1. Define Game State Representation: Establish a clear, unambiguous text format for the current state of the board.
  2. Create a System Instruction (Persona): Prompt the model (any model available through OpenRouter) to adopt the persona of a highly strategic “Dots and Boxes” player and define its output format.
  3. Send the Request: On the AI player’s turn, send the current game state to the OpenRouter API (the chat completions endpoint, https://openrouter.ai/api/v1/chat/completions).
  4. Parse and Validate the Move: Your application must receive the model’s text output and parse it into a valid move (e.g., coordinates), then update the game board.

Prompt Templates for Dots and Boxes

You should use a multi-part prompt that includes a System Instruction (static for the entire game) and a User Prompt (dynamic for each turn).

1. System Instruction (Static Setup)

This instruction is sent once per API call, as the message with "role": "system", to define the model’s persona, the rules, and the strict output format. This is the most critical part.

System Prompt:

You are an expert, highly strategic AI player of the game "Dots and Boxes" on a 3x3 grid of dots (2x2 boxes). Your sole objective is to win by maximizing closed boxes, focusing on forced moves and chain strategies.
You will be provided the current game state as a JSON object.
You MUST analyze the input JSON and respond with ONLY a single, valid, and legal move in the required JSON response format. Do NOT include any explanations, commentary, or extra text outside the required JSON.

2. User Prompt (Dynamic Input)

This is the JSON data structure your application sends to the OpenRouter API (as the content of the "role": "user" message) on the AI’s turn, detailing the current state of the game.

Example JSON Input for Board State

The // comments below are only there to explain each part. Real JSON can’t contain comments, so leave them out of the JSON your program sends.

{
  "turn": "AI_Player",
  "board_dimensions": "3x3 dots",
  "occupied_lines": [
    // Format: [r1, c1, r2, c2] where the line connects the two coordinates
    // Example: Line from (0, 0) to (0, 1) is taken
    [0, 0, 0, 1],
    // Example: Line from (0, 0) to (1, 0) is taken
    [0, 0, 1, 0],
    // Example: Line from (1, 1) to (2, 1) is taken
    [1, 1, 2, 1]
    // ... list of ALL taken lines ...
  ],
  "closed_boxes": [
    // Format: {"r": r, "c": c, "owner": owner} where (r, c) is the top-left dot of the box
    // Example: Box at (0, 0) is closed and owned by the Human
    {"r": 0, "c": 0, "owner": "Human"},
    // Example: Box at (1, 1) is not yet closed (owner: "None")
    {"r": 1, "c": 1, "owner": "None"}
    // ... list of ALL box states (optional, can be derived from lines) ...
  ],
  "move_request": "Provide your optimal move as a JSON object."
}

3. Required JSON Response Format (Output)

In your API call, you would set response_format to {"type": "json_schema", ...} and provide a schema defining this structure. (Not every model on OpenRouter supports structured outputs, so choose one that does.)

Required JSON Output from the Model

{
  "move": [1, 2, 2, 2]
}

Requiring this JSON output format keeps the model from adding conversational text, so your program always gets a reply it can parse. It doesn’t guarantee a legal move, though. The model might still choose a line that’s already taken or one that isn’t between two neighboring dots, so your program must check the move and ask again (or pick a fallback move) if it’s invalid.

The Example Code: DotsAndBoxesWithAI

The DotsAndBoxesWithAI example follows the four steps above, with a simpler output format: the model replies with plain text instead of JSON.

Setup

  1. Install the packages: pip install -r requirements.txt (installs openai and python-dotenv).
  2. Copy .env.example to .env in the project folder and put your key in it:
    OPENROUTER_API_KEY=your-key-here
    

    The project’s .gitignore already lists .env.

  3. Run python main.py and type AI when asked for a player’s type.

Where the AI Code Is

File What it does
llm_client.py The ask_llm function. It sends a system prompt and a user prompt to a model on OpenRouter and returns the reply.
ai_player.py The AIPlayer class and helper functions. They describe the board in a prompt, read the move out of the model’s reply, and check that the move is legal.
test_ai_player.py A test driver. Part 1 checks the prompt and reply code without calling the model. Part 2 asks the model for a real move.

Calling the Model

llm_client.py is the same pattern as the review example at the top of these notes:

settings = dotenv_values(Path(__file__).with_name(".env"))  # the .env next to this file

def ask_llm(system_prompt, user_prompt):
    api_key = settings.get("OPENROUTER_API_KEY")
    if not api_key:
        raise ValueError("No OPENROUTER_API_KEY found in the .env file")

    client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=api_key)
    response = client.chat.completions.create(
        model=MODEL,                        # "baidu/ernie-5.5-flash-preview:free"
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ],
    )
    return response.choices[0].message.content or ""

The Prompts

The system prompt asks for a move as plain text in the same format a human player types, x1,y1 x2,y2:

You are an AI opponent in the game Dots and Boxes. Reply with ONLY one legal move in the format x1,y1 x2,y2 and nothing else. ...

build_prompt in ai_player.py writes the user prompt. It describes the board, the lines already drawn, and the legal moves. Giving the model the legal moves makes an illegal answer less likely.

The board has 3x3 dots, numbered from 0,0 (top left) to 2,2 (bottom right).
Lines already drawn: 0,0 1,0
Legal moves: 1,0 2,0; 0,1 1,1; 1,1 2,1; ...
Which line do you draw?

Note that the example labels dots x,y (column, row), while the JSON format earlier in these notes uses r, c (row, column). Either works, as long as the prompt says which one you use.

Reading and Checking the Move

Because the reply is plain text, parse_move uses a regular expression to find the first x1,y1 x2,y2 in it. Then choose_move checks that the move is legal:

def choose_move(self, game_logic):
    grid = game_logic.grid
    prompt = build_prompt(grid, game_logic.get_valid_moves())
    reply = ask_llm(SYSTEM_PROMPT, prompt)

    move = parse_move(reply)
    if move is None or not grid.is_valid_line(move[0], move[1]):
        raise ValueError(f"The model did not give a legal move. It replied: {reply!r}")
    return move

Handling Errors

If there’s no key, the model isn’t responding, or its move isn’t legal, choose_move raises an exception. The game loop in main.py catches it, shows the message, and ends the game:

try:
    start, end = current.choose_move(game_logic)
except Exception as error:
    print(f"AI error: {error}")
    print("The game cannot continue without the AI. Goodbye!")
    return

Ideas for Improving the Example

Reference

OpenRouter Models

OpenRouter Quickstart

OpenRouter Structured Outputs

Building an LLM-Powered Text-Based AI RPG— This video shows how an LLM can be used to drive game mechanics in a text-based RPG.

Note: Parts of this document were drafted using Gemini Flash 2.5 (2025).


Creative Commons License AI Programming 1 lecture notes by Brian Bird, written in , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


  1. GOFAI stands for “Good Old-Fashioned AI,” a nickname for symbolic AI. It uses hand-written rules and search algorithms, such as minimax, instead of learning from data. ↩