Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestMap
- {
- public static void main(String [] args)
- {
- // dungeon variables
- int mapWidth, mapHeight;
- mapWidth = 5;
- mapHeight = 5;
- int [][] myMap = new int [mapWidth][mapHeight];
- myMap [mapWidth/2][mapHeight/2] = 1;
- int myX = mapWidth/2, myY = mapWidth/2;
- int count = 1, pathLength = 8;
- boolean search, deadEndN, deadEndS, deadEndW, deadEndE;
- // randomise dungeon
- for (int i = 0; i < pathLength; i++)
- {
- // reset vars for new search cycle
- search = true;
- deadEndN = false; deadEndS = false; deadEndW = false; deadEndE = false;
- // keep randomising until new XY is different to old XY
- while (search)
- {
- int random = (int) (Math.random() * 4.0);
- System.out.println("random " + random);
- // new XY must be horizontally or vertically aligned
- switch (random)
- {
- // DUE EAST
- case 0:
- // value must be within array bounds
- if (myX+1 < mapWidth)
- {
- // only if myX++ would occupy zero/empty
- if (myMap[myX+1][myY] == 0)
- {
- myX++;
- search = false;
- }
- else
- {
- deadEndE = true;
- }
- }
- else
- {
- deadEndE = true;
- }
- break;
- // DUE WEST
- case 1:
- // value must be within array bounds
- if (myX-1 >= 0)
- {
- if (myMap[myX-1][myY] == 0)
- {
- myX--;
- search = false;
- }
- else
- {
- deadEndW = true;
- }
- }
- else
- {
- deadEndW = true;
- }
- break;
- // DUE NORTH
- case 2:
- // value must be within array bounds
- if (myY+1 < mapHeight)
- {
- if (myMap[myX][myY+1] == 0)
- {
- myY++;
- search = false;
- }
- else
- {
- deadEndN = true;
- }
- }
- else
- {
- deadEndN = true;
- }
- break;
- // DUE SOUTH
- case 3:
- // value must be within array bounds
- if (myY-1 >= 0)
- {
- if (myMap[myX][myY-1] == 0)
- {
- myY--;
- search = false;
- }
- else
- {
- deadEndS = true;
- }
- }
- else
- {
- deadEndS = true;
- }
- break;
- }
- // upon search completion update XY
- if (!search)
- {
- count++;
- myMap [myX][myY] = count;
- }
- // with no possibilities left; end cycle, execute AFTER update XY! (don't accidentally update XY!)
- if (deadEndN == true &&
- deadEndS == true &&
- deadEndW == true &&
- deadEndE == true)
- {
- search = false;
- i = pathLength;
- }
- }
- }
- // visualise dungeon
- for (int i = 0; i < myMap.length; i++)
- {
- System.out.print("\n");
- for (int j = 0; j < myMap.length; j++)
- {
- System.out.print(" " + myMap[i][j] + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement