I’m building a Block Blast solver in Unity like built here (blockblastsolverlive.com) as short code and wondering what the most efficient way is to represent the 9x9 board internally.
I need to:
Check for valid placements of 3-piece sets
Detect completed rows/columns/3x3 squares
Allow fast state manipulation and rollback
Right now I’m using a bool[,] for the grid and defining each piece as a List<Vector2Int> shape, but it’s getting messy when testing different placement scenarios.
Any suggestions on more efficient structures (bitboards, enums, structs)? Also curious if anyone uses ScriptableObjects for defining pieces dynamically.
Congrats! You’re already FAR ahead of the game by having a data representation separate from what is in the Unity engine. Too many tutorials conflate these into an ugly mess.
Wrap that stuff up in an API that represents the actions you do to / from / with your data, such as checking validity, detecting stuff, etc.
For rollback, at the scale you’re talking about, just allocate copies and put them in a List<T>(); so you can undo by going back and restoring prior states.
Here was my standard blurb for these sorts of grid games, including source (see near bottom) for my Match3 implementation:
For any tile-based game such as Match3 or Tetris or a grid-based Roguelike, do all the logical comparisons in your own data storage mechanism for the tiles, such as a 2D array of tiles.
Otherwise you needlessly bind your game logic into Unity objects and the Unity API, making it about 10x more complicated than it needs to be.
If you have no idea how to work with 2D arrays, hurry to some basic C# tutorials for the language portions of it, then look at any good tutorial that uses a 2D array
Here is my Match3 demo using this technique of storing data in a grid. Full source linked in game comments.
It stores all of its data in a 2D array:
PieceController[,] Board;
This allows for easy simple checking in code, not relying on anything like physics.
You should strive to use that pattern for all logic, then only use Unity to present what is happening in the game logic.
Most likely you’re going to want a 2d array of a class/struct to encapsulate the information on each board piece, managed by an outer class which provides other parts of your project with a clean API to interface with it.
Though don’t get too hung up on perfecting it or making it ‘optimal’ on the first go. The structure of your data is going to change over the course of your project. No doubt you’ll have things you missed and need to refactor for.