android中迷宫生成的递归分割算法
我有生成完美迷宫的递归除法算法的java代码,但问题是我想要实现它,并且找不到在android中打印生成的迷宫的方法。。。因为它为垂直线“|”和水平线“-”生成一个字符数组。 我试着在两个数组上循环,如果“|”的话画一条垂直线,如果“-”的话画一条水平线,但很明显,这不起作用,因为我无法在android活动中设置线的正确位置。 那么,我如何设置位置来精确绘制生成的迷宫呢? 还是他们的算法在android上的另一个实现 这就是我使用的实现:android中迷宫生成的递归分割算法,android,algorithm,recursion,implementation,division,Android,Algorithm,Recursion,Implementation,Division,我有生成完美迷宫的递归除法算法的java代码,但问题是我想要实现它,并且找不到在android中打印生成的迷宫的方法。。。因为它为垂直线“|”和水平线“-”生成一个字符数组。 我试着在两个数组上循环,如果“|”的话画一条垂直线,如果“-”的话画一条水平线,但很明显,这不起作用,因为我无法在android活动中设置线的正确位置。 那么,我如何设置位置来精确绘制生成的迷宫呢? 还是他们的算法在android上的另一个实现 这就是我使用的实现: package com.jforeach.mazegam
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;
}
}
package com.jforeach.mazegame;
导入java.util.*;
导入android.util.Log;
类递归除法
{
静态最终字符VWALL='|';
静态最终字符HWALL='-';
静态最终字符路径=“”;
int行;
int cols;
int act_行;
国际法;
字符[][]板;
公共递归除法(整数行,整数列)
{
//初始化实例变量
行=行*2+1;
cols=col*2+1;
行=行;
act_cols=col;
board=新字符[行][列];
//将迷宫设置为空
/*用于(int i=0;i宽度)
水平分割(左、右、上、下);
否则如果(高度==宽度){
Random rand=新的Random();
布尔值pickOne=rand.nextBoolean();
if(pickOne)
垂直分割(左、右、上、下);
其他的
水平分割(左、右、上、下);
}
}否则如果(宽度>2和高度这个迷宫生成算法工作得很好。请注意,finalboard
数组中有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("");
}
}
public void printMaze2()
{
对于(int i=0;我很想知道你的答案,但在控制台中打印迷宫不是问题,我的问题是我想在android活动中绘制迷宫。我尝试过使用drawLine功能,但这个方法失败了,我想是因为android活动的位置设置不好。Rami,你是否尝试过绘制矩形而不是线条。类似这样的:for(inti=0;iOHH)非常好,非常感谢。它的打印与现在生成的完全一样,但有些小,但这是我最不担心的!非常感谢:d*10
,和+10
定义矩形的尺寸。将其更改为某个变量以调整矩形的大小。