Java 我怎样才能停止在ascii迷宫中打印一面墙的两面?
我已经写了一些代码为我生成迷宫。迷宫由(n x n)个单元组成,每个单元都有一个布尔值来表示一堵墙(北、南、东、西) 它工作正常,我编写了下面的函数来打印迷宫:Java 我怎样才能停止在ascii迷宫中打印一面墙的两面?,java,maze,Java,Maze,我已经写了一些代码为我生成迷宫。迷宫由(n x n)个单元组成,每个单元都有一个布尔值来表示一堵墙(北、南、东、西) 它工作正常,我编写了下面的函数来打印迷宫: public static void printMaze(Cell[][] maze) { for(int i = 0; i < maze.length; i++) { for(int j = 0; j < maze[i].length; j++)
public static void printMaze(Cell[][] maze)
{
for(int i = 0; i < maze.length; i++)
{
for(int j = 0; j < maze[i].length; j++)
{
System.out.print((maze[i][j].walls.get(Dir.NORTH)) ? "+--+" : "+ +");
}
System.out.println();
for(int j = 0; j < maze[i].length; j++)
{
System.out.print((maze[i][j].walls.get(Dir.WEST)) ? "|" : " ");
System.out.print(" ");
System.out.print((maze[i][j].walls.get(Dir.EAST)) ? "|" : " ");
}
System.out.println();
for(int j = 0; j < maze[i].length; j++)
{
System.out.print((maze[i][j].walls.get(Dir.SOUTH)) ? "+--+" : "+ +");
}
System.out.println();
}
}
我应该如何修改打印功能,使其看起来像:
+--+--+--+--+--+--+--+--+--+--+
| | | |
+--+ +--+--+ +--+--+ + +--+
| | | | | |
+ +--+--+ + + +--+--+--+ +
| | | | | | | |
+ + + +--+ + + + + + +
| | | | | | | |
+ + + + + +--+--+--+--+ +
| | | | | |
+ +--+--+--+--+--+ +--+ + +
| | | | | |
+ +--+ + + +--+--+ +--+ +
| | | | | | |
+--+--+--+ + + + + + + +
| | | | | | | |
+ + +--+ + + + +--+--+ +
| | | | | | | |
+ + + +--+--+--+ + + +--+
| | | |
+--+--+--+--+--+--+--+--+--+--+
public static void printMaze(final Cell[][] maze) {
for (int r = 0; r < maze.length; r++) {
final Cell[] row = maze[r];
printTop(row);
printMiddle(row);
if (r == maze.length - 1) {
printBottom(row);
}
}
}
private static void printBottom(final Cell[] row) {
for (final Cell cell : row) {
System.out.print(cell.walls.contains(Dir.SOUTH) ? "+--" : "+ ");
}
System.out.println("+");
}
private static void printMiddle(final Cell[] row) {
for (int c = 0; c < row.length; c++) {
final Cell cell = row[c];
System.out.print(cell.walls.contains(Dir.WEST) ? "| " : " ");
if (c == row.length - 1) {
System.out.println(cell.walls.contains(Dir.EAST) ? "|" : " ");
}
}
}
private static void printTop(final Cell[] row) {
for (final Cell cell : row) {
System.out.print(cell.walls.contains(Dir.NORTH) ? "+--" : "+ ");
}
System.out.println("+");
}
我担心当我最终开始使用实际图形而不是ascii来绘制迷宫时,我会面临类似的问题
如何修改printMaze方法,使其从第一个示例转到第二个示例
如果有人感兴趣,我的类用于生成这些的源代码是。因为单元格共享墙,您可以忽略一半的值。如果从最西北的单元开始,只测试南部和东部的墙,则可以绘制单墙迷宫。当然,迷宫的北墙和西墙必须完全封闭
免责声明:我没有认真考虑过,所以可能根本不起作用,但对我来说这听起来很合理。如果这个牢房的西面应该有一面墙,你需要这样做:“除非这个牢房在迷宫的边缘,否则不要在北面或西面打印墙。”,西边的牢房已经把它印成了自己的东墙
如果门/入口也在北墙上或西墙上,则可能需要对其进行特殊处理 仅打印北墙和西墙。正在路上的代码 我把墙改成了一套
public Set<Dir> walls = EnumSet.allOf(Dir.class);
要移除墙壁,请使用:
this.walls.remove(randDir);
randomNeighbor.walls.remove(randDir.opposite());
然后打印代码如下所示:
+--+--+--+--+--+--+--+--+--+--+
| | | |
+--+ +--+--+ +--+--+ + +--+
| | | | | |
+ +--+--+ + + +--+--+--+ +
| | | | | | | |
+ + + +--+ + + + + + +
| | | | | | | |
+ + + + + +--+--+--+--+ +
| | | | | |
+ +--+--+--+--+--+ +--+ + +
| | | | | |
+ +--+ + + +--+--+ +--+ +
| | | | | | |
+--+--+--+ + + + + + + +
| | | | | | | |
+ + +--+ + + + +--+--+ +
| | | | | | | |
+ + + +--+--+--+ + + +--+
| | | |
+--+--+--+--+--+--+--+--+--+--+
public static void printMaze(final Cell[][] maze) {
for (int r = 0; r < maze.length; r++) {
final Cell[] row = maze[r];
printTop(row);
printMiddle(row);
if (r == maze.length - 1) {
printBottom(row);
}
}
}
private static void printBottom(final Cell[] row) {
for (final Cell cell : row) {
System.out.print(cell.walls.contains(Dir.SOUTH) ? "+--" : "+ ");
}
System.out.println("+");
}
private static void printMiddle(final Cell[] row) {
for (int c = 0; c < row.length; c++) {
final Cell cell = row[c];
System.out.print(cell.walls.contains(Dir.WEST) ? "| " : " ");
if (c == row.length - 1) {
System.out.println(cell.walls.contains(Dir.EAST) ? "|" : " ");
}
}
}
private static void printTop(final Cell[] row) {
for (final Cell cell : row) {
System.out.print(cell.walls.contains(Dir.NORTH) ? "+--" : "+ ");
}
System.out.println("+");
}
publicstaticvoidprintmaze(最终单元格[][]迷宫){
对于(int r=0;r
(注意:从美学角度看,我更喜欢方向和随机方向。但这只是我;-)如果使用这种方法,则应仅为每个单元格存储两面墙(北墙和西墙);南墙和东墙只是相邻空间的北墙和西墙的多余副本,无论如何都将被忽略。迷宫的边界是自动围起来的。