Thomas Gill / Mbed 2 deprecated LabyrinthOfTheMinotaur

Dependencies:   N5110 PowerControl mbed

Revision:
19:1ab2d83ebffa
Parent:
18:98ea2b787894
Child:
20:e54792b89571
--- a/main.cpp	Sun May 03 21:29:01 2015 +0000
+++ b/main.cpp	Mon May 04 15:25:02 2015 +0000
@@ -245,17 +245,20 @@
     int sw = rand()%5 + 5;
     int sh = rand()%5 + 5;
 
-    int b = 0;
+    /*
+        int b = 0;
 
-    //Check each space. +1 to variable if wall. If total = w*h then build room
-    for(int i = sx; i < sx + sw; i++) {
-        for(int j = sy; j < sy + sh; j++) {
-            if(map[i][j] == WALL) {
-                b++;
+        //Check each space. +1 to variable if wall. If total = w*h then build room
+        for(int i = sx; i < sx + sw; i++) {
+            for(int j = sy; j < sy + sh; j++) {
+                if(map[i][j] == WALL) {
+                    b++;
+                }
             }
         }
-    }
-    if(b == sw*sh) {
+        if(b == sw*sh) {
+            */
+    if(sx + sw < 83 && sy + sh < 47) {
         for(int i = sx; i < sx + sw; i++) {
             for(int j = sy; j < sy + sh; j++) {
                 if(i < 84 && j < 48) {
@@ -264,6 +267,7 @@
             }
         }
     }
+
     if(rand()%3 == 0) {
         int i = rand()%sw + sx;
         int j = rand()%sh + sy;
@@ -336,7 +340,7 @@
 
 }
 
-void Maze()
+void MazeOld()
 {
     for(int i = 1; i < 83; i+=2) {
         for(int j = 1; j < 47; j+=2) {
@@ -358,38 +362,115 @@
     }
 }
 
-void MazeNew()
+void MazeKill()
+{
+
+    int move[4] = { UP, DOWN, LEFT, RIGHT};
+
+    bool moved = true;
+
+    while(moved == true) {
+
+        moved = false;
+
+        for (int s = 0; s < 3; s++) { //Shuffle array
+
+            int r = rand() % 4;
+
+            int temp = move[s];
+
+            move[s] = move[r];
+
+            move[r] = temp;
+        }
+
+
+
+        for (int i = 0; i < 3; i++) {
+            if (move[i] == UP) {
+                if (map[sx][sy - 1] == WALL && Neighbours(sx, sy - 1) == 1 && map[sx][sy - 2] == WALL && Neighbours(sx, sy - 2) == 0 && sy > 3) {
+                    map[sx][sy - 1] = FLOOR;
+                    map[sx][sy - 2] = FLOOR;
+                    sy = sy - 2;
+                    moved = true;
+                    break;
+                }
+            } else if (move[i] == DOWN) {
+                if (map[sx][sy + 1] == WALL && Neighbours(sx, sy + 1) == 1 && map[sx][sy + 2] == WALL && Neighbours(sx, sy + 2) == 0 && sy < 45) {
+                    map[sx][sy + 1] = FLOOR;
+                    map[sx][sy + 2] = FLOOR;
+                    sy = sy + 2;
+                    moved = true;
+                    break;
+                }
+            } else if (move[i] == LEFT) {
+                if (map[sx - 1][sy] == WALL && Neighbours(sx - 1, sy) == 1 && map[sx - 2][sy] == WALL && Neighbours(sx - 2, sy) == 0 && sx > 3) {
+                    map[sx - 1][sy] = FLOOR;
+                    map[sx - 2][sy] = FLOOR;
+                    sx = sx - 2;
+                    moved = true;
+                    break;
+                }
+            } else if (move[i] == RIGHT) {
+                if (map[sx + 1][sy] == WALL && Neighbours(sx + 1, sy) == 1 && map[sx + 2][sy] == WALL && Neighbours(sx + 2, sy) == 0 && sx < 81) {
+                    map[sx + 1][sy] = FLOOR;
+                    map[sx + 2][sy] = FLOOR;
+                    sx = sx + 2;
+                    moved = true;
+                    break;
+                }
+            }
+        }
+    }
+
+}
+
+void Maze()
 {
     sx = 1;
     sy = 1;
-    
+
     //Choose random direction
     //Check if 2 cells in direction have no neighbours (excluding current position)
     //If true then build and set new current position
     //If false chose next direction
-    
+
     //If cannot move in any direction scan through each cell until there is one which can be built on
     //If scan completes END
-    
-    int move[4] = {UP, DOWN, LEFT, RIGHT};
-    
-    for(int s = 0; s < 3; s++){ //Shuffle array
-        
-        int r = rand()%4;
-        
-        int temp = move[s];
-        
-        move[s] = move[r];
-        
-        move[r] = temp;
+
+    int end = false;
+
+    while(end == false) {
+
+        end = true;
+
+        map[sx][sy] = FLOOR;
+
+        MazeKill();
+
+        //DrawMap();
+
+        for(int i = 1; i < 83; i++) {
+            for(int j = 1; j < 47; j++) {
+
+                if(map[i][j] == WALL && Neighbours(i, j) == 1) {
+                    sx = i;
+                    sy = j;
+
+                    end = false;
+                }
+
+            }
         }
-    
-    
+
+    }
 }
 
 void DungeonBuilder()
 {
 
+    Maze();
+
     FirstRoom();
     ExitRoom();
 
@@ -399,7 +480,7 @@
         DungeonRoomBuilder();
     }
 
-    Maze();
+
 
     RandFloor(51);
 
@@ -412,10 +493,12 @@
 void LabyrinthBuilder()
 {
 
+    Maze();
+
     FirstRoom();
     ExitRoom();
 
-    Maze();
+
 
     RandFloor(551);
 
@@ -1083,7 +1166,7 @@
         World();
 
         LevelScreen();
-        
+
         RevealMap();
 
         while(1) {
@@ -1113,6 +1196,12 @@
 
                 PlayerMove();
 
+                if(rand()%60 == 0) {
+                    if(ph < 15) {
+                        ph++;
+                    }
+                }
+
                 if(rand()%50 == 0) {
                     Fight();
                 }
@@ -1200,5 +1289,4 @@
     Intro();
 
     MainMenu();
-
 }