William Marais
All tutorials
Programming, ExJSON, example guide

Draw a text grid with ExJSON

Use input, nested loops and string concatenation to turn two dimensions into a printable grid.

Beginner · 10 minute walkthrough
Before you start
  • Python installed locally.
  • A local copy of the ExJSON repository.
  • Basic familiarity with variables and loops.

1. Prepare the project

Clone ExJSON, then work from the repository root. The transpiler reads a .xjs source file and writes JSON instructions; the interpreter executes the JSON file.

terminalShell
git clone https://github.com/SAED2906/ExJSON.git
cd ExJSON

2. Create the grid program

Save the following program as grid.xjs in the repository root. The first two inputs specify the width and height. A row of underscores forms each horizontal edge, and repeated vertical bars form the cells.

grid.xjsExJSON
int x = 0
int y = 0

in(x)
in(y)

String grid = ""

int counterx = 0
int countery = 0
int linewidthcounter = 0
while (countery < y) {
    linewidthcounter = 0
    while (linewidthcounter <= x) {
        grid = grid + "__"
        linewidthcounter = linewidthcounter + 1
    }
    grid = grid + "\n"
    while (counterx < x) {
        grid = grid + " |"
        counterx = counterx + 1
    }
    grid = grid + "\n"
    countery = countery + 1
    counterx = 0
}
linewidthcounter = 0
while (linewidthcounter <= x) {
    grid = grid + "__"
    linewidthcounter = linewidthcounter + 1
}

print(${grid})

3. Transpile and run

Both scripts accept the file basename without its extension. The first command produces grid.json. The second prompts for two input values; enter 3 and 2 on separate lines.

terminalShell
python3 src/transpiler.py grid
python3 src/compiler.py grid

After the interpreter’s program label, the resulting grid should look like this:

outputText
________
 | | |
________
 | | |
________

4. Experiment with the loops

The outer loop advances through rows. The inner loops build each horizontal edge and the vertical dividers. Resetting counterx after each row lets the next row use the same width.

  • Try different positive widths and heights.
  • Change the horizontal edge from underscores to another character.
  • Trace the counter values for a 1 × 1 grid before running the program.
Explore the ExJSON project
Browse all tutorials