For the last few weeks, I’ve spent some of my spare time building Soli, a small collection of solitaire card games for the desktop, written in Rust. It currently has Klondike, Spider, FreeCell and an original game called Cairn.

Cairn, on the Peak layout

Why build it?

There is absolutely nothing novel here, and there are plenty of solitaire games out there. I built Soli because I missed playing the old Solitaire and FreeCell games that used to ship with Windows, and I haven’t found a replacement I liked.

Building it has been a lot of fun. And yes, it’s all Claude. I don’t mind.

How it is structured

The code is split into two crates:

  • soli-core holds the rules of every game. It has no dependencies at all, which keeps the rules easy to test.
  • soli is the desktop front end, built on macroquad. It owns layout, drawing, animation, input, settings and statistics.

The front end doesn’t know the rules of any particular game. It talks to every game through a single Game trait, and only asks things like “can these cards move there?” or “where should this card go on a double-click?”. Every game keeps its changing state in a small Board struct that is cheap to clone, so undo is just a stack of snapshots. Animation is generic too: the front end compares where each card was drawn before an action with where it should rest after it, and flies whatever moved, so there’s no animation code specific to moves, undo or dealing.

Some interesting bits

Everything is drawn at runtime

The only asset in the game is the UI font. Cards, suits and the line-art animals on the card backs and court cards are all drawn from primitives at runtime. However, a single card takes thousands of triangles, mostly for the animals, which is far too many to draw every frame. So every face and back is painted once into a texture atlas at the card’s size in physical pixels, and each card on the table is drawn as a single textured quad. That came with a couple of gotchas:

  • Repainting the atlas on every frame while the window was being resized pushed memory use into the gigabytes, because the driver is slow to free the old render targets. The atlas now waits until the new size has held for a fraction of a second.
  • Ranks would sometimes go missing from cards. Turns out macroquad keeps all glyphs for a font in one texture and replaces it when it runs out of room, so text queued before the replacement comes out blank. On screen, the next frame fixes it. In the atlas, it stayed until the next repaint. The rank labels are now rasterised separately with fontdue.

Frame pacing

With VSync on, the OpenGL swap chain queues frames and a dragged card trails behind the cursor. So VSync is off and the game caps the frame rate itself: 165 fps while something is moving, 60 fps otherwise, and a table at rest isn’t redrawn at all until a window message arrives.

Winnable deals

FreeCell uses the same deal generator as the Microsoft version, so deal numbers match the classic game. The other games use their own small, seeded random generator, so a deal number always produces the same deal.

Spider was trickier, as many random deals can’t be won, particularly with four suits. Soli runs a beam search solver over random deals until it finds one it can win, on a background thread so the next deal is ready ahead of time. The catch is that the solver only finds the deals it’s good at, so winnable deals lean towards the easier ones.

Cairn

Cairn is the one original game in Soli, a mix of solitaire and Mahjong solitaire: two decks are dealt into an overlapping layout, and you clear it by removing free cards in matching pairs.

Random deals are almost never winnable here, so every deal is built around a solution: the generator plays the still-blank layout forwards, picking pairs of free slots, and only then assigns cards to them. The problem is that deals built this way felt too convenient, because removing a pair very often uncovered a card whose partner was already free. Measured with a simple player following the hints, that happened on about 70% of the moves that freed a card, against about 15% in a random deal.

What really fixed it were traps: on Normal and Hard, some pairs get the same kind as a later pair, so three copies of a card are free at once and one of them lies over the fourth. Pair the right two and you’re fine. Pair the wrong two, and the deal can no longer be won. The solution still works, so every deal stays winnable, but you have to pay attention now.

Trying it out

You can find the code on GitHub, and Windows binaries on the releases page. It should build and run on macOS and Linux as well with cargo run --release.

Hope you enjoy it!