Reputation: 35
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.
Upvotes: 2
Views: 643
Reputation: 187
@GhostCat ist right. Look at what you want to be the outcome:
@ @@@@@@@@@@@@@
@ @
@@@@@@@@@@@@@ @
@ @
@ @@@@@@@@@@@@@
@ @
@@@@@@@@@@@@@ @
@ @
@ @@@@@@@@@@@@@
@ @
@@@@@@@@@@@@@ @
@ @
@ @@@@@@@@@@@@@
@ @
@@@@@@@@@@@@@@@
Don't try to write the whole algorithm at once. Create it step by step.
1) Does your maze work with every number of lines?
2) Are there lines that differ completly from the other lines?
3) Is there a pattern that can be found in any of your lines?
4) Describe the pattern for the pathway-lines.
6) How much sorts of wall-lines can you identify?
7) Describe a pattern for each of the types of wall-lines.
etc.
...
at https://ideone.com/DBOJRy this works:
//import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//Scanner sc = new Scanner(System.in);
int dim = 16; //sc.nextInt();
System.out.println("Maze Dimension: " + dim);
//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;
baris -= (baris%2 !=0) ? 1 : 2;
for (initBaris = 0; initBaris < baris; initBaris++)
{
System.out.print(maze[initBaris][0] = '@');
if (initBaris % 2 != 1)
{
System.out.print(maze[initBaris][1] = (initBaris%4!=2)?' ':'@');
for (initKolom = 2; initKolom < kolom-2; initKolom++)
{
System.out.print(maze[initBaris][initKolom] = '@');
}
System.out.print(maze[initBaris][kolom-2] = (initBaris%4!=2)?'@':' ');
}
else
{
for (initKolom = 1; initKolom < kolom-1 ; initKolom++)
{
System.out.print(maze[initBaris][initKolom] = ' ');
}
}
System.out.print(maze[initBaris][kolom-1] = '@');
System.out.println();
}
for (initKolom = 0; initKolom < kolom ; initKolom++)
{
System.out.print(maze[baris][initKolom] = '@');
}
}
}
Upvotes: 2
Reputation: 140427
Obviously your "algorithm" to build the maze is insufficient. There are two ways to improve that:
One important thing to understand: you should separate the initialization of your array from printing it. You see - working an array to get its content right is much easier when you do not have to worry about printing it at the same time. So: use one or more steps to initialize the array - and then have a separate step to print it.
Finally: consider not making the maze an array of characters. Rather use boolean or an enum of yours. The fact that you later want to print empty and "taken" slots with different characters should not drive how the maze is internally represented.
Upvotes: 2
Reputation: 449
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.
Upvotes: 0