Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class drunk_walk_1{
- public static void main(String[] args) {
- int height = 15;
- int width = 15;
- int rooms = 46;
- int matrix[][] = configMatrix(height,width,rooms);
- printMatrix(matrix,height,width);
- }
- public static void printMatrix(int matrix[][], int height, int width){
- for (int i=0;i<height ;i++ ) {
- for (int j=0;j<width ;j++ ) {
- System.out.print(matrix[i][j]);
- }
- System.out.println();
- }
- }
- public static int[][] configMatrix(int height, int width, int rooms){
- int matrix[][] = new int[height][width];
- for (int i=0;i<height ;i++ ) { /*se llena con espacios default*/
- for (int j=0;j<width ;j++ ) {
- matrix[i][j]= 0;
- }
- }
- Random random = new Random(); /*Un poco de ajustes*/
- int minx = (int)(width/3);
- int miny = (int)(height/3);
- int maxx = (int)(width-minx);
- int maxy = (int)(height-miny);
- int x = minx + random.nextInt(maxx-minx);
- int y = miny + random.nextInt(maxy-miny);
- System.out.println(x + "," + y); /*Sala por la cual parte el algoritmo*/
- matrix[y][x] = 1;
- int roomsCount = 1;
- while (roomsCount < rooms){ /*Generador de salas*/
- int r = random.nextInt(4);
- if(r==0){ /*NORTE*/
- if(y-2>=0){
- y=y-1;
- }
- }
- if(r==1){ /*SUR*/
- if(y+2<height){
- y=y+1;
- }
- }
- if(r==2){ /*ESTE*/
- if(x-2>=0){
- x=x-1;
- }
- }
- if(r==3){ /*OESTE*/
- if(x+2<width){
- x=x+1;
- }
- }
- if(matrix[y][x]==0){ //Entra si esta ubicado en pared.
- if((matrix[y+1][x]==1 && matrix[y][x+1]==1 && matrix[y+1][x+1]==1) //Revisa SE
- || (matrix[y+1][x]==1 && matrix[y][x-1]==1 && matrix[y+1][x-1]==1) //Revisa SO
- || (matrix[y-1][x]==1 && matrix[y][x+1]==1 && matrix[y-1][x+1]==1) //Revisa NE
- || (matrix[y-1][x]==1 && matrix[y][x-1]==1 && matrix[y-1][x-1]==1)){//Revisa NO
- //Si se cumple el if, la matriz vuelve hacia atras.
- if(r==0){ /*NORTE*/
- y=y+1;
- }
- if(r==1){ /*SUR*/
- y=y-1;
- }
- if(r==2){ /*ESTE*/
- x=x+1;
- }
- if(r==3){ /*OESTE*/
- x=x-1;
- }
- }else{ //Si no se cumple el if, convierte pared en piso.
- matrix[y][x]=1;
- roomsCount = roomsCount + 1;
- }
- }
- }return matrix;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement