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.
On top of GhostCat advices that you should definitely apply, considering your current code:
Then you currently have two "logic blocks" in your structure initialization: one for odd rows and one for even rows.
For even rows (corridors in your maze), just fill up the first and last column.
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.