MJr is a probabilistic programming language based on pattern-rewriting, heavily inspired by MarkovJunior by Maxim Gumin. This project provides a compiler for the MJr programming language, which compiles MJr programs to JavaScript, TypeScript or Python 3.
- Basic.mjr: the simplest MJr program that does anything. Hello, World!
- Backtracker.mjr: generates connected cave systems with pools, using only two rewrite rules.
- BacktrackerCycle.mjr: generates a random cycle.
- River.mjr: generates a river with grass growing around it.
- GameOfLife.mjr: John Conway's "Game of Life".
- BasicSnake.mjr: plays a classic game of Snake, albeit not very well.
- PacMan.mjr: another classic game.
- BasicDungeonGrowth.mjr: builds a connected dungeon with rooms and corridors.
- NystromDungeon.mjr: builds a connected dungeon using Bob Nystrom's algorithm.
- MazesAndLakes.mjr: generates a weird landscape of cities, forests and lakes.
More examples can be found in the models folder.
MJr programs can be compiled to JavaScript, TypeScript or Python 3. To use the compiled output in your project, you must also include the MJr runtime library:
- JavaScript/TypeScript: MJr.js
- TypeScript type declarations: MJr.d.ts
- Python 3: MJr.py (requires Python 3.10+)
The MJr program can then be used by calling its main function, which has the following signature:
function main(width: int, height: int, rng?: PRNG): Grid;
The return value is a Grid
object with the following properties:
width
andheight
: the grid dimensionsdata
: the grid cells, as a flat array of unsigned integers in row-major order.alphabet
: a string mapping the values indata
to the alphabet symbols used in the MJr source code.
For convenience, Grid
objects can also be converted to strings for display.
The rng
parameter is optional; if provided, it must be an object with a nextDouble
method returning a random floating-point value in the half-open interval [0, 1)
, and a nextInt
method accepting a positive integer n
and returning a random integer in the half-open interval [0, n)
. If not provided, a default unseeded algorithm is used.
See the documentation and implementation notes for more information.