Java 枚举型数组迷宫
我正在制作一个带有枚举类型的迷宫游戏,以保存墙、开放空间(等)的值,我不确定为什么此代码不起作用,我正在尝试创建一个新的板,并将所有内容设置为打开,然后遍历并随机设置数组中的点的值Java 枚举型数组迷宫,java,enums,multidimensional-array,maze,Java,Enums,Multidimensional Array,Maze,我正在制作一个带有枚举类型的迷宫游戏,以保存墙、开放空间(等)的值,我不确定为什么此代码不起作用,我正在尝试创建一个新的板,并将所有内容设置为打开,然后遍历并随机设置数组中的点的值 maze = new Cell[row][col]; for (int r = 0; r < maze.length; r++) { for (int c = 0; c < maze.length; c++) maze[r][c].setType(CellType.OPEN);
maze = new Cell[row][col];
for (int r = 0; r < maze.length; r++) {
for (int c = 0; c < maze.length; c++)
maze[r][c].setType(CellType.OPEN);
}
Random randomMaze = new Random();
for (int ran = 0; ran <= numWalls ; ran++){
maze[randomMaze.nextInt(maze.length)][randomMaze.nextInt(maze.length)].setType(CellType.WALL);
}
maze=新单元格[行][col];
对于(int r=0;r 对于(int-ran=0;ran我认为,您的内部循环应该是
for (int c = 0; c < maze[r].length; c++)
for(int c=0;c
…使用[r]
不过我还没有试过。我认为你的迷宫是一个很好的课程候选。类似这样的东西应该有用:
import java.util.Random;
public class Maze {
private int[][] mMaze;
private int mRows;
private int mCols;
//enums here:
public static int CELL_TYPE_OPEN = 0;
public static int CELL_TYPE_WALL = 1;
public Maze(int rows, int cols){
mRows = rows;
mCols = cols;
mMaze = new int[mRows][mCols];
for (int r = 0; r < mRows; r++) {
for (int c = 0; c < mCols; c++) {
mMaze[r][c] = Maze.CELL_TYPE_OPEN;
}
}
}
public void RandomizeMaze(int numWalls){
Random randomMaze = new Random();
for (int ran = 0; ran <= numWalls ; ran++){
mMaze[randomMaze.nextInt(mRows)][randomMaze.nextInt(mCols)]=(Maze.CELL_TYPE_WALL);
}
}
import java.util.Random;
公共类迷宫{
私有int[][]mMaze;
私人内勤;
私人国际联络处;
//此处列举:
公共静态整数单元格类型为0;
公共静态内部单元类型墙=1;
公共迷宫(整数行,整数列){
mRows=行;
mCols=cols;
mMaze=新整数[mRows][mCols];
对于(int r=0;r 对于(int-ran=0;ran这将实现您所说的。不确定您是否会得到您想要的迷宫类型:
import java.util.Random;
class Maze {
enum CellType {
open,wall;
}
Maze(int n) {
this.n=n;
maze=new CellType[n][n];
init();
}
private void init() {
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
maze[i][j]=CellType.open;
}
void randomize(int walls) {
init();
Random random=new Random();
for(int i=0;i<=walls;i++)
maze[random.nextInt(n)][random.nextInt(n)]=CellType.wall;
}
public String toString() {
StringBuffer sb=new StringBuffer();
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++)
switch(maze[i][j]) {
case open:
sb.append(' ');
break;
case wall:
sb.append('|');
break;
}
sb.append('\n');
}
return sb.toString();
}
final int n;
CellType[][] maze;
}
public class Main {
public static void main(String[] args) {
Maze maze=new Maze(5);
System.out.println(maze);
maze.randomize(4);
System.out.println(maze);
}
}
import java.util.Random;
班级迷宫{
枚举单元类型{
开放式,墙壁式;
}
迷宫(int n){
这个,n=n;
迷宫=新的细胞类型[n][n];
init();
}
私有void init(){
对于(int i=0;i