Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. public class TestMap
  2. {
  3. public static void main(String [] args)
  4. {
  5. // dungeon variables
  6. int mapWidth, mapHeight;
  7.  
  8. mapWidth = 5;
  9. mapHeight = 5;
  10.  
  11. int [][] myMap = new int [mapWidth][mapHeight];
  12. myMap [mapWidth/2][mapHeight/2] = 1;
  13. int myX = mapWidth/2, myY = mapWidth/2;
  14. int count = 1, pathLength = 8;
  15. boolean search, deadEndN, deadEndS, deadEndW, deadEndE;
  16.  
  17. // randomise dungeon
  18. for (int i = 0; i < pathLength; i++)
  19. {
  20. // reset vars for new search cycle
  21. search = true;
  22. deadEndN = false; deadEndS = false; deadEndW = false; deadEndE = false;
  23.  
  24. // keep randomising until new XY is different to old XY
  25. while (search)
  26. {
  27. int random = (int) (Math.random() * 4.0);
  28. System.out.println("random " + random);
  29. // new XY must be horizontally or vertically aligned
  30. switch (random)
  31. {
  32. // DUE EAST
  33. case 0:
  34. // value must be within array bounds
  35. if (myX+1 < mapWidth)
  36. {
  37. // only if myX++ would occupy zero/empty
  38. if (myMap[myX+1][myY] == 0)
  39. {
  40. myX++;
  41. search = false;
  42. }
  43. else
  44. {
  45. deadEndE = true;
  46. }
  47. }
  48. else
  49. {
  50. deadEndE = true;
  51. }
  52. break;
  53. // DUE WEST
  54. case 1:
  55. // value must be within array bounds
  56. if (myX-1 >= 0)
  57. {
  58. if (myMap[myX-1][myY] == 0)
  59. {
  60. myX--;
  61. search = false;
  62. }
  63. else
  64. {
  65. deadEndW = true;
  66. }
  67. }
  68. else
  69. {
  70. deadEndW = true;
  71. }
  72. break;
  73. // DUE NORTH
  74. case 2:
  75. // value must be within array bounds
  76. if (myY+1 < mapHeight)
  77. {
  78. if (myMap[myX][myY+1] == 0)
  79. {
  80. myY++;
  81. search = false;
  82. }
  83. else
  84. {
  85. deadEndN = true;
  86. }
  87. }
  88. else
  89. {
  90. deadEndN = true;
  91. }
  92. break;
  93. // DUE SOUTH
  94. case 3:
  95. // value must be within array bounds
  96. if (myY-1 >= 0)
  97. {
  98. if (myMap[myX][myY-1] == 0)
  99. {
  100. myY--;
  101. search = false;
  102. }
  103. else
  104. {
  105. deadEndS = true;
  106. }
  107. }
  108. else
  109. {
  110. deadEndS = true;
  111. }
  112. break;
  113. }
  114.  
  115. // upon search completion update XY
  116. if (!search)
  117. {
  118. count++;
  119. myMap [myX][myY] = count;
  120. }
  121.  
  122. // with no possibilities left; end cycle, execute AFTER update XY! (don't accidentally update XY!)
  123. if (deadEndN == true &&
  124. deadEndS == true &&
  125. deadEndW == true &&
  126. deadEndE == true)
  127. {
  128. search = false;
  129. i = pathLength;
  130. }
  131. }
  132. }
  133.  
  134. // visualise dungeon
  135. for (int i = 0; i < myMap.length; i++)
  136. {
  137. System.out.print("\n");
  138.  
  139. for (int j = 0; j < myMap.length; j++)
  140. {
  141. System.out.print(" " + myMap[i][j] + " ");
  142. }
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement