Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kabirproject.Slide;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- public class Maze
- {
- ShapeRenderer shapeRenderer;
- private int N;
- private boolean[][] north;
- private boolean[][] east;
- private boolean[][] south;
- private boolean[][] west;
- private boolean[][] visited;
- private boolean done = false;
- int SIZE_FACTOR = 40;
- int lineWidth = 10;
- public Maze(int N)
- {
- shapeRenderer = new ShapeRenderer();
- Gdx.gl.glLineWidth(lineWidth);
- this.N = N;
- init();
- generate();
- solve();
- }
- public void setSizeFactor(int sizeFactor)
- {
- this.SIZE_FACTOR = sizeFactor;
- }
- public int getSizeFactor()
- {
- return SIZE_FACTOR;
- }
- private void init() {
- // initialize border cells as already visited
- visited = new boolean[N+2][N+2];
- for (int x = 0; x < N+2; x++) visited[x][0] = visited[x][N+1] = true;
- for (int y = 0; y < N+2; y++) visited[0][y] = visited[N+1][y] = true;
- // initialize all walls as present
- north = new boolean[N+2][N+2];
- east = new boolean[N+2][N+2];
- south = new boolean[N+2][N+2];
- west = new boolean[N+2][N+2];
- for (int x = 0; x < N+2; x++)
- for (int y = 0; y < N+2; y++)
- north[x][y] = east[x][y] = south[x][y] = west[x][y] = true;
- }
- // generate the maze
- private void generate(int x, int y) {
- visited[x][y] = true;
- // while there is an unvisited neighbor
- while (!visited[x][y+1] || !visited[x+1][y] || !visited[x][y-1] || !visited[x-1][y]) {
- // pick random neighbor
- while (true) {
- double r = Math.random();
- if (r < 0.25 && !visited[x][y+1]) {
- north[x][y] = south[x][y+1] = false;
- generate(x, y + 1);
- break;
- }
- else if (r >= 0.25 && r < 0.50 && !visited[x+1][y]) {
- east[x][y] = west[x+1][y] = false;
- generate(x+1, y);
- break;
- }
- else if (r >= 0.5 && r < 0.75 && !visited[x][y-1]) {
- south[x][y] = north[x][y-1] = false;
- generate(x, y-1);
- break;
- }
- else if (r >= 0.75 && r < 1.00 && !visited[x-1][y]) {
- west[x][y] = east[x-1][y] = false;
- generate(x-1, y);
- break;
- }
- }
- }
- }
- private void generate()
- {
- generate(1, 1);
- //delete some random walls
- for (int i = 0; i < N; i++) {
- int x = (int) (1 + Math.random() * (N-1));
- int y = (int) (1 + Math.random() * (N-1));
- north[x][y] = south[x][y+1] = false;
- }
- // add some random walls
- for (int i = 0; i < 10; i++) {
- int x = (int) (N / 2 + Math.random() * (N / 2));
- int y = (int) (N / 2 + Math.random() * (N / 2));
- east[x][y] = west[x+1][y] = true;
- }
- }
- private void solve(int x, int y) {
- if (x == 0 || y == 0 || x == N+1 || y == N+1) return;
- if (done || visited[x][y]) return;
- visited[x][y] = true;
- shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
- shapeRenderer.circle(x + 1, y + 1, 1);
- shapeRenderer.end();
- // reached middle
- if (x == N/2 && y == N/2) done = true;
- if (!north[x][y]) solve(x, y + 1);
- if (!east[x][y]) solve(x + 1, y);
- if (!south[x][y]) solve(x, y - 1);
- if (!west[x][y]) solve(x - 1, y);
- if (done) return;
- shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
- shapeRenderer.circle(x + 1, y + 1, 1);
- shapeRenderer.end();
- }
- // solve the maze starting from the start state
- public void solve() {
- for (int x = 1; x <= N; x++)
- for (int y = 1; y <= N; y++)
- visited[x][y] = false;
- done = false;
- solve(1, 1);
- }
- // solve the maze starting from the start state
- public void draw()
- {
- shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
- for (int x = 1; x <= N; x++) {
- for (int y = 1; y <= N; y++) {
- shapeRenderer.setColor(Color.RED);
- if (south[x][y])
- {
- //shapeRenderer.line(x*SIZE_FACTOR, y*SIZE_FACTOR, (x + 1)*SIZE_FACTOR, y*SIZE_FACTOR );
- shapeRenderer.rect(x*SIZE_FACTOR, y*SIZE_FACTOR, SIZE_FACTOR*1, lineWidth);
- }
- shapeRenderer.setColor(Color.BLUE);
- if (north[x][y])
- {
- //shapeRenderer.line(x*SIZE_FACTOR, (y + 1)*SIZE_FACTOR, (x + 1)*SIZE_FACTOR, (y + 1)*SIZE_FACTOR);
- shapeRenderer.rect(x*SIZE_FACTOR, (y+1)*SIZE_FACTOR, SIZE_FACTOR*1, lineWidth);
- }
- shapeRenderer.setColor(Color.YELLOW);
- if (west[x][y])
- {
- shapeRenderer.rect(x * SIZE_FACTOR, y * SIZE_FACTOR, lineWidth, 1*SIZE_FACTOR);
- }
- shapeRenderer.setColor(Color.GREEN);
- if (east[x][y])
- {
- shapeRenderer.rect((x + 1)*SIZE_FACTOR, y*SIZE_FACTOR, lineWidth, 1*SIZE_FACTOR);
- }
- /*shapeRenderer.setColor(1, 1, 0, 1);
- if(south[x][y]) shapeRenderer.rect(x*SIZE_FACTOR, y*SIZE_FACTOR, getDistance(x, x+1, y, y)*SIZE_FACTOR, lineWidth*SIZE_FACTOR);
- shapeRenderer.setColor(1, 0, 0, 1);
- 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);
- shapeRenderer.setColor(0, 1, 0, 1);
- if(west[x][y]) shapeRenderer.rect(x*SIZE_FACTOR, y*SIZE_FACTOR, lineWidth*SIZE_FACTOR, getDistance(x, x, y, y+1)*SIZE_FACTOR);
- shapeRenderer.setColor(1, 1, 2, 1);
- 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);*/
- }
- }
- shapeRenderer.end();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement