- Updated: January 4, 2026
- 6 min read
Open‑Source JAX Tic‑Tac‑Toe AI: Reinforcement Learning Tutorial and Results
The JAX Tic‑Tac‑Toe AI project shows how to train a reinforcement‑learning agent that achieves perfect play in under 15 seconds on a laptop by using the open‑source PGX library, a deep Q‑network, and JAX’s high‑performance autodiff.
JAX‑Powered Tic‑Tac‑Toe AI: A Complete Open‑Source Reinforcement Learning Tutorial
Learn how to build a perfect‑play Tic‑Tac‑Toe AI with JAX, PGX, and deep Q‑networks. Step‑by‑step methodology, results, and future directions for AI developers.
In early 2026, a community‑driven tutorial demonstrated that a simple neural network can master Tic‑Tac‑Toe using JAX and the original JAX Tic‑Tac‑Toe guide. This article expands on that work, providing a deeper technical dive, performance benchmarks, and practical tips for developers who want to replicate or extend the project.
Whether you are a data‑science student, an AI hobbyist, or a SaaS engineer looking for a quick reinforcement‑learning (RL) demo, the steps below will give you a reproducible pipeline that runs on any modern laptop.
Project Overview: Why JAX and PGX?
JAX combines NumPy‑style syntax with just‑in‑time (JIT) compilation, automatic differentiation, and seamless GPU/TPU acceleration. The machine learning tutorials on UBOS highlight JAX’s suitability for RL because it can vectorize thousands of game simulations in parallel.
The JAX projects collection also showcases the PGX library, a pure‑JAX implementation of classic board games (including Tic‑Tac‑Toe). PGX provides a State dataclass that packs the board, legal moves, rewards, and termination flag—all in a format that JAX can batch efficiently.
Key advantages of this stack:
- Fast parallel simulation of hundreds of games per training step.
- Zero‑copy GPU execution thanks to JAX’s functional programming model.
- Open‑source, fully reproducible code that can be adapted to other grid‑based games.
Methodology
1. State Representation
PGX encodes the board as a (3, 3, 2) boolean tensor. The first channel marks the current player’s pieces, the second channel marks the opponent’s pieces. This representation flips automatically each turn, allowing a single neural network to always play as “player 0”.
Example state (visualized):
[[[False, False], [False, True], [False, True]],
[[False, False], [ True, False], [False, False]],
[[ True, False], [False, False], [False, False]]]
2. Deep Q‑Network Architecture
The model is a lightweight fully‑connected network (FCN) with two hidden layers of 128 neurons each. After flattening the (3,3,2) tensor to a 9‑element vector (X = +1, O = ‑1), the network outputs a Q‑value for each of the nine board positions.
Tailwind‑styled code snippet:
class DQN(nnx.Module):
def __init__(self, *, rngs, n_neurons: int = 128):
self.linear1 = nnx.Linear(9, n_neurons, rngs=rngs)
self.linear2 = nnx.Linear(n_neurons, n_neurons, rngs=rngs)
self.linear3 = nnx.Linear(n_neurons, 9, rngs=rngs)
def __call__(self, x):
x = x.astype(jnp.float32)
x = x[..., 0] - x[..., 1] # X → +1, O → -1
x = jnp.reshape(x, (-1, 9))
x = nnx.relu(self.linear1(x))
x = nnx.relu(self.linear2(x))
return nnx.tanh(self.linear3(x)) # Q‑values in [-1, 1]
3. Training Loop & Temporal‑Difference Learning
The agent learns via TD‑learning (Q‑learning). For each transition (s, a, r, s’), the target is:
Q_target = r + max_a’ Q_target(s’, a’)
To stabilize learning, a separate target network is updated with an exponential moving average (τ = 0.005) of the policy network’s weights.
Exploration is handled with an ε‑greedy schedule that decays from 0.9 to 0.05 over 2,500 training steps.
def train_step(policy_net, target_net, optimizer, transition, batch_size, tau):
state, action, next_state = transition
best_next = jnp.max(
target_net(next_state.observation) * next_state.legal_action_mask,
axis=1
)
reward = next_state.rewards[jnp.arange(batch_size), next_state.current_player]
target = -(reward + (~next_state.terminated).astype(jnp.float32) * best_next)
loss, grads = nnx.value_and_grad(loss_fn)(
policy_net, target, state, action, batch_size
)
optimizer.update(policy_net, grads)
# EMA update for target net
policy_params = nnx.split(policy_net)[0]
target_params = nnx.split(target_net)[0]
new_target = jax.tree.map(
lambda p, t: (1 - tau) * t + tau * p, policy_params, target_params
)
nnx.update(target_net, new_target)
Results & Performance Metrics
After roughly 2,500 training steps (≈15 seconds on a mid‑range laptop), the agent reaches the following win‑rate against a random opponent:
| Training Step | Win % | Tie % | Loss % |
|---|---|---|---|
| 0 | 46.4 | 17.8 | 35.8 |
| 1,200 | 92.9 | 7.1 | 0.0 |
| 2,500 | 94.1 | 5.9 | 0.0 |
Key observations:
- The model never loses after ~1,200 steps, confirming convergence to optimal play.
- Ties gradually decrease as the policy learns to force a win when possible.
- Training time scales linearly with batch size; a batch of 2,048 steps fits comfortably in GPU memory.
For developers interested in rapid prototyping, the AI agents page on UBOS provides ready‑made templates that can be adapted to this workflow.
Key Takeaways & Future Directions
Takeaway 1 – Simplicity wins. A modest FCN with 128‑neuron layers is sufficient for a solved game like Tic‑Tac‑Toe. The same architecture can be a baseline for more complex board games (e.g., Connect‑Four, Othello).
Takeaway 2 – Vectorized environments accelerate RL. PGX’s ability to batch 2,048 games in a single JAX call reduces wall‑clock time dramatically. For larger state spaces, consider Workflow automation studio to orchestrate data pipelines.
Takeaway 3 – Target networks are essential for stability. The EMA update (τ = 0.005) prevents the moving target problem that often destabilizes Q‑learning.
Future Extensions
- Curriculum learning. Start training on end‑game positions, then gradually introduce early‑game states to speed convergence.
- Multi‑agent self‑play. Let two identical agents play each other without a random opponent to generate richer experience buffers.
- Transfer to other domains. Reuse the same codebase for turn‑based strategy games or even simple text‑based decision problems.
UBOS offers a template marketplace where you can find pre‑built “AI Article Copywriter” or “AI SEO Analyzer” templates that illustrate how to plug a trained model into a web service with just a few clicks.
Conclusion
The JAX Tic‑Tac‑Toe tutorial proves that high‑performance reinforcement learning is accessible to anyone with a modest laptop. By leveraging JAX’s JIT compilation, PGX’s vectorized game engine, and a simple deep Q‑network, developers can achieve perfect play in seconds and then iterate toward more ambitious projects.
Ready to experiment? Explore the UBOS platform overview for a low‑code environment that hosts JAX notebooks, or dive straight into the Enterprise AI platform by UBOS for scalable GPU workloads.

Meta description: Discover how to build a perfect‑play Tic‑Tac‑Toe AI with JAX, PGX, and deep Q‑networks. Step‑by‑step methodology, performance results, and future extensions for AI developers.
For a broader view of how AI can accelerate business processes, check out our AI marketing agents. If you’re a startup, the UBOS for startups page explains how to spin up AI‑driven products quickly. SMBs can benefit from UBOS solutions for SMBs, while larger enterprises may explore the Enterprise AI platform by UBOS.
Developers who prefer a visual builder can use the Web app editor on UBOS to wrap the trained model in a REST API, and the UBOS pricing plans make scaling affordable.
Andrii Bidochko
CTO UBOS
Andrii Bidochko is an AI entrepreneur and researcher focused on AI agents, reinforcement learning, and autonomous systems. He writes about the technologies shaping the future of machine intelligence, from frontier models and agent architectures to real-world AI applications.