Tips to create a labyrinth with lanterna – Part II

The terminal (in lanterna) has a fixed size. But sometimes you will have a labyrinth which will be bigger than this terminal. As soon as your user moves the player outside of the fixed sized terminal screen you will not be able to see him anymore. What can you do about that? You can redraw the labyrinth.

Calculate Sections

Each labyrinth can be of a different size. It may fit into the terminal or not. Whenever a character is put or drawn in the terminal the cursor has to be moved to the right position (x-coordinate, y-coordinate).  Ideally you need to cluster a labyrinth into serval sections. Depending on which section the user is positioned the content of the terminal is filled. How to calculate the right section is illustrated in the following example:

  • The terminal has a size of 10×30.
  • The labyrinth has a size of 20×35.
  • The user has the Position x: 33, y: 15

In sum the user can be on four different sections. Horizontally (x- axis) there are 2 sections and vertically (y-axis) there are also 2 axis.  To calculate on which section (horizontally/vertically) the user is positioned division can be used.

  • x-Axis section: 33/30= 1
  • y-Axis section: 15/20= 0


private void calculateCoordinates() {
   Position positionOfPlayer = getPlayerPosition();
   xAxisSection = calculateSection(positionOfPlayer.getXAxis, getTerminalColumns());
   yAxisSection = calculateSection(positionOfPlayer.getYAxis, getTerminalRows());

private int calculateSection (int coordinateOfPlayer, int coordinateOfterminal) {
   return coordinateOfPlayer / coordinateOfterminal;

Draw Sections

Use these sections to fill the terminal with the according labyrinth part. Use adequate loops and the modulo operator. But before drawing don’t forget to clear the terminal screen first (use terminal.clearScreen() ). Furthermore calculate the x-Axis and y-Axis before drawing.  After drawing the visible section of the labyrinth- move the cursor to the position of the player and insert the player’s symbol in it.

int y = (terminalRows * yAxisSection);
while (y < terminalRows * yAxisSection + terminalRows) {
   int x = (terminalColumns * xAxisSection);
   while (x < terminalColumns * xAxisSection + terminalColumns) {
       drawOnTerminal(x, y);

private void drawOnTerminal(int x, int y){
   Symbol symbol = getSymbolOnPosition (new Position(x, y));
   terminal.moveCursor(x % terminalColumns, y % terminalRows);



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s