← Nesim Apps

Browser Game Demos: Porting App Rules to JavaScript

By Nesim Razon, independent mobile developer · 2026-11-28

#Dev notes#Game design#Algorithms

A screenshot shows what a game looks like. A small playable board shows what it feels like to take a turn. I added browser versions of three of my Android games to their pages: tic-tac-toe, 2048 and a turn-based maze called Grumblings. The interesting part was not drawing them. It was making sure the web version plays by the same rules as the app.

Why a port at all

The apps are written in Kotlin. The site is plain static files, with no server and no build step beyond a few Python scripts that write HTML. Compiling the Kotlin game logic for the web would be possible, but it would bring a toolchain and a download size far larger than the problem. The rules of these three games are small, so I wrote each of them again in plain JavaScript.

That choice has a clear cost: two implementations of the same rules can drift apart. A visitor who learns the game on the web and then finds different behaviour in the app has been misled. So the port is only acceptable if something checks it, and the rest of this post is about that something.

One file, two halves

Each demo is a single script. The first half is the game itself: functions that take a board and return a new board, with no reference to the page. The second half builds the buttons and cells, handles taps and keys and animates the result.

The split is not about elegance. It is what makes testing possible. Because the logic half never touches the document, the same file can be loaded by Node, which has no browser at all. The script exports its functions when it finds itself in Node, and mounts itself on the page when it runs in a browser, wherever it finds a placeholder element with its data attribute.

The site generator only has to add that placeholder and one script tag to a game page. The script address carries a version parameter, so a visitor does not keep an old cached copy after a fix.

Tests that mirror the app

Each demo has one test file that runs with Node and its built-in assert module. There is no test framework to install or update. Every test is a named function, and the file prints a line for each one and a total at the end.

The test cases are modelled on the app's rules and tests. For 2048 that means the merge rules written as small rows: four twos slide into two fours, and a tile that was just created cannot merge a second time in the same move. For the maze it means the same tiny positions the Kotlin tests use: which way a monster steps first, what happens when it is blocked and in which order two monsters take their turns.

Randomness you can repeat

Two of the games use chance. A test that passes on most runs is worse than no test, so every function that needs a random number receives the generator as a parameter. In the browser that is the normal random function. In tests it is a few lines of a seeded generator that produces the same sequence every time.

With repeatable randomness you can make statements about many games. In 2048, a new tile should be a two in about nine cases out of ten and a four otherwise, so the test spawns twenty thousand tiles and checks that the number of fours stays in the expected range. Another test plays hundreds of complete random games and verifies that two tiles never occupy the same cell and the score never goes down.

Testing a computer opponent

Tic-tac-toe has three difficulty levels, and each one is a promise to the player. Hard must be unbeatable. For a board this small the test does not need to trust sampling: it walks through every possible sequence of human moves against the hard player and fails if the human ever wins.

Medium must be beatable, because it deliberately does not understand forks. The test lets a flawless player compete against it and requires a number of wins. Easy must be clearly weaker than medium. These tests protect the feel of the game, which a rule test cannot see. A well meant improvement that made medium perfect would be caught.

Levels copied from the app

The maze demo does not generate levels. It carries the first thirty levels of the app, as short text lines that describe the board size, the positions, the walls and the best solution. Copied data can go stale, so two checks run on it.

The first replays each stored best solution through the JavaScript rules and requires a win, then asks the JavaScript solver for the shortest path and requires the same length. The second compares the embedded lines with the app's level file, line by line, when that file is available on the machine. If I regenerate the levels in the app and forget the site, the test says which line differs.

What the tests do not cover

Passing tests do not prove that the two versions are identical. They prove that the cases I wrote down behave the same. Animation, touch handling and layout are checked by hand on a phone and a desktop screen. A rule that exists in the app and was never written as a test can still be missing from the port.

That is why I keep the demos deliberately small: one mode of each game, no accounts, no saved progress. Every extra feature in the demo would be another thing to keep in step.

Input for a phone and a keyboard

A demo on a web page is opened on whatever the visitor has. The 2048 board accepts arrow keys when it has focus and swipes on a touch screen. The maze accepts keys, including one to wait a turn and one to undo, and reads pointer gestures. Each demo also has ordinary buttons, so nothing depends on knowing a gesture.

The labels exist in the four languages of the site, and the demo picks the set that matches the page. Keeping the texts inside the script means one file still contains everything the game needs, at the price of a few extra lines.

Small helps that the app also gives

A first visit should not end in confusion. The maze demo offers undo, restart, a hint taken from the solver and a switch that shows the path the monster would take. Because the solver is part of the logic half, the hint is real: it is computed from the current position, not read from a stored answer.

The 2048 demo keeps one step of undo and lets you continue after reaching 2048, as the app does. Each demo ends with a link to the game on Google Play, passed in from the page, so the script itself does not need to know store addresses.

If you want to do the same

Frequently asked questions

Did you compile the Kotlin code for the web?

No. The rules are small, so they are rewritten in plain JavaScript and checked by tests.

How are the demos tested without a browser?

The game logic does not touch the page, so Node can load the file and call its functions directly.

Is the hard tic-tac-toe player really unbeatable?

A test walks through all human move sequences against it, with several seeds for the computer's equal choices, and the human never wins.

What keeps the web levels the same as the app?

A test replays every embedded solution and compares the lines with the app's level file when it is present.

More from the blog