zopiac

RandDun

Apr 13th, 2011
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Random;
  4. import character.Player;
  5.  
  6. public class RandDun {
  7.  
  8.     private static int worldbounds = 256;
  9.     private static boolean[][] world = new boolean [worldbounds][worldbounds];
  10.     private static int[][] material = new int [worldbounds][worldbounds];
  11.     private static Random randInt = new Random();
  12.     private static int ysight = 11;
  13.     private static int xsight = 20;
  14.     private static int sandstone;
  15.     private static int granite;
  16.     private static int iron;
  17.     private static int gold;
  18.     //  private static int x;
  19.     //  private static int y;
  20.  
  21.     public static void main(String[] args) {
  22.         generate();
  23.         validate();
  24.         place();
  25.         while (true) {
  26.             display();
  27.         }
  28.     }
  29.  
  30.    
  31.     public static void generate() {
  32.         int tile;
  33.         for (int v = 0; v < worldbounds; v++) {
  34.             for (int h = 0; h < worldbounds; h++) {
  35.                 /*Materials:
  36.                  * 0:20:Empty
  37.                  * 1:40:Granite
  38.                  * 2:20:Sandstone
  39.                  * 3:15:Iron
  40.                  * 4: 5:Gold
  41.                  */
  42.                 tile = randInt.nextInt(100);
  43.                 if (tile < 20)
  44.                     material[h][v]=0;
  45.                 else if (tile < 60)
  46.                     material[h][v]=1;
  47.                 else if (tile < 90)
  48.                     material[h][v]=2;
  49.                 else
  50.                     material[h][v]=3;
  51.                 //                  material[h][v]=4;
  52.                 if (randInt.nextInt(2) == 1)
  53.                     world[h][v]=true;
  54.                 else
  55.                     world[h][v]=false;
  56.             }
  57.         }
  58.     }
  59.  
  60.     public static void bake() {
  61.         for (int i = 0; i < 3; i++) {
  62.  
  63.             for (int v = 0; v < worldbounds; v++) {
  64.                 for (int h = 0; h < worldbounds; h++) {
  65.                     if (material[h][v]==3) {
  66.                         if (randInt.nextInt(3)==0) {
  67.                             material[h][v+1]=4;
  68.                         } if (randInt.nextInt(3)==0) {
  69.                             material[h][v-1]=4;
  70.                         } if (randInt.nextInt(3)==0) {
  71.                             material[h+1][v]=4;
  72.                         } if (randInt.nextInt(3)==0) {
  73.                             material[h-1][v]=4;
  74.                         }
  75.                     } else if (material[h][v]==1) {
  76.                         if (
  77.                                 (
  78.                                         material[h][v-1]==1 ||
  79.                                         material[h][v-1]==1
  80.                                 )&&(
  81.                                         material[h][v+1]==1 ||
  82.                                         material[h][v+1]==1
  83.                                 )&&(
  84.                                         material[h-1][v]==1 ||
  85.                                         material[h-1][v]==1
  86.                                 )||(
  87.                                         material[h+1][v]==1 ||
  88.                                         material[h+1][v]==1 //&&
  89.                                 )//randInt.nextInt(4)==0)
  90.                         )
  91.                             material[h][v]=4;
  92.  
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     public static void validate() {
  100.         for (int v = 1; v < worldbounds-1; v++) {
  101.             for (int h = 1; h < worldbounds-1; h++) {
  102.                 int adj = 0;
  103.                 for (int i = 0; i < 4; i++) {
  104.                     if (material[h+1][v]==0)
  105.                         adj++;
  106.                     if (material[h-1][v]==0)
  107.                         adj++;
  108.                     if (material[h][v+1]==0)
  109.                         adj++;
  110.                     if (material[h][v-1]==0)
  111.                         adj++;
  112.                 }
  113.                 if (adj <3)
  114.                     material[h][v]=1;
  115.             }
  116.         }
  117.     }
  118.  
  119.     public static void place() {
  120.         Player.setX(worldbounds/2);
  121.         Player.setY(worldbounds/2);
  122.         material[Player.getX()][Player.getY()]=0;
  123.         material[Player.getX()+1][Player.getY()]=0;
  124.         material[Player.getX()+1][Player.getY()+1]=0;
  125.         material[Player.getX()][Player.getY()+1]=0;
  126.         material[Player.getX()-1][Player.getY()+1]=0;
  127.         material[Player.getX()-1][Player.getY()]=0;
  128.         material[Player.getX()-1][Player.getY()-1]=0;
  129.         material[Player.getX()][Player.getY()-1]=0;
  130.         material[Player.getX()+1][Player.getY()-1]=0;
  131.     }
  132.  
  133.     public static void display() {
  134.         System.out.println();
  135.         System.out.print(
  136.                 "      Granite:" + Format.BIFormat(granite, 4) +
  137.                 "    Sandstone:" + Format.BIFormat(sandstone, 4) +
  138.                 "         Iron:" + Format.BIFormat(iron, 4) +
  139.                 "         Gold:" + Format.BIFormat(gold, 4)
  140.         );
  141.         System.out.println();
  142.         for (int v = 0; v < worldbounds; v++) {
  143.             for (int h = 0; h < worldbounds; h++) {
  144.                 if (
  145.                         v > Player.getY() - ysight &&
  146.                         v < Player.getY() + ysight &&
  147.                         h > Player.getX() - xsight &&
  148.                         h < Player.getX() + xsight) {
  149.                     if (v == Player.getY() && h == Player.getX())
  150.                         System.out.print("[]");
  151.                     else if (material[h][v]==0)
  152.                         System.out.print(".`");
  153.                     else if (material[h][v]==1)
  154.                         System.out.print("%%");
  155.                     else if (material[h][v]==2)
  156.                         System.out.print("##");
  157.                     else if (material[h][v]==3)
  158.                         System.out.print("&&");
  159.                     else if (material[h][v]==4)
  160.                         System.out.print("**");
  161.                 }
  162.             }
  163.             if (v < Player.getY() + ysight && v > Player.getY() - ysight)
  164.                 System.out.println("");
  165.         }
  166.     }
  167.  
  168.    
  169. }
Advertisement
Add Comment
Please, Sign In to add comment