완벽한 미로를 생성하는 재귀 적 분할 알고리즘에 대한 자바 코드가 있지만 문제는 구현하고 싶지 않으며 안드로이드에서 생성 된 미로를 인쇄하는 방법을 찾을 수 없습니다. 수직선 "|"에 대한 문자 배열을 생성하기 때문에 다른 하나는 가로선 "-"입니다. 두 배열에서 루프를 시도하고 "|" 수평선은 "-"하지만 안드로이드 활동에 대한 선의 정확한 위치를 설정할 수 없으므로 분명히 작동하지 않았습니다. 그러면 미로를 정확히 그려 내기 위해 위치를 설정할 수 있습니까? OR은 안드로이드의 알고리즘에 대한 또 다른 구현입니까? 사전에안드로이드에서 미로 생성을위한 재귀 적 분할 알고리즘
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;
}
}
감사 : 내가 사용하는 구현의
.
감사 답변에 대한 alot을하지만, 콘솔의 미로를 인쇄하는 것은 문제가되지 않습니다, 내 문제는 내가 안드로이드 활동을 그리려는 것입니다. 나는 drawLine 함수를 사용하여 시도했지만이 방법은 포스팅이 안드로이드 활동을 위해 잘 설정되지 않았기 때문에 실패했다고 생각합니다. – Rami
라미, 선 대신 사각형을 그리려고 했습니까? (int i = 0; i
rostok
OHH 매우 잘 작동했습니다. (보드 [i] [j] == HWALL) canvas.drawRect 고마워요 .. 지금은 생성 된 그대로 인쇄되었지만 어쨌든 작지만 저의 걱정은 없습니다! 감사합니다. d – Rami