A Blokus Engine Written in C

Jan 25, 2020 - Github Link

This is a C implementation of the popular Blokus board game. The program only supports the 4-Player variation of the game in which 4 (human) players have sets of colored pieces and each have the goal of placing as many pieces on the board as possible and reach the highest score. All players share the same keyboard inputs.

the board

The game board is a 20x20px display scaled up for readability.

the pieces

Every piece is a 5x5 array of 1s and 0s. When the program decides whether to place a piece or not, it loops through all the values of the array and if the value is 1, it draws a pixel to the screen corresponding to the color of the active player.

Here is the J tetromino for example:


{
    {0, 1},
    {0, 1},
    {1, 1},
}
    

All the rotations are hardcoded as well.


{
    {
        {0, 1},
        {0, 1},
        {1, 1},
    },
    {
        {1, 0, 0},
        {1, 1, 1},
    },
    {
        {1, 1},
        {1, 0},
        {1, 0},
    },
    {
        {1, 1, 1},
        {0, 0, 1},
    },
}
    

reflections

In real life you can flip the piece any which way before deciding to put it on the board. This means that for a correct recreation of the board game in a program, we need to allow for reflections as well as rotations. For our example piece above, we therefore need to allow a player to flip the piece to an L (and cycle through those rotations if desired).

Before the program draws straight from the lookup table of pieces - which do not contain the reflection arrays of the pieces - it first copies the piece into a special 5x5 array called int Piece_Preview[5][5].

Then if the user desires to reflect the piece vertically (by pressing A), the program applies the function Reflect_Preview_Piece to the Piece_Preview variable and reflects it.


void Reflect_Preview_Piece(int grid[5][5])
{
    // reverse the rows
    Swap_Rows(grid, 0, 4);
    Swap_Rows(grid, 1, 3);

    for(int row = 0; row < 5; row++)
    {
        if(Row_Is_Zero(grid[0]))
        {
            // move top row down
            Swap_Rows(grid, 0, 1);
            Swap_Rows(grid, 1, 2);
            Swap_Rows(grid, 2, 3);
            Swap_Rows(grid, 3, 4);       
        }
    }
}
    

Some pieces are totally symmetrical and will not look different, but for the ones that do, the display of the piece will flip and it can be rotated like normal.

thanks for reading

See the README on how to run. Enjoy!