结果:0
程序:
public class ConstructionOfMaze{
private int row ;
private int column; 
public ConstructionOfMaze(){
row = 0 ;
column = 0 ;
}

public ConstructionOfMaze(int row ,int column){
this.row = row ;
this.column = column ; 
}
//Initially, input 1 into MazeMap[][], 1 stands for wall of maze 

public int getRow(){
return row ;
}

public int getColumn(){
return column;
}
//establish path of maze for robot   public void setMaze(int row ,int column){
int [][]MazeMap = new int[row][column];
//setMazeWall
for (int i = 0 ;i < row -1 ; i ++ ){
for (int j = 0 ; j < column - 1 ; j++ ){
MazeMap[i][j] =  1 ; 
}
}

double randomDirection = Math.random() * 4 ; 
// randomDirection stands for direction
// 0< randomDirection < 1 for east 
// 1< randomDirection < 2 for south 
// 2< randomDirection < 3 for west 
// 3< randomDirection < 4 for north 

   for (int i = 0 ; i < row - 1 ; i++ ){
     for (int j = 0 ; j < column - 1 ; j ++ ){
     if ( randomDirection < 1 && j != column -1 ) {
     MazeMap[i][ j + 1 ] = 0 ;
     }
     if ( randomDirection >= 1 && randomDirection < 2 && i != row - 1 ){
     MazeMap[i + 1][j ] = 0 ;
     }
     if ( randomDirection >= 2 && randomDirection < 3 && j != 0 ){
     MazeMap[i][ j - 1 ] = 0 ;
     }
         if ( randomDirection >= 3 && randomDirection < 4 && i != 0 ){
     MazeMap[i -1][j] = 0 ;
     }
     }
     }
   for (int i = 0 ; i < row - 1; i++ ){
for (int j = 0 ; j < column - 1 ; j++){
System.out.print(MazeMap[i][j]);
if (i > row)
System.out.println();
}
}
   }
//test the construction of maze ,checking random maze map 
public static void main(String[] args){

ConstructionOfMaze test = new ConstructionOfMaze(2,2) ;

test.setMaze(test.getRow(),test.getColumn()) ;
//这里的意思是通过行和列这两个参数付给setMaze方法里的二维数组,然后显示出来
}
}
我建了constructionOfMaze 这个类,目的是随机建造迷宫地图,用字数字0代表路,用1代表墙,可能是我的参数传递出了问题,但是我还是没有解决,望大家帮忙!
结果:就是一个 0 ,所以我怀疑是不是这个二维数组根本就没有得到参数阿?

解决方案 »

  1.   

    我的建造迷宫的类里面除了构造函数外,那个setMaze的方法意思是先将二维数组全赋为1,然后由随机函数来为迷宫造路,“0“为路。但是结果不对,二维数组怀疑是没有初始化??
    谢谢大家乐!
      

  2.   

    问题出在循环条件判断上,i<row-1和j<column-1改为i<row和j<column即可
      

  3.   

    public class ConstructionOfMaze{
    private int row ;
    private int column; 
    public ConstructionOfMaze(){
    row = 0 ;
    column = 0 ;
    }

    public ConstructionOfMaze(int row ,int column){
    this.row = row ;
    this.column = column ; 
    }
    //Initially, input 1 into MazeMap[][], 1 stands for wall of maze 

    public int getRow(){
    return row ;
    }

    public int getColumn(){
    return column;
    }
    //establish path of maze for robot   public void setMaze(int row ,int column){
    int [][]MazeMap = new int[row][column];
    //setMazeWall
    for (int i = 0 ;i < row  ; i ++ ){
    for (int j = 0 ; j < column  ; j++ ){
    MazeMap[i][j] =  1 ; 
    }
    //System.out.println();
    }

    //double randomDirection = Math.random() * 20 ; 
    // randomDirection stands for direction
    // 0< randomDirection < 1 for east 
    // 1< randomDirection < 2 for south 
    // 2< randomDirection < 3 for west 
    // 3< randomDirection < 4 for north 

       for (int i = 0 ; i < row  ; i++ ){
         for (int j = 0 ; j < column  ; j ++ ){
         double randomDirection = Math.random() * 20 ;
         if ( randomDirection >= 4 && randomDirection<5&&j != column -1 ) {
         MazeMap[i][ j + 1 ] = 0 ;
         }
         if ( randomDirection >=9&&randomDirection< 10  && i != row - 1 ){
         MazeMap[i + 1][j ] = 0 ;
         }
         if ( randomDirection >=14 && randomDirection<15&&j != 0 ){
         MazeMap[i][ j - 1 ] = 0 ;
         }
             if ( randomDirection >=19 && randomDirection<20&&i != 0 ){
         MazeMap[i -1][j] = 0 ;
         }
         }
         }
       for (int i = 0 ; i < row ; i++ ){
    for (int j = 0 ; j < column  ; j++){
    System.out.print(MazeMap[i][j]);


    }
    System.out.println();
    }
       }
    //test the construction of maze ,checking random maze map 
    public static void main(String[] args){

    ConstructionOfMaze test = new ConstructionOfMaze(10,10) ;

    test.setMaze(test.getRow(),test.getColumn()) ;
    //这里的意思是通过行和列这两个参数付给setMaze方法里的二维数组,然后显示出来
    }
    }
    这是我给修改的程序,你把随即数randomDirection放到
    for(....)
    {for(.....){
    double ra....=Math.random()*20;
    }}
    还有,我把*4改成*20了,里面的条件也改了,如果找你那样的话,所有的随机数都会在0到4之间,你应该从中区四个数段,而不是区全部的区间,我改成20,区间为(0,20)我去了这样四个区间[4,5),[9,10),[14,15),[19,20)
    只是一些个人的看法,不好别介意,我也是个新手,以后多交流,呵呵
      

  4.   

    稍微修改了一下你程序中一些晦涩的地方,运行一次随机得到下面结果:11010
    01000
    11010
    00010
    00011public class ConstructionOfMaze{
    private int row;
    private int column;
    private int [][]MazeMap;

    public ConstructionOfMaze(int row ,int column){
    this.row = row;
    this.column = column;
    MazeMap = new int [row][column];
    }
    //Initially, input 1 into MazeMap[][], 1 stands for wall of maze 
    //establish path of maze for robot public void setMaze(){
    //setMazeWall
    for (int i = 0 ;i < row ; i ++ ){
    for (int j = 0 ; j < column; j++ ){
    MazeMap[i][j] =  1 ; 
    }
    }
    // randomDirection stands for direction
    // 0< randomDirection < 1 for east 
    // 1< randomDirection < 2 for south 
    // 2< randomDirection < 3 for west 
    // 3< randomDirection < 4 for north
       for (int i = 0 ; i < row; i++ ){
         for (int j = 0 ; j < column; j ++ ){
         double randomDirection = Math.random() * 4;
         if ( randomDirection < 1 && j != column -1 ) {
         MazeMap[i][ j + 1 ] = 0 ;
         }
         if ( randomDirection >= 1 && randomDirection < 2 && i != row - 1 ){
         MazeMap[i + 1][j ] = 0 ;
         }
         if ( randomDirection >= 2 && randomDirection < 3 && j != 0 ){
         MazeMap[i][ j - 1 ] = 0 ;
         }
             if ( randomDirection >= 3 && randomDirection < 4 && i != 0 ){
         MazeMap[i -1 ][j] = 0 ;
         }
         }
         }
       for (int i = 0 ; i < row; i++ ){
    for (int j = 0 ; j < column; j++)
    System.out.print(MazeMap[i][j]);
    System.out.println();
       }
    }
    //test the construction of maze ,checking random maze map 
    public static void main(String[] args){
    ConstructionOfMaze test = new ConstructionOfMaze(5,5);
    test.setMaze();
    //这里的意思是通过行和列这两个参数付给setMaze方法里的二维数组,然后显示出来
    }
    }
      

  5.   

    for(int i=0;i<row;i++)
                for(int j=0;j<column;j++)
                    maze[i][j]=new Random().nextInt(2);
      

  6.   

    /*
     * Created on 2005/11/17
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package A;/**
     * @author victim
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class ConstructionOfMaze {
    private int row ;
    private int column; 
    public ConstructionOfMaze(){
    row = 0 ;
    column = 0 ;
    }

    public ConstructionOfMaze(int row ,int column){
    this.row = row ;
    this.column = column ; 
    }
    //Initially, input 1 into MazeMap[][], 1 stands for wall of maze 

    public int getRow(){
    return row ;
    }

    public int getColumn(){
    return column;
    }
    //establish path of maze for robot   public void setMaze(int row ,int column){
    int [][]MazeMap = new int[row][column];
    //setMazeWall
    for (int i = 0 ;i < row ; i ++ ){
    for (int j = 0 ; j < column ; j++ ){
    MazeMap[i][j] =  1 ; 
    }
    }

    // randomDirection stands for direction
    // 0< randomDirection < 1 for east 
    // 1< randomDirection < 2 for south 
    // 2< randomDirection < 3 for west 
    // 3< randomDirection < 4 for north 

       for (int i = 0 ; i < row ; i++ ){
         for (int j = 0 ; j < column ; j ++ ){
         double randomDirection = Math.random() * 4 ; 
         if ( randomDirection < 1 && j != column -1 ) {
         MazeMap[i][ j + 1 ] = 0 ;
         }
         if ( randomDirection >= 1 && randomDirection < 2 && i != row - 1 ){
         MazeMap[i + 1][j ] = 0 ;
         }
         if ( randomDirection >= 2 && randomDirection < 3 && j != 0 ){
         MazeMap[i][ j - 1 ] = 0 ;
         }
             if ( randomDirection >= 3 && randomDirection < 4 && i != 0 ){
         MazeMap[i -1][j] = 0 ;
         }
         }
       }
       for (int i = 0 ; i < row ; i++ ){
        for (int j = 0 ; j < column  ; j++){
    System.out.print(MazeMap[i][j]);
    }
    System.out.println();
       }
       } //test the construction of maze ,checking random maze map 
    public static void main(String[] args){

    ConstructionOfMaze test = new ConstructionOfMaze(5,5) ;

    test.setMaze(test.getRow(),test.getColumn()) ;
    //这里的意思是通过行和列这两个参数付给setMaze方法里的二维数组,然后显示出来
    }
    }是啊,跟楼上的一样啊
      

  7.   

    /*
     * Created on 2005/11/17
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package A;/**
     * @author victim
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class ConstructionOfMaze {
    private int row ;
    private int column; 
    public ConstructionOfMaze(){
    row = 0 ;
    column = 0 ;
    }

    public ConstructionOfMaze(int row ,int column){
    this.row = row ;
    this.column = column ; 
    }
    //Initially, input 1 into MazeMap[][], 1 stands for wall of maze 

    public int getRow(){
    return row ;
    }

    public int getColumn(){
    return column;
    }
    //establish path of maze for robot   public void setMaze(int row ,int column){
    int [][]MazeMap = new int[row][column];
    //setMazeWall
    for (int i = 0 ;i < row ; i ++ ){
    for (int j = 0 ; j < column ; j++ ){
    MazeMap[i][j] = 1 ; 
    }
    }
    MazeMap[0][0] = 0 ;
    // randomDirection stands for direction
    // 0< randomDirection < 1 for east 
    // 1< randomDirection < 2 for south 
    // 2< randomDirection < 3 for west 
    // 3< randomDirection < 4 for north 

       for (int i = 0 ; i < row ; i++ ){
         for (int j = 0 ; j < column ; j ++ ){
         boolean flag = true;
         for(;flag;){
         flag = false;
         double randomDirection = Math.random() * 4 ; 
         if ( randomDirection < 1 && j != column -1 ) {
         MazeMap[i][ j + 1 ] = 0 ;
         }else if ( randomDirection >= 1 && randomDirection < 2 && i != row - 1 ){
         MazeMap[i + 1][j ] = 0 ;
         }else if ( randomDirection >= 2 && randomDirection < 3 && j != 0 ){
         MazeMap[i][ j - 1 ] = 0 ;
         }else if ( randomDirection >= 3 && randomDirection < 4 && i != 0 ){
         MazeMap[i -1][j] = 0 ;
         }else{
         flag = true;
         }
         }
         }
       }
       for (int i = 0 ; i < row ; i++ ){
        for (int j = 0 ; j < column  ; j++){
    System.out.print(MazeMap[i][j] + (j == column-1?"":" "));
    }
    System.out.println();
       }
       } //test the construction of maze ,checking random maze map 
    public static void main(String[] args){

    ConstructionOfMaze test = new ConstructionOfMaze(5,5) ;

    test.setMaze(test.getRow(),test.getColumn()) ;
    //这里的意思是通过行和列这两个参数付给setMaze方法里的二维数组,然后显示出来
    }
    }
    这样会不会好些呢?
      

  8.   

    楼上的方案不错,按照楼上的意见,我又修改了一下程序
    程序结果:16*16 的迷宫图
    我现在在想,能不能把算法再改进一下,路旁边不要有路了,就是0000。。旁边不要再是零的行,这样显得路太宽了。?
    1000011000100100
    0000001110000100
    0111000010000000
    0001001000100010
    0000000001001100
    0000100000000100
    0000000000000100
    0111001110000011
    0000001010001000
    0011010010001001
    1011100000001111
    0110001110010000
    0100010010010011
    0100010001000010
    0100101010010000
    1001101010010010
    public class ConstructionOfMaze{
    private int [][] MazeMap ;
    private int row ;
    private int column; 
    public ConstructionOfMaze(){
    row = 0 ;
    column = 0 ;
    MazeMap = new int[row][column];
    }

    public ConstructionOfMaze(int row ,int column){
    this.row = row ;
    this.column = column ;
    MazeMap = new int[row][column];
    }
    //Initially, input 1 into MazeMap[][], 1 stands for wall of maze 

    public int getRow(){
    return row ;
    }

    public int getColumn(){
    return column;
    }
    //establish path of maze for robot  
    public int[][] setMaze(int row ,int column){
    //setMazeWall
    for (int i = 0 ;i < row  ; i ++ ){
    for (int j = 0 ; j < column  ; j++ ){
    MazeMap[i][j] =  1 ; 
    }
    }

    //double randomDirection = Math.random() * 4 ; 
    // randomDirection stands for direction
    // 0< randomDirection < 1 for east 
    // 1< randomDirection < 2 for south 
    // 2< randomDirection < 3 for west 
    // 3< randomDirection < 4 for north 

        for (int i = 0 ; i < row ; i++ ){
         for (int j = 0 ; j < column ; j ++ ){
         boolean flag = true;
         for(;flag;){
         flag = false;
         double randomDirection = Math.random() * 4 ; 
         if ( randomDirection < 1 && j != column -1 ) {
         MazeMap[i][ j + 1 ] = 0 ;
         }else if ( randomDirection >= 1 && randomDirection < 2 && i != row - 1 ){
         MazeMap[i + 1][j ] = 0 ;
         }else if ( randomDirection >= 2 && randomDirection < 3 && j != 0 ){
         MazeMap[i][ j - 1 ] = 0 ;
         }else if ( randomDirection >= 3 && randomDirection < 4 && i != 0 ){
         MazeMap[i -1][j] = 0 ;
         }else{
         flag = true;
         }
         }
         }
       }      return MazeMap ;
     }
    //test the construction of maze ,checking random maze map 
    public static void main(String[] args){

    ConstructionOfMaze test = new ConstructionOfMaze(16,16) ;
    int [][] MazeMap = test.setMaze(test.getRow(),test.getColumn()) ;
    for (int i = 0 ; i < test.row; i++ ){
    for (int j = 0 ; j < test.column ; j++){
    System.out.print(MazeMap[i][j]);
    if (j == test.getColumn() - 1 )
    System.out.println();
    }
    }
    }
    }

      

  9.   

    算法还需要修改,但是下一步准备把字母显示迷宫,改为图像显示迷宫,拟用applet显示,那个东东画图好像挺方便的
      

  10.   

    数字运行结果: 18*18 迷宫地图
    0:路      1:墙     2 :出口 (实际上,我这里的算法设计不得当,我的本来的目的是通过自己定义有几个出口在数组的边缘来随机建几个出口,在这里,我的主函数里定义的是3个出口,但是结果却是这样的???) 222222222222222222
    200000000001100000
    201000001001100000
    200001101000100000
    200000100000001010
    201011110001010010
    211010000011000000
    210001000110011000
    200010010100100000
    200001001100001000
    210101101001001000
    200100000000010001
    211100010001000111
    211010011001000000
    200001000000110111
    200001100000110001
    200000001000000001
    201111001001001001还有根据需要,我要把文字的迷宫转换成图像显示,比如用GUI实现,或者APPLET实现,如果用后者的话,我目前还没有办法想出怎么把我这个数组的参数输入到applet程序里,因为APPLET里的画图函数是 public void paint(Graphics g) 这里的对象Graphics是抽象的,如果自己建一个新类来继承这个方法的话,还是不能将数组内容输入到继承的函数里。所以自己选择了GUI,但是结果有误,
    代码如下:
    public class ConstructionOfMaze{
    private int [][] MazeMap ;
    private int row ;
    private int column; 
    private int numberOfExit;
    public ConstructionOfMaze(){
    row = 0 ;
    column = 0 ;
    numberOfExit = 0;
    MazeMap = new int[row][column];
    }

    public ConstructionOfMaze(int row ,int column, int numberOfExit ){
    this.row = row ;
    this.column = column ;
    this.numberOfExit = numberOfExit ;
    MazeMap = new int[row][column];
    }
    //Initially, input 1 into MazeMap[][], 1 stands for wall of maze 

    public int getRow(){
    return row ;
    }

    public int getColumn(){
    return column;
    }
    public int getNumberOfExit(){
        return numberOfExit ;
    } //establish path of maze for robot  
    public int[][] setMaze(int row ,int column ,int numberOfExit){
    //setMazeWall
    for (int i = 0 ;i < row  ; i ++ ){
    for (int j = 0 ; j < column  ; j++ ){
    MazeMap[i][j] =  1 ; 
    }
    }

    //double randomDirection = Math.random() * 4 ; 
    // randomDirection stands for direction
    // 0< randomDirection < 1 for east 
    // 1< randomDirection < 2 for south 
    // 2< randomDirection < 3 for west 
    // 3< randomDirection < 4 for north 

        for (int i = 0 ; i < row ; i++ ){
         for (int j = 0 ; j < column ; j ++ ){
         boolean flag = true;
         for(;flag;){
         flag = false;
         double randomDirection = Math.random() * 4 ; 
         if ( randomDirection < 1 && j != column -1 ) {
         MazeMap[i][ j + 1 ] = 0 ;
         }else if ( randomDirection >= 1 && randomDirection < 2 && i != row - 1 ){
         MazeMap[i + 1][j ] = 0 ;
         }else if ( randomDirection >= 2 && randomDirection < 3 && j != 0 ){
         MazeMap[i][ j - 1 ] = 0 ;
         }else if ( randomDirection >= 3 && randomDirection < 4 && i != 0 ){
         MazeMap[i -1][j] = 0 ;
         }else{
         flag = true;
         }
         }
         }
       }
        //set exits of maze
       int k= 0 ;
       for (int i =0 ; i < row ; i++  )
       for (int j =0 ; j < column ; j++ ){
       if (i == 0 ||j == 0 || i == row || j == column ){
       MazeMap[i][j] = 2 ;
       k ++ ;
       
       }
       }
       while (k == numberOfExit )
               break;      return MazeMap ;
     }
    //test the construction of maze ,checking random maze map 
    public static void main(String[] args){

    ConstructionOfMaze test = new ConstructionOfMaze(18, 18, 3) ;
    int [][] MazeMap = test.setMaze(test.getRow(), test.getColumn(), test.getNumberOfExit()) ;
    for (int i = 0 ; i < 18 ; i++ ){
    for (int j = 0 ; j < 18 ; j++){
    System.out.print(MazeMap[i][j]);
    if (j == test.getColumn() - 1 )
    System.out.println();
    }
    }
    }
    }

    //设置一个迷宫的FRAME类 , 将panel类嵌到frame里,通过容器
    import java.awt.*;
    import javax.swing.*;public class MazeFrame extends JFrame{
    private static MazePanel myDrawing ;
    public MazeFrame(){
    setTitle("The map of maze");
    setSize(500,500);
    Container contentPane = this.getContentPane();
    myDrawing = new MazePanel();
    contentPane.add(myDrawing);
    }
    public MazePanel getMydrawing(){
    return myDrawing;
    }
    public static void main(String args[]){
    ConstructionOfMaze a = new ConstructionOfMaze(16,16,2);
    int [][] MazeMap = a.setMaze(a.getRow(),a.getColumn(),a.getNumberOfExit() );
    JFrame f = new MazeFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    myDrawing = new MazePanel();
    myDrawing.trypaint(MazeMap);
    }

    }
    // 这里,panel类里我本意是想设置一个新的函数可以将生成迷宫的数组的内容读到trypaint()里,然后显示迷宫图像,通过panel的画图,但是不行,Frame 能出来,但是上面什么也没有,就是说,panel根本没有起作用,怎么解决? 希望大家帮忙!!谢谢!import java.awt.*;import javax.swing.JPanel;
    public class MazePanel extends JPanel{ 

    private Graphics g; public void trypaint(int[][] MazeMap){

    super.paint(g);
      for (int i = 0 ; i< MazeMap.length ; i += 10)
      for(int j = 0 ; j < MazeMap[i].length ; j += 10 ){
           
           
             g.drawRect(i,j,10,10);
             if (MazeMap[i][j] == 0){
            g.fillRect(i,j,10,10);
               g.setColor(Color.red);
             }
      }
     //我画图的意思是 如果矩阵某个值等于0,也就是路的话,划出一个10*10的矩形,并填充颜色
      
    }
       // public void paint(Graphics g){
       //   g.setColor(Color.red);
          g.drawRect(1,1,20,20);
       // }
      //这里如果把注释符号去掉,就可以显示这个红边的正方形,不取掉,则frame里面什么也没有,这是怎么回事? }
    高手们,帮我想一想啊!!!!!