Advertisement
Guest User

Untitled

a guest
May 28th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 KB | None | 0 0
  1. package com.kabirproject.Slide;
  2.  
  3.  
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.Color;
  6. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  7.  
  8. public class Maze
  9. {
  10. ShapeRenderer shapeRenderer;
  11.  
  12. private int N;
  13. private boolean[][] north;
  14. private boolean[][] east;
  15. private boolean[][] south;
  16. private boolean[][] west;
  17. private boolean[][] visited;
  18. private boolean done = false;
  19. int SIZE_FACTOR = 40;
  20. int lineWidth = 10;
  21.  
  22. public Maze(int N)
  23. {
  24. shapeRenderer = new ShapeRenderer();
  25. Gdx.gl.glLineWidth(lineWidth);
  26. this.N = N;
  27. init();
  28. generate();
  29. solve();
  30.  
  31. }
  32.  
  33. public void setSizeFactor(int sizeFactor)
  34. {
  35. this.SIZE_FACTOR = sizeFactor;
  36. }
  37.  
  38. public int getSizeFactor()
  39. {
  40. return SIZE_FACTOR;
  41. }
  42.  
  43. private void init() {
  44. // initialize border cells as already visited
  45. visited = new boolean[N+2][N+2];
  46. for (int x = 0; x < N+2; x++) visited[x][0] = visited[x][N+1] = true;
  47. for (int y = 0; y < N+2; y++) visited[0][y] = visited[N+1][y] = true;
  48.  
  49.  
  50. // initialize all walls as present
  51. north = new boolean[N+2][N+2];
  52. east = new boolean[N+2][N+2];
  53. south = new boolean[N+2][N+2];
  54. west = new boolean[N+2][N+2];
  55. for (int x = 0; x < N+2; x++)
  56. for (int y = 0; y < N+2; y++)
  57. north[x][y] = east[x][y] = south[x][y] = west[x][y] = true;
  58. }
  59.  
  60.  
  61. // generate the maze
  62. private void generate(int x, int y) {
  63. visited[x][y] = true;
  64.  
  65. // while there is an unvisited neighbor
  66. while (!visited[x][y+1] || !visited[x+1][y] || !visited[x][y-1] || !visited[x-1][y]) {
  67.  
  68. // pick random neighbor
  69. while (true) {
  70. double r = Math.random();
  71. if (r < 0.25 && !visited[x][y+1]) {
  72. north[x][y] = south[x][y+1] = false;
  73. generate(x, y + 1);
  74. break;
  75. }
  76. else if (r >= 0.25 && r < 0.50 && !visited[x+1][y]) {
  77. east[x][y] = west[x+1][y] = false;
  78. generate(x+1, y);
  79. break;
  80. }
  81. else if (r >= 0.5 && r < 0.75 && !visited[x][y-1]) {
  82. south[x][y] = north[x][y-1] = false;
  83. generate(x, y-1);
  84. break;
  85. }
  86. else if (r >= 0.75 && r < 1.00 && !visited[x-1][y]) {
  87. west[x][y] = east[x-1][y] = false;
  88. generate(x-1, y);
  89. break;
  90. }
  91. }
  92. }
  93. }
  94.  
  95.  
  96. private void generate()
  97. {
  98. generate(1, 1);
  99.  
  100.  
  101. //delete some random walls
  102. for (int i = 0; i < N; i++) {
  103. int x = (int) (1 + Math.random() * (N-1));
  104. int y = (int) (1 + Math.random() * (N-1));
  105. north[x][y] = south[x][y+1] = false;
  106. }
  107.  
  108. // add some random walls
  109. for (int i = 0; i < 10; i++) {
  110. int x = (int) (N / 2 + Math.random() * (N / 2));
  111. int y = (int) (N / 2 + Math.random() * (N / 2));
  112. east[x][y] = west[x+1][y] = true;
  113. }
  114.  
  115. }
  116.  
  117. private void solve(int x, int y) {
  118. if (x == 0 || y == 0 || x == N+1 || y == N+1) return;
  119. if (done || visited[x][y]) return;
  120. visited[x][y] = true;
  121.  
  122. shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
  123. shapeRenderer.circle(x + 1, y + 1, 1);
  124. shapeRenderer.end();
  125.  
  126. // reached middle
  127. if (x == N/2 && y == N/2) done = true;
  128.  
  129. if (!north[x][y]) solve(x, y + 1);
  130. if (!east[x][y]) solve(x + 1, y);
  131. if (!south[x][y]) solve(x, y - 1);
  132. if (!west[x][y]) solve(x - 1, y);
  133.  
  134. if (done) return;
  135.  
  136. shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
  137. shapeRenderer.circle(x + 1, y + 1, 1);
  138. shapeRenderer.end();
  139. }
  140.  
  141.  
  142.  
  143. // solve the maze starting from the start state
  144. public void solve() {
  145. for (int x = 1; x <= N; x++)
  146. for (int y = 1; y <= N; y++)
  147. visited[x][y] = false;
  148. done = false;
  149. solve(1, 1);
  150. }
  151.  
  152.  
  153. // solve the maze starting from the start state
  154.  
  155.  
  156. public void draw()
  157. {
  158. shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
  159.  
  160. for (int x = 1; x <= N; x++) {
  161. for (int y = 1; y <= N; y++) {
  162. shapeRenderer.setColor(Color.RED);
  163. if (south[x][y])
  164. {
  165. //shapeRenderer.line(x*SIZE_FACTOR, y*SIZE_FACTOR, (x + 1)*SIZE_FACTOR, y*SIZE_FACTOR );
  166. shapeRenderer.rect(x*SIZE_FACTOR, y*SIZE_FACTOR, SIZE_FACTOR*1, lineWidth);
  167. }
  168. shapeRenderer.setColor(Color.BLUE);
  169. if (north[x][y])
  170. {
  171. //shapeRenderer.line(x*SIZE_FACTOR, (y + 1)*SIZE_FACTOR, (x + 1)*SIZE_FACTOR, (y + 1)*SIZE_FACTOR);
  172. shapeRenderer.rect(x*SIZE_FACTOR, (y+1)*SIZE_FACTOR, SIZE_FACTOR*1, lineWidth);
  173. }
  174. shapeRenderer.setColor(Color.YELLOW);
  175. if (west[x][y])
  176. {
  177. shapeRenderer.rect(x * SIZE_FACTOR, y * SIZE_FACTOR, lineWidth, 1*SIZE_FACTOR);
  178. }
  179. shapeRenderer.setColor(Color.GREEN);
  180. if (east[x][y])
  181. {
  182. shapeRenderer.rect((x + 1)*SIZE_FACTOR, y*SIZE_FACTOR, lineWidth, 1*SIZE_FACTOR);
  183. }
  184.  
  185. /*shapeRenderer.setColor(1, 1, 0, 1);
  186. if(south[x][y]) shapeRenderer.rect(x*SIZE_FACTOR, y*SIZE_FACTOR, getDistance(x, x+1, y, y)*SIZE_FACTOR, lineWidth*SIZE_FACTOR);
  187. shapeRenderer.setColor(1, 0, 0, 1);
  188. if(north[x][y]) shapeRenderer.rect(x*SIZE_FACTOR, (y+1)*SIZE_FACTOR, getDistance(x, x+1, y+1, y+1)*SIZE_FACTOR, lineWidth*SIZE_FACTOR);
  189. shapeRenderer.setColor(0, 1, 0, 1);
  190. if(west[x][y]) shapeRenderer.rect(x*SIZE_FACTOR, y*SIZE_FACTOR, lineWidth*SIZE_FACTOR, getDistance(x, x, y, y+1)*SIZE_FACTOR);
  191. shapeRenderer.setColor(1, 1, 2, 1);
  192. if(east[x][y]) shapeRenderer.rect((x+1)*SIZE_FACTOR, y*SIZE_FACTOR, lineWidth*SIZE_FACTOR, getDistance(x+1,x+1, y, y+1)*SIZE_FACTOR);*/
  193. }
  194. }
  195.  
  196. shapeRenderer.end();
  197.  
  198. }
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement