Search code examples
javamultidimensional-arraymaze

Maze Pattern Building in Java


Here is some code I work on, to build a maze with pattern like this where I implement 2D array.

The idea of mine, first is trying to build a full '@' in 2D array, and every odd rows I give it ' '. It doesn't finish yet

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Maze Dimension: ");
        int dim = sc.nextInt();

        //dimension
        char[][] maze = new char[dim][dim];
        int baris = maze.length;
        System.out.println("rows : " + baris);
        int kolom = maze[0].length;
        System.out.println("column : " + kolom);

        //initialize rows and column;
        int initBaris;
        int initKolom;

        for (initBaris = 0; initBaris < baris; initBaris++) {
            if (initBaris % 2 != 1) {
                for (initKolom = 0; initKolom < kolom; initKolom++) {
                    System.out.print(maze[initBaris][initKolom] = '@');
                }
            } else {
                for (initKolom = 0; initKolom < kolom ; initKolom++) {
                    System.out.print(maze[initBaris][initKolom] = ' ');
                }
            } System.out.println();
        }
    }
}

The result of my code is shown below:

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@

I want the result like below :

@ @@@@@@@@@@@@@
@             @  
@@@@@@@@@@@@@ @
@             @  
@ @@@@@@@@@@@@@
@             @  
@@@@@@@@@@@@@ @
@             @  
@ @@@@@@@@@@@@@
@             @  
@@@@@@@@@@@@@ @
@             @  
@ @@@@@@@@@@@@@
@             @  
@@@@@@@@@@@@@ @

I lack of logic here, to be simple. Meaning - I don't know how to get to the expected result so looking for guidance on how to adapt the given code.


Solution

  • On top of GhostCat advices that you should definitely apply, considering your current code:

    1. I would try to validate the input for the dimension. Indeed since you want the first and last row to be walls (@) and each wall rows to be separated by space/corridor rows, you will always have an odd number of rows (you could decrease/increase dimension by 1 if input%2==0 meaning when input is even).

    Then you currently have two "logic blocks" in your structure initialization: one for odd rows and one for even rows.

    1. For even rows (corridors in your maze), just fill up the first and last column.

    2. Now for your odd rows, you want to alternate the "door", don't you? You need a "switch/interruptor" which indicates the state: door at the column index 2 (beginning of the row) or index N-1 (end of the row). Have a condition checking the row index or have a boolean which will do just fine. With a boolean, change its state at each odd row process. Then when your boolean is true, place your door at the beginning of the row, else at the end. Just don't forget to not do it for the last row if you want it closed.