The making of King Thirteen

The making of King Thirteen

Hey hey people,

I wrote a game recently for a certain coding competition, and thought I'd share the thought process behind it. Let's start with the

Design

King Thirteen is by far the most deliberately designed game I've made. It all began with a very simple proposition: what if the tiles in the game of 2048 were chess knights?

It had to be specifically knights because

  • Bishops would play like two separate boards, having no interaction between the light-squared and dark-squared bishops
  • Rooks is just 2048
  • Queens is 2048 but very easy
  • Pawns I couldn't think of a way to make playable

This also meant that unlike 2048, the pieces would have to be controlled individually, as knights may have several valid moves in the same general direction.

Which, in turn, led to

Challenge №1: Coming back from a full board

The number of free squares on the board is clearly a resource in this game. (The fail state is when the board is full and there are no valid moves.)

If we spawn a new piece after every move, there are two possible outcomes:

  • Either the move merges two pieces, with no change to the number of free squares
  • Or the move doesn't merge two pieces, and one free square is lost forever

This is decidedly unfun, so a mechanic to free up squares was needed. I came up with the following:

  • If the current move is a merge, and the resulting piece has a follow-up move which is also a merge, then don't spawn a new piece (and keep the current piece selected)
  • Otherwise, spawn a new piece

This little combo mechanic I'm very happy with. It rewards the player for thinking ahead, and allows for satisfying plays.

However this leads to

Challenge №2: Indicating that a combo is happening

Communicating the mid-combo state to the player turned out to be unexpectedly difficult.

Some options I considered were

  • Putting a "combo" text on the screen, like in fighting games
  • Highlighting the follow-up move more prominently
  • Screen shake

In the end I didn't really solve this, and instead made the selected piece easily noticeable with a bright outline and a subtle idle animation.

Based on early playtesting, this was sufficient for (attentive) players to figure out the combo mechanic. Clearly there's a lot of room for improvement here.

Another feature that came out of this was the ability to undo the last move. It was added mainly to amend accidental moves, but in turn introduced

Challenge №3: Deterministic randomness

When a new piece is spawned, its location is chosen at random. If the player can manipulate said location by repeatedly making and then undoing a move, then this is a disaster! Such strategy trivializes the game, while also being unfathomably boring.

To combat this, the board state includes the internal state of the PRNG (I went with Mulberry32). When the undo button is pressed, both the board and the PRNG are reverted to their previous state.

The entirety of King Thirteen's (pseudo-)random behavior is now deterministic: repeating the same move will always result in the same outcome.

This also survives saving and loading the game: quicksave is done by writing the very data structure that's used for undo to local storage.

I really thought this whole PRNG tangent would be shorter. Oh well.

Now that setting up long combos became the way to play the game, comes

Challenge №4: Enabling tactical plays

The player might want to leave a specific square empty to set up a combo. However, moving a piece out of the way would often result in another piece immediately taking its place. This contributes to difficulty, but not in a fun way, it's more annoying than anything.

My solution was, when spawning a new piece,

  • Roll for an empty square
  • If the position we got was just vacated, roll again
  • The result of the second roll is final

So leaving a square empty on purpose became a viable strategy, as it should be.

At this point I though I was mostly done with the game design. Then I showed an early prototype to friends at work, and after recovering from the terrible terrible placeholder art they unanimously asked,

Why are there only knights though? Where's the rest of the chess pieces?

Design Episode 2: The rest of the chess pieces

What followed was the best possible brainstorming session, in which two competing ideas emerged:

  • Change the game to Arms Race-style upgrades, meaning two knights make a bishop, two bishops make a rook, and so on.
  • Keep the 2048 upgrades, but gradually introduce other pieces as the game progresses.

One advantage of the first game mode is readability: there are no numeric values attached to the pieces, so the board is less cluttered. I'll probably include this as a separate game mode in the future.

For the compo version, I went with the second option and added progression to the game. It works like this:

  • The game starts with only knights (OnlyKnights is a fantastic name for a chess website, by the way)
  • Bishops start appearing after reaching a value of 16, with ~33% spawn rate
  • Rooks start appearing after reaching a value of 64, with ~22% spawn rate
  • Queens start appearing after reaching a value of 256, with ~11% spawn rate

Despite the spawn rates, when every threshold is reached for the first time, the new piece is guaranteed to appear. I think this serves to better communicate the progression to the player.

Then finally it was time to add the last piece,

The King

The King is meant to be the antagonist of the game. He's also the only piece that isn't controlled by the player.

Bearing the number XIII to represent the theme of the compo ("triskaidekaphobia"), the crimson figure roams the board unpredictably.

The King resorts to violence only when he is cornered and has nowhere else to go. It's below him to kill commoners otherwise.

If there's no unobstructed path for the King, he will take the highest-value piece in his vicinity. However if you might lose due to the board being full, then the King won't help you by clearing the board; he'll just skip his turn.

I had so much fun programming these little behaviors. The resulting King is a very simple AI, just enough to introduce a bit of chaos to the game.

I'm also reasonably happy with the flavor text:

King Godric XIII, called the Crimson Hand, the Withering Flame, the dreaded King Thirteen rose from his throne.

The whispers of his subjects had grown too loud, too bold. They had forgotten their place.

Silence!

Clad in blood-red armor, Godric towered over them, an enormous and imposing figure.

I am fear incarnate! — his roar echoed through the hall. — You will bow before me, or you will burn!

Soon after, swords were drawn, and scarlet splattered the floor.

The King, adjusted

During the final playtesting, one issue with the King became apparent:

  • The game first waits for the player's move
  • Then spawns a new piece
  • Then the King moves

The problem is, if the King had just one opportunity to move, and the spawned piece blocked that move, then the King would instead go into attack mode. This is despite the player's best efforts to leave a path open for the King to go uncontested.

This felt unfair when it happened, so I changed the order of actions: first the player moves, then the King moves, then a new piece spawns. However the new piece will never choose the square that the King has just vacated. (The reason for this is purely aesthetic.)

Ah, and the end goal of the game is to reach the value of 2048. The King's value is 1024. Do with it as you will; regicide is not the only way to win the game, but it sure is satisfying.

So there's that

Thank you for reading! I hope you enjoyed the game more than this post. If you haven't played it yet, please do, and tell me what you think.

King Thirteen is available on

Any instance of "I" in this text should be read as "myself, with the invaluable help of my friends who are brilliant and I appreciate them very much".

Disappointingly, no LLM was sufficiently harmed in the writing of this post.