Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PowerControl mbed
Diff: main.cpp
- Revision:
- 2:0b0311609edc
- Parent:
- 1:0f774d41584c
- Child:
- 3:1a25939df22a
diff -r 0f774d41584c -r 0b0311609edc main.cpp
--- a/main.cpp Fri Apr 10 11:47:04 2015 +0000
+++ b/main.cpp Wed Apr 15 12:14:27 2015 +0000
@@ -35,10 +35,21 @@
int map[84][48];
+//Enterance coordinates
+int enx;
+int eny;
+
+//Exit coordinates
+int exx;
+int exy;
+
int sx;
int sy;
int dir;
+int n;
+
+
//Power Saving
int semihost_powerdown()
{
@@ -112,9 +123,20 @@
}
}
//Create enterance in room
- int ex = rand()%sw + si;
- int ey = rand()%sh + sj;
- map[ex][ey] = ENTER;
+ enx = rand()%sw + si;
+ eny = rand()%sh + sj;
+ map[enx][eny] = ENTER;
+}
+
+void SetExit()
+{
+ do {
+ exx = enx + rand()%30 +15;
+ exy = eny + rand()%20 +15;
+ } while(map[exx][exy] == WALL && exx > 84 && exy > 48);
+
+ map[exx][exy] = EXIT;
+
}
void MinePoint()
@@ -196,7 +218,7 @@
void MineRoomBuilder()
{
MinePoint();
-
+
//Get length
int sw = rand()%5 + 5;
int sh = rand()%5 + 5;
@@ -216,7 +238,9 @@
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
- map[i][j] = FLOOR;
+ if(i < 84 && j < 48) {
+ map[i][j] = FLOOR;
+ }
}
}
}
@@ -234,7 +258,9 @@
if(b == sw*sh) {
for(int i = sx; i > sx - sw; i--) {
for(int j = sy; j < sy + sh; j++) {
- map[i][j] = FLOOR;
+ if(i < 84 && j < 48) {
+ map[i][j] = FLOOR;
+ }
}
}
}
@@ -252,7 +278,9 @@
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j < sy + sh; j++) {
- map[i][j] = FLOOR;
+ if(i < 84 && j < 48) {
+ map[i][j] = FLOOR;
+ }
}
}
}
@@ -270,6 +298,38 @@
if(b == sw*sh) {
for(int i = sx; i < sx + sw; i++) {
for(int j = sy; j > sy - sh; j--) {
+ if(i < 84 && j < 48) {
+ map[i][j] = FLOOR;
+ }
+ }
+ }
+ }
+ }
+}
+
+void DungeonRoomBuilder()
+{
+ sx = rand()%84;
+ sy = rand()%48;
+
+ //Get length
+ int sw = rand()%5 + 5;
+ int sh = rand()%5 + 5;
+
+ 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++;
+ }
+ }
+ }
+ if(b == sw*sh) {
+ for(int i = sx; i < sx + sw; i++) {
+ for(int j = sy; j < sy + sh; j++) {
+ if(i < 84 && j < 48) {
map[i][j] = FLOOR;
}
}
@@ -277,8 +337,155 @@
}
}
+void Neighbours()
+{
+
+ //Check neighbours
+ n = 0;
+
+ if(map[sx+1][sy] == FLOOR) {
+ n++;
+ }
+ if(map[sx-1][sy] == FLOOR) {
+ n++;
+ }
+ if(map[sx][sy+1] == FLOOR) {
+ n++;
+ }
+ if(map[sx][sy-1] == FLOOR) {
+ n++;
+ }
+}
+
+void Maze()
+{
+
+ //Random start point
+ sx = rand()%84;
+ sy = rand()%48;
+
+ char unvisited = 0x0; //0000 (Up, Down, Left, Right)
+
+ int visited[84][48];
+
+ int totalVisited = 0; //Max 4032
+
+ map[sx][sy] = FLOOR;
+ visited[sx][sy] = 1;
+
+ while(totalVisited < 4032) {
+ DrawMap();
+ unvisited = 0x0;
+
+ //Check if cell neighbours are visited
+ if(visited[sx+1][sy] == 0 && sx < 83) {
+ unvisited |= (1<<0); //Set 1st bit to 1
+ }
+ if(visited[sx-1][sy] == 0 && sx > 1) {
+ unvisited |= (1<<1); //Set 2nd bit to 1
+ }
+ if(visited[sx][sy-1] == 0 && sy > 1) {
+ unvisited |= (1<<2); //Set 3rd bit to 1
+ }
+ if(visited[sx][sy+1] == 0 && sy < 47) {
+ unvisited |= (1<<3); //Set 4th bit to 1
+ }
+
+ dir = rand()%4;
+
+ if(dir == RIGHT && (0x1 & unvisited)) {
+ sx++;
+ visited[sx][sy] = 1;
+ totalVisited++;
+
+ //Check neighbours
+ Neighbours();
+
+ //If only has 1 neighbour create floor
+ if(n == 1) {
+ map[sx][sy] = FLOOR;
+ } else {
+ sx = rand()%84;
+ sy = rand()%48;
+
+ Neighbours();
+ }
+ while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
+
+ } else if(dir == LEFT && (0x2 & unvisited)) {
+ sx--;
+ visited[sx][sy] = 1;
+ totalVisited++;
+
+ //Check neighbours
+ Neighbours();
+
+ //If only has 1 neighbour create floor
+ if(n == 1) {
+ map[sx][sy] = FLOOR;
+ } else {
+ sx = rand()%84;
+ sy = rand()%48;
+
+ Neighbours();
+ }
+ while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
+
+ } else if(dir == DOWN && (0x4 & unvisited)) {
+ sy--;
+ visited[sx][sy] = 1;
+ totalVisited++;
+
+ //Check neighbours
+ Neighbours();
+
+ //If only has 1 neighbour create floor
+ if(n == 1) {
+ map[sx][sy] = FLOOR;
+ } else {
+ sx = rand()%84;
+ sy = rand()%48;
+
+ Neighbours();
+ }
+ while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
+
+ } else if(dir == UP && (0x8 & unvisited)) {
+ sy++;
+ visited[sx][sy] = 1;
+ totalVisited++;
+
+ //Check neighbours
+ Neighbours();
+
+ //If only has 1 neighbour create floor
+ if(n == 1) {
+ map[sx][sy] = FLOOR;
+ } else {
+ sx = rand()%84;
+ sy = rand()%48;
+
+ Neighbours();
+ }
+ while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
+ } else if((0x1 & unvisited) || (0x2 & unvisited) || (0x4 & unvisited) || (0x8 & unvisited)) {
+ continue;
+ } else {
+ //Find new starting co-ordinate
+ do {
+ sx = rand()%84;
+ sy = rand()%48;
+
+ Neighbours();
+ } while(visited[sx][sy] == 1 || n != 1 || map[sx][sy] != WALL);
+ }
+ }
+}
+
void MineBuilder()
{
+ Walls();
+
FirstRoom();
int fn = rand()%20 + 20;
@@ -291,12 +498,32 @@
MineCorridorBuilder();
}
}
+
+ SetExit();
}
-void Dungeon()
+void DungeonBuilder()
{
+ Walls();
+ int rn = rand()%20 + 20;
+
+ for(int i = rn; i>0; i--) {
+ DungeonRoomBuilder();
+ }
+
+ int cn = rand()%20 + 20;
+
+ for(int i = cn; i>0; i--) {
+ MineCorridorBuilder();
+ }
+}
+
+void World()
+{
+ Walls();
MineBuilder();
+ //DungeonBuilder();
DrawMap();
}
@@ -315,8 +542,9 @@
//Game loop
while(1) {
- Walls();
- Dungeon();
+ Maze();
+
+ //World();
wait(2.0);
}
}
\ No newline at end of file