DEV Community

Cover image for Using a local LLM AI agent to solve the N puzzle
Dang Manh Truong
Dang Manh Truong

Posted on

Using a local LLM AI agent to solve the N puzzle

Hi everyone, I have just made some program to make an AI agent solve the N puzzle.

Github link: https://github.com/dangmanhtruong1995/N-puzzle-Agent/tree/main

Youtube link: https://www.youtube.com/watch?v=Ntol4F4tilg

The qwen3:latest model in the Ollama library was used as the agent, while I chose a simple N puzzle as the problem for it to solve.

Experiments were done on an ASUS Vivobook Pro 15 laptop, with a NVIDIA GeForce RTX 4060 having 8GB of VRAM.

Overview

This project demonstrates an AI agent solving the classic N-puzzle (sliding tile puzzle) by:

  • Analyzing and planning optimal moves using the Qwen3 language model

  • Executing moves through automated mouse clicks on the GUI

How it works

The LLM is given some prompt, with instructions that it could control the following functions: move_up, move_down, move_left, move_right. At each turn, the LLM will try to choose from those functions, and the moves would then be made. Code is inspired from the following tutorials on functional calling and ReAct agent from scratch:

Installation

To install the necessary libraries, type the following (assuming you are using conda):


conda create --name aiagent python=3.14

conda activate aiagent

pip install -r requirements.txt

Enter fullscreen mode Exit fullscreen mode

How to run

There are two files, demo_1_n_puzzle_gui.py (for GUI) and demo_1_agent.py (for the AI agent). First, run the GUi file:


python demo_1_n_puzzle_gui.py

Enter fullscreen mode Exit fullscreen mode

The N puzzle GUI will show up. Now, what you need to do is to move it to a proper position of your choosing (I used the top left corner). The reason we need to do this is that the AI agent will control the mouse to click on the move up, down, left, right buttons to interact with the GUI.

Next, we need to use the Pyautogui library to make the AI agent program aware of the button locations. Follow the tutorial here to get the coordinates: link). An example:


(aiagent) C:\TRUONG\Code_tu_hoc\AI_agent_tutorials\N_puzzle_agent\demo1>python

Python 3.13.5 | packaged by Anaconda, Inc. | (main, Jun 12 2025, 16:37:03) [MSC v.1929 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import pyautogui

>>> pyautogui.position() # current mouse x and y. Move the mouse into position before enter

(968, 56)

Enter fullscreen mode Exit fullscreen mode

Once you get the coordinates, please populate the following fields in the demo_1_agent.py file:


MOVE_UP_BUTTON_POS = (285, 559)

MOVE_DOWN_BUTTON_POS = (279, 718)

MOVE_LEFT_BUTTON_POS = (195, 646)

MOVE_RIGHT_BUTTON_POS = (367, 647)

Enter fullscreen mode Exit fullscreen mode

Next, open another Anaconda Prompt and run:


ollama run qwen3:latest

Enter fullscreen mode Exit fullscreen mode

Now, open yet another Anaconda Prompt and run:


python demo_1_agent.py

Enter fullscreen mode Exit fullscreen mode

You should start seein the model's thinking trace. Be patient, it takes a while for the AI agent to find the solution.

However, a limitation of this code is that when I tried to run on bigger problems (4x4 puzzle) the AI agent failed to solve it. Perharps if I run models which can fit on 24GB VRAM then it might work, but then I would need to do additional experiments.

Top comments (0)