Constrained Text Generation with AI

I was discussing how AI text generation, such as ChatGPT, might end up getting used in computer games. So far, designers are fairly reluctant to adopt the technology. One of the key problems is that you just can’t control the output enough. Language models will break character or respond in inappropriate and toxic ways. Finding a good solution to this is a huge research field, and not likely to get cracked soon.

For the foreseeable future, AI in games is much more likely to be used offline – assets and dialog generation generated up front, so it can be vetted before being integrated into the game.

But it got me thinking, can we vet the AI’s output in advance, but still get the benefits of intelligent decision making at runtime? It turns out, we can! I doubt it’ll be useful in every circumstance, but I can certainly see uses for it, like chatbots, games.

The code and demonstration for this article is available here.

Continue reading

Defining Chess Piece Moves using Regular Expressions

Suppose you wanted to code a simple chess game. One key bit of game logic is to describe what are legal moves for each piece. There’s only 6 types of piece (pawn, knight, bishop, rook, queen, king) so this isn’t exactly a hard task. You can write rules such as:

def canRookMove(from, to):
  # Ignores questions about colliding with other pieces
  return (from.x == to.x or from.y == to.y) and from is not to

But these days, I’ve been thinking a lot about grids, and the above approach just doesn’t generalize. What if you wanted to play chess on a stranger grid?

Three player chess (CC BY 2.0, source)

What would it mean to play chess on the grid above, or a hexagonal grid, and so on? You’d have to write a whole new set of rules, and they could get very complicated depending on the grid in question. What I want is a language that describes how pieces move, which generalizes to any board. And I think I’ve found it, using regular expressions.

Continue reading

Learning to Finish Things

For many years I was a hobbyist programmer. I’d try out small projects, experiment, then move on to the next thing. This was a great way to learn a lot, but I’ve got almost nothing tangible to show from that era. Despite the best of intentions, every project reached a point where it started to drag, and I’d get bored and move on.

It was only a problem for the projects I worked on myself. Working for others, I never really found the same problems. It was a problem of motivation and focus.

More recently, it is different. Now when I start hobby projects, there’s a good chance I’ll cross that finish line, and have something I’m ready to share with the world. What changed? Well in part it is increased experience and maturity, things I cannot teach. But also, I have found some strategies and thought processes helpful, and other, very tempting ones, not so much.

In short, I’ve learned to finish, which is a real skill you can learn over time. I thought I’d share with you what has worked for me. Maybe it’ll work for you too. I make software, but I think this advice is generally true for any other spare time activities. There’s three sections – scope, motivation and distractions.

Continue reading


I’ve relased a new library, VoronatorSharp.

VoronatorSharp is a C# library that computes Voronoi diagrams. The Voronoi diagram for a collection of points is the polygons that enclose the areas nearest each of those sites.

Voronoi diagrams have applications in a number of areas such as computer graphics.

This library features:

  • Computes Voronoi diagrams and Delaunay triangulations.
  • Voronoi polygons can be clipped to a rectangular area.
  • Uses a n log(n) sweephull algorithm.
  • The implementation attempts to minimize memory allocations.
  • Integrates with Unity or can be be used standalone.
  • Uses robust orientation code.
  • Handles Voronoi diagrams with only 1 or 2 points, and collinear points.

The Grokalow

One day, the grokalow crawled out of the swamp.

“It looks like a gargantuan alligator”, cried a bystander, as it approached.

“Nay, it is a brobdingnagian crocodile”, countered a second, as the grokalow licked its leathery lips.

“Without a clear definition, we cannot conclude this thing is a threat”, surmised the third, settling the matter.

The grokalow ate them all, with great satisfaction.