Your cat is not available now, please call back later
CAT'S BLOG

Yet another Multiplayer Game

icon

Chunk - multiplayer and singleplayer strategic game where you clain territory and dominate the board

TL;DR

Strategic game that suports player vs player in single browser or player vs bot.

Try it here: chunk.x96.dev

Overview

What is this game about? Dominating the game field and it is stupid simple.

board

Rules are simple:

  • Players step one after another
  • During each step, player chooses one of black edges and paints it into white
  • If edge closes the shape, shape area is colored into player's color (blue or red). Shape can be anything: square 1x1, square 2x2, pixel-art of octopus. Filling all edges is nore required, player have to fill only contour of the shape
  • Player with the most cells colored wins

It requires both: luck and strategy to win.

This game is planned to have 2 modes: offline (when you choose the amount of players and they all play on single device) and online - when you share room code.

Upd

During development, design and colors have been changed from "dark to light" to "light to dark"

Development

Prototype

Prototype of game field: simple square with extra paddings from top and bottom for controls

<!DOCTYPE html>
<html>
    <head>
        <style>
            :root {
                --unit: min(1vw, 1vh, 1rem);

                --background: #141414;
            }

            * {
                box-sizing: border-box;
            }

            html, head, body {
                width: 100%;
                height: 100%;

                margin: 0;
                padding: 0;
            }

            body {
                background-color: var(--background);
            }

            .vertical {
                width: 100%;
                height: 100%;

                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
            }

            .horisontal {
                width: 100%;
                height: 100%;

                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: center;
            }

            .game {
                width: calc(var(--unit) * 75);
                height: calc(var(--unit) * 95);

                border: 1px solid red;

                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;

                .board {
                    width: calc(var(--unit) * 75);
                    height: calc(var(--unit) * 75);

                    border: 1px solid green;
                }
            }

        </style>
    </head>
    <body>
        <div class="horisontal">
            <div class="vertical">
                <div class="game">
                    <div class="board">

                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

This is a simple prototype for future game UI, example below:

screenshot

First hardcoded page

stackscreenshoting

This makes eyes hurt, but that's how field should look before coloring. Next step is to add hover effects and test coloring.

Sizes were computed using OCD method: node is 2x2, edge is 2x8 and square is 8x8. Obviously, 82 != 75, so we need to scale it down:

Here is the equation: 2u * (N + 1) + 8u * N = 75, where u is unit and N is size of grid. Need to determine the u multiplier.

Finally, multiplier based on grid size:

        75
u = ----------
    (10 N + 2)
Can be used for parametrization of grid size and adaptive game UI.

So, now it looks better and handles :hover

screenshot

Adding .selected class for edges:

screenshot

And .selected class for cells (partially filled because I'm lazy):

screenshot

And .selected class for closed node to make it look better

And closing partially open edges:

screenshot

Implementing click handlers

Every time when player clicks on edge, game should check if this edge closes the full shape and this shape can be filled. To make so, click handler starts "recursive" fill for cell to the left and to the right (for vertical edge) and top with bottom for horisontal edge respectively.

While performing check, it iterates "recursively" through adjacent cells that do not have the filled edge between. If algorhitm meets the open edge at the edge of game board - cells can not be filled, because shape is not closed. Else, it can be filled.

screenshot

Attaching click handlers to every edge element is easy, but when it's time to fill the cells...

screenshot

This supposed to be full red, but something went wrong.

...

Meanwhile, after several hours of debugging, it works as expected:

record

Multiplayer logic

Currently, to implement multiplayer, I use state object with curretn game state that is enough to restore state on client (in addition to current game board).

Game state requires to know at least:

  • If game is over
  • Current player move
  • winners list
  • game mode (to know what should happen when player clicks on the edge)
app.state = {
    // Size of field
    size: 8,
    // Player turn
    player: 0,
    // Who am I
    whoami: 0,
    // Lock to prevent step during processing
    can_move: true,
    // Amount of players in session
    max_players: 2,
    // Current game mode
    game_mode: "bot",
    // Game state
    game_over: false,
    // Winner IDs
    winner_players: [],
    // Winner score
    winner_score: -1,
};

Basic game with bot looks like this. Bot acts by selecting random free edge without any strategy.

record

Restart button also works just fine.

record

Designing Menu

Time to design menu. For now, there are three modes: debug, play vs bots and local.

User can easily select size of field, I've set limit to 32x32 because larger fields may cause rendering issues.

By design, bot picker allows to select amount of bots that will be spawned to play with. Local picker allows selecting amount of players that will play the game locally in the same browser.

screenshot

Each of the buttons will spawn the game with selected mode and generate board for user-selected size.

"Local" mode allows playing with multiple people in single browser window without internet, bluetooth or IR port.

...

And it's actually playable now

screenshot

Future Plans

For now, it works. You can play it versus bot or with friends in a single browser.

Later I want to implement multi-browser multiplayer logic and allow creating sessions to be shared through internet. It's gonna take some time to implement, but I think it's gonna be fun anyway.

Last modified at 2026-01-20 23:53:26 UTC