Advertisement
Garro

Drunk Walk (w/o 2x2 Rooms)

May 6th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.util.*;
  2. public class drunk_walk_1{
  3.     public static void main(String[] args) {
  4.         int height = 15;
  5.         int width = 15;
  6.         int rooms = 46;
  7.         int matrix[][] = configMatrix(height,width,rooms);
  8.         printMatrix(matrix,height,width);
  9.     }
  10.  
  11.     public static void printMatrix(int matrix[][], int height, int width){
  12.         for (int i=0;i<height ;i++ ) {
  13.             for (int j=0;j<width ;j++ ) {
  14.                 System.out.print(matrix[i][j]);
  15.             }
  16.             System.out.println();
  17.         }
  18.     }
  19.  
  20.     public static int[][] configMatrix(int height, int width, int rooms){
  21.         int matrix[][] = new int[height][width];
  22.  
  23.         for (int i=0;i<height ;i++ ) {                          /*se llena con espacios default*/
  24.                 for (int j=0;j<width ;j++ ) {
  25.                         matrix[i][j]= 0;
  26.                 }
  27.         }
  28.  
  29.         Random random = new Random();                           /*Un poco de ajustes*/
  30.         int minx = (int)(width/3);
  31.         int miny = (int)(height/3);
  32.         int maxx = (int)(width-minx);
  33.         int maxy = (int)(height-miny);
  34.  
  35.         int x = minx + random.nextInt(maxx-minx);
  36.         int y = miny + random.nextInt(maxy-miny);
  37.  
  38.         System.out.println(x + "," + y);                        /*Sala por la cual parte el algoritmo*/
  39.  
  40.         matrix[y][x] = 1;
  41.         int roomsCount = 1;
  42.  
  43.         while (roomsCount < rooms){                             /*Generador de salas*/
  44.             int r = random.nextInt(4);
  45.             if(r==0){               /*NORTE*/
  46.                 if(y-2>=0){
  47.                     y=y-1;
  48.                 }
  49.             }
  50.             if(r==1){               /*SUR*/
  51.                 if(y+2<height){
  52.                     y=y+1;
  53.                 }
  54.             }
  55.             if(r==2){               /*ESTE*/
  56.                 if(x-2>=0){
  57.                     x=x-1;
  58.                 }
  59.             }
  60.             if(r==3){               /*OESTE*/
  61.                 if(x+2<width){
  62.                     x=x+1;
  63.                 }
  64.             }
  65.  
  66.             if(matrix[y][x]==0){        //Entra si esta ubicado en pared.
  67.                 if((matrix[y+1][x]==1 && matrix[y][x+1]==1 && matrix[y+1][x+1]==1)      //Revisa SE
  68.                     || (matrix[y+1][x]==1 && matrix[y][x-1]==1 && matrix[y+1][x-1]==1)  //Revisa SO
  69.                     || (matrix[y-1][x]==1 && matrix[y][x+1]==1 && matrix[y-1][x+1]==1)  //Revisa NE
  70.                     || (matrix[y-1][x]==1 && matrix[y][x-1]==1 && matrix[y-1][x-1]==1)){//Revisa NO
  71.                                         //Si se cumple el if, la matriz vuelve hacia atras.
  72.                     if(r==0){               /*NORTE*/
  73.                         y=y+1;
  74.                     }
  75.                     if(r==1){               /*SUR*/
  76.                             y=y-1;
  77.                     }
  78.                     if(r==2){               /*ESTE*/
  79.                             x=x+1;
  80.                     }
  81.                     if(r==3){               /*OESTE*/
  82.                             x=x-1;
  83.                     }
  84.                 }else{                      //Si no se cumple el if, convierte pared en piso.
  85.                     matrix[y][x]=1;
  86.                     roomsCount = roomsCount + 1;
  87.                 }
  88.             }
  89.         }return matrix;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement