2014-09-28 87 views
0

我有用於生成完美迷宮的遞歸除法算法的java代碼,但問題是我想實現它,並且無法找到一種方法在android中打印生成的迷宮...因爲它會爲垂直線生成一個字符數組「|」另一個用於水平線「 - 」。 我試圖循環兩個數組,並繪製垂直線,如果「|」和一個水平線,如果「 - 」但顯然沒有工作,因爲我不能在android活動上設置行的正確位置。 那麼我如何設置完全按照生成的方式繪製迷宮呢? 或者是他們在android上的算法的另一個實現?在android中用於迷宮生成的遞歸除法算法

這是我使用的實現:

package com.jforeach.mazegame; 

import java.util.*; 
import android.util.Log; 

class RecursiveDivision 
{ 

    static final char VWALL = '|'; 
    static final char HWALL = '-'; 

    static final char MAZE_PATH = ' '; 

    int rows; 
    int cols; 
    int act_rows; 
    int act_cols; 

    char[][] board; 

    public RecursiveDivision(int row, int col) 
    { 

     //initialize instance variables 
     rows = row*2+1; 
     cols = col*2+1; 
     act_rows = row; 
     act_cols = col; 
     board = new char[rows][cols]; 

     //set the maze to empty  
    /* for(int i=0; i<rows; i++){ 
      for(int j=0; j<cols; j++){ 
       board[i][j] = MAZE_PATH; 
      } 
     }*/ 

     //make the outter walls 
     for(int i=0; i<rows; i++){ 
      board[i][0] = VWALL; 
      board[i][cols-1] = VWALL; 
     } 

     for(int i=0; i<cols; i++){ 
      board[0][i] = HWALL; 
      board[rows-1][i] = HWALL; 
     } 


    } 

    //storefront method to make the maze 
    public void makeMaze() 
    { 
     makeMaze(0,cols-1,0,rows-1); 
     makeOpenings(); 


    } 

    //behind the scences actual mazemaking 
    private void makeMaze(int left, int right, int top, int bottom) 
    { 
     int width = right-left; 
     int height = bottom-top; 

     //makes sure there is still room to divide, then picks the best 
     //direction to divide into 
     if(width > 2 && height > 2){ 

      if(width > height) 
       divideVertical(left, right, top, bottom); 

      else if(height > width) 
       divideHorizontal(left, right, top, bottom); 

      else if(height == width){ 
       Random rand = new Random(); 
       boolean pickOne = rand.nextBoolean(); 

       if(pickOne) 
        divideVertical(left, right, top, bottom); 
       else 
        divideHorizontal(left, right, top, bottom); 
      } 
     }else if(width > 2 && height <=2){ 
      divideVertical(left, right, top, bottom); 
     }else if(width <=2 && height > 2){ 
      divideHorizontal(left, right, top, bottom); 
     } 
    } 

    private void divideVertical(int left, int right, int top, int bottom) 
    { 
     Random rand = new Random(); 

     //find a random point to divide at 
     //must be even to draw a wall there 
     int divide = left + 2 + rand.nextInt((right-left-1)/2)*2; 

     //draw a line at the halfway point 
     for(int i=top; i<bottom; i++){ 
      board[i][divide] = VWALL; 
     } 

     //get a random odd integer between top and bottom and clear it 
     int clearSpace = top + rand.nextInt((bottom-top)/2) * 2 + 1; 

     board[clearSpace][divide] = MAZE_PATH; 

     makeMaze(left, divide, top, bottom); 
     makeMaze(divide, right, top, bottom); 
    } 

    private void divideHorizontal(int left, int right, int top, int bottom) 
    { 
     Random rand = new Random(); 

     //find a random point to divide at 
     //must be even to draw a wall there 
     int divide = top + 2 + rand.nextInt((bottom-top-1)/2)*2; 
     if(divide%2 == 1) 
      divide++; 

     //draw a line at the halfway point 
     for(int i=left; i<right; i++){ 
      board[divide][i] = HWALL; 
     } 

     //get a random odd integer between left and right and clear it 
     int clearSpace = left + rand.nextInt((right-left)/2) * 2 + 1; 

     board[divide][clearSpace] = MAZE_PATH; 

     //recur for both parts of the newly split section 
     makeMaze(left, right, top, divide); 
     makeMaze(left, right, divide, bottom); 
    } 

    public void makeOpenings(){ 

     Random rand = new Random(); //two different random number generators 
     Random rand2 = new Random();//just in case 

     //a random location for the entrance and exit 
     int entrance_row = rand.nextInt(act_rows-1) * 2 +1; 
     int exit_row = rand2.nextInt(act_rows-1) * 2 +1; 

     //clear the location 
     board[entrance_row][0] = MAZE_PATH; 
     board[exit_row][cols-1] = MAZE_PATH; 

    } 

    public void printMaze() 
    {   
     for(int i=0; i<rows; i++){ 
      for(int j=0; j<cols; j++){ 

       Log.d("MAZE", i +" "+ j+" "+ String.valueOf(board[i][j])); 

      } 
     } 
    } 


    public Maze getMaze() 
    { 
     Maze maze = convert(); 
     return maze; 
    } 
} 

在此先感謝。

回答

1

這個迷宮生成算法工作得很好。請注意,最終board數組中有4個可能的字符:管道,減號,空格和0 ASCII字符。我注意到牆之間確實沒有真正的區別,因爲你可以把它們當作塊。所以也許不應該繪製線條,而應繪製填充的矩形。看看這個功能,打印迷宮:

public void printMaze2() 
{   
    for(int i=0; i<rows; i++){ 
     for(int j=0; j<cols; j++){ 
      System.out.print((board[i][j])); 
     } 
     System.out.println(""); 
    } 
} 

public void printMaze3() 
{   
    for(int i=0; i<rows; i++){ 
     for(int j=0; j<cols; j++){ 
      if (board[i][j]==MAZE_PATH) System.out.print(" "); 
      else if (board[i][j]==VWALL) System.out.print("#"); 
      else if (board[i][j]==HWALL) System.out.print("#"); 
      else System.out.print(" "); // this last case is for \0 
     } 
     System.out.println(""); 
    } 
} 
+0

非常感謝您的回答,但在控制檯打印迷宮是沒有問題的,我的問題是我要畫它在android系統的活動。 我試着使用drawLine函數,但是這種方法失敗了,我認爲是因爲postions沒有很好地爲android活動設置。 – Rami 2014-09-29 08:29:37

+0

Rami,您是否嘗試繪製矩形而不是線條。就像這樣:for(int i = 0; i rostok 2014-09-29 09:17:02

+0

OHH工作得很好非常感謝..它的印刷完全像現在產生的一樣,但不知怎麼的小,但我最不用擔心!非常感謝:d – Rami 2014-09-29 09:50:28