Scrappy Unity Coding: making Chess work in a day


For my 7DRL submission Wayward King Attack my idea was to make a chess game where instead of the board being even, you’d collect points or pieces as you went along, and so too would the enemies get harder. I wanted to make more than just the regular pieces: there’s only so much variety in pawn, bishop, rook etc, and it’s a roguelike challenge, so the randomizer element was big. Also this being a 7 day jam I wanted to use the fastest possible method of generating the pieces, because I also had to do level generation, AI, and the rest of the project. I believe I found a pretty interesting way to do this in unity.

Final chess board

Note that special rules in chess were out of scope for this project. So any situational moves that would be available or unavailable I decided were too much for the 7 days. Off the top of my head these would be en passant and pins. I did however cover the special pawn move on the first turn.


Serializing Tile Availability

So instead of hardcoding pieces or making individual prefabs, I wanted to use Scriptable Objects to store the move information along with things like the sprite and the value of the piece. The challenge here came from how to represent this easily in the inspector.

You could theoretically have individual switches for types of movement (e.g. a boolean for diagonal movement, vertical & horizontal, etc.) but that breaks down pretty quickly for things like knights and pawns. I also wanted to get abstract with the movement, so I wasn’t satisfied just hardcoding all the original pieces and combining them.

The buck can move in the shape of antlers

Another option was to use a custom inspector with a grid of booleans for possible moves. That’d turn move assignment into a pretty easy clicking session for each piece. I didn’t do this because I didn’t want to figure out the custom inspector code and it would still be pretty tiresome.

What I ended up going with was 3 string fields of a serialized array of possible moves per piece: one for basic movement, one for captures (things like how pawns capture diagonally), and one for the first move (again for pawns).

The Pawn scriptable object

Here’s the pawn object. It’s not very readable, but the only possible move the pawn has is 0,1 since it can only go forward. The first move serial adds 0,2 as an option, and the capture serial has the diagonals only. This is arguably more cumbersome than the custom inspector thing but that’s where the next part comes in.


Implementing a Tilemap Serializer

Instead of writing out all of the pieces, the idea was to write a class that took in a Tilemap and reported the relative positions of every tile. Then I could just copy and paste that into the textboxes for my scriptable objects.

All the code is doing is iterating through the tilemap and checking if there’s a tile on the position. It then adds that position (relative to the center point) to a list, and at the end of iterating, serializes the list to json in the same format my scriptables expect. My grid is 15x15 because the most a piece will ever be able to move in a single direction on the classic chess board is 7 tiles.

Queen tilemap and json

Here we have the queen represented on the tilemap. Using the tile paintbrush I just painted this into the scene in a few seconds, and then made a small class that fires an event when an inspector button is pressed, which tells the TilemapSerializer to dump the json array into the textbox field. Using this I was able to hack together ~47 pieces very quickly. Of course this falls apart for any move that requires a special board state (actually, check would work here, I just didn’t code deep enough to check for king threats) but it was sufficient for a 7 day game jam.

Get Wayward King Attack

Leave a comment

Log in with itch.io to leave a comment.