Thomas Gill / Mbed 2 deprecated LabyrinthOfTheMinotaur

Dependencies:   N5110 PowerControl mbed

Committer:
ThomasBGill
Date:
Fri Apr 17 00:18:50 2015 +0000
Revision:
3:1a25939df22a
Parent:
2:0b0311609edc
Child:
4:6482ceb08dc8
Basic maze algorithm finished

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ThomasBGill 0:9200b3c387ed 1 #include "mbed.h"
ThomasBGill 0:9200b3c387ed 2 #include "N5110.h"
ThomasBGill 1:0f774d41584c 3 #include "PowerControl/PowerControl.h"
ThomasBGill 1:0f774d41584c 4 #include "PowerControl/EthernetPowerControl.h"
ThomasBGill 0:9200b3c387ed 5
ThomasBGill 0:9200b3c387ed 6 // vcc sce rst dc mosi clk led
ThomasBGill 0:9200b3c387ed 7 N5110 lcd (p5, p6, p7, p8, p11, p13, p21);
ThomasBGill 0:9200b3c387ed 8 InterruptIn Act (p22);
ThomasBGill 0:9200b3c387ed 9 InterruptIn Start (p23);
ThomasBGill 0:9200b3c387ed 10 InterruptIn Up (p24);
ThomasBGill 0:9200b3c387ed 11 InterruptIn Down (p25);
ThomasBGill 0:9200b3c387ed 12 InterruptIn Left (p26);
ThomasBGill 0:9200b3c387ed 13 InterruptIn Right (p27);
ThomasBGill 0:9200b3c387ed 14 AnalogIn Noise (p19);
ThomasBGill 0:9200b3c387ed 15
ThomasBGill 1:0f774d41584c 16 #define USR_POWERDOWN (0x104)
ThomasBGill 1:0f774d41584c 17
ThomasBGill 0:9200b3c387ed 18 #define WALL 0
ThomasBGill 0:9200b3c387ed 19 #define FLOOR 1
ThomasBGill 0:9200b3c387ed 20 #define ENTER 2
ThomasBGill 0:9200b3c387ed 21 #define EXIT 3
ThomasBGill 0:9200b3c387ed 22
ThomasBGill 0:9200b3c387ed 23 #define RIGHT 0
ThomasBGill 0:9200b3c387ed 24 #define LEFT 1
ThomasBGill 0:9200b3c387ed 25 #define UP 2
ThomasBGill 0:9200b3c387ed 26 #define DOWN 3
ThomasBGill 0:9200b3c387ed 27
ThomasBGill 3:1a25939df22a 28 #define SOUTH 0
ThomasBGill 3:1a25939df22a 29 #define EAST 1
ThomasBGill 3:1a25939df22a 30
ThomasBGill 0:9200b3c387ed 31 //Variables
ThomasBGill 0:9200b3c387ed 32 int buttonFlagA = 0;
ThomasBGill 0:9200b3c387ed 33 int buttonFlagS = 0;
ThomasBGill 0:9200b3c387ed 34 int buttonFlagU = 0;
ThomasBGill 0:9200b3c387ed 35 int buttonFlagD = 0;
ThomasBGill 0:9200b3c387ed 36 int buttonFlagL = 0;
ThomasBGill 0:9200b3c387ed 37 int buttonFlagR = 0;
ThomasBGill 0:9200b3c387ed 38
ThomasBGill 0:9200b3c387ed 39 int map[84][48];
ThomasBGill 0:9200b3c387ed 40
ThomasBGill 2:0b0311609edc 41 //Enterance coordinates
ThomasBGill 2:0b0311609edc 42 int enx;
ThomasBGill 2:0b0311609edc 43 int eny;
ThomasBGill 2:0b0311609edc 44
ThomasBGill 2:0b0311609edc 45 //Exit coordinates
ThomasBGill 2:0b0311609edc 46 int exx;
ThomasBGill 2:0b0311609edc 47 int exy;
ThomasBGill 2:0b0311609edc 48
ThomasBGill 1:0f774d41584c 49 int sx;
ThomasBGill 1:0f774d41584c 50 int sy;
ThomasBGill 1:0f774d41584c 51 int dir;
ThomasBGill 1:0f774d41584c 52
ThomasBGill 2:0b0311609edc 53 int n;
ThomasBGill 2:0b0311609edc 54
ThomasBGill 2:0b0311609edc 55
ThomasBGill 1:0f774d41584c 56 //Power Saving
ThomasBGill 1:0f774d41584c 57 int semihost_powerdown()
ThomasBGill 1:0f774d41584c 58 {
ThomasBGill 1:0f774d41584c 59 uint32_t arg;
ThomasBGill 1:0f774d41584c 60 return __semihost(USR_POWERDOWN, &arg);
ThomasBGill 1:0f774d41584c 61 }
ThomasBGill 1:0f774d41584c 62
ThomasBGill 0:9200b3c387ed 63 //ISR
ThomasBGill 0:9200b3c387ed 64 void ActPressed()
ThomasBGill 0:9200b3c387ed 65 {
ThomasBGill 0:9200b3c387ed 66 buttonFlagA = 0;
ThomasBGill 0:9200b3c387ed 67 }
ThomasBGill 0:9200b3c387ed 68 void StartPressed()
ThomasBGill 0:9200b3c387ed 69 {
ThomasBGill 0:9200b3c387ed 70 buttonFlagS = 0;
ThomasBGill 0:9200b3c387ed 71 }
ThomasBGill 0:9200b3c387ed 72 void UpPressed()
ThomasBGill 0:9200b3c387ed 73 {
ThomasBGill 0:9200b3c387ed 74 buttonFlagU = 0;
ThomasBGill 0:9200b3c387ed 75 }
ThomasBGill 0:9200b3c387ed 76 void DownPressed()
ThomasBGill 0:9200b3c387ed 77 {
ThomasBGill 0:9200b3c387ed 78 buttonFlagD = 0;
ThomasBGill 0:9200b3c387ed 79 }
ThomasBGill 0:9200b3c387ed 80 void LeftPressed()
ThomasBGill 0:9200b3c387ed 81 {
ThomasBGill 0:9200b3c387ed 82 buttonFlagL = 0;
ThomasBGill 0:9200b3c387ed 83 }
ThomasBGill 0:9200b3c387ed 84 void RightPressed()
ThomasBGill 0:9200b3c387ed 85 {
ThomasBGill 0:9200b3c387ed 86 buttonFlagR = 0;
ThomasBGill 0:9200b3c387ed 87 }
ThomasBGill 0:9200b3c387ed 88
ThomasBGill 0:9200b3c387ed 89 void Walls()
ThomasBGill 0:9200b3c387ed 90 {
ThomasBGill 0:9200b3c387ed 91 //Fill map with walls
ThomasBGill 0:9200b3c387ed 92 for(int i=0; i<84; i++) {
ThomasBGill 0:9200b3c387ed 93 for (int j=0; j<48; j++) {
ThomasBGill 3:1a25939df22a 94
ThomasBGill 0:9200b3c387ed 95 map[i][j] = WALL;
ThomasBGill 0:9200b3c387ed 96 }
ThomasBGill 0:9200b3c387ed 97 }
ThomasBGill 0:9200b3c387ed 98 }
ThomasBGill 0:9200b3c387ed 99
ThomasBGill 3:1a25939df22a 100 void Floors()
ThomasBGill 3:1a25939df22a 101 {
ThomasBGill 3:1a25939df22a 102 //Fill map with floors
ThomasBGill 3:1a25939df22a 103 for(int i=0; i<84; i++) {
ThomasBGill 3:1a25939df22a 104 for (int j=0; j<48; j++) {
ThomasBGill 3:1a25939df22a 105 map[i][j] = FLOOR;
ThomasBGill 3:1a25939df22a 106 }
ThomasBGill 3:1a25939df22a 107 }
ThomasBGill 3:1a25939df22a 108 }
ThomasBGill 3:1a25939df22a 109
ThomasBGill 1:0f774d41584c 110 void DrawMap()
ThomasBGill 1:0f774d41584c 111 {
ThomasBGill 1:0f774d41584c 112 //Draw map on screen
ThomasBGill 1:0f774d41584c 113 for(int i=0; i<84; i++) {
ThomasBGill 1:0f774d41584c 114 for (int j=0; j<48; j++) {
ThomasBGill 1:0f774d41584c 115 if(map[i][j] == FLOOR) {
ThomasBGill 1:0f774d41584c 116 lcd.clearPixel(i, j);
ThomasBGill 1:0f774d41584c 117 } else {
ThomasBGill 1:0f774d41584c 118 lcd.setPixel(i, j);
ThomasBGill 1:0f774d41584c 119 }
ThomasBGill 1:0f774d41584c 120 }
ThomasBGill 1:0f774d41584c 121 }
ThomasBGill 1:0f774d41584c 122 lcd.refresh();
ThomasBGill 1:0f774d41584c 123 }
ThomasBGill 1:0f774d41584c 124
ThomasBGill 0:9200b3c387ed 125 void FirstRoom()
ThomasBGill 0:9200b3c387ed 126 {
ThomasBGill 0:9200b3c387ed 127 //Create initial room
ThomasBGill 1:0f774d41584c 128 int si = rand()%25 + 1;
ThomasBGill 1:0f774d41584c 129 int sj = rand()%15 + 1;
ThomasBGill 0:9200b3c387ed 130
ThomasBGill 0:9200b3c387ed 131 int sw = rand()%5+5;
ThomasBGill 0:9200b3c387ed 132 int sh = rand()%5+5;
ThomasBGill 0:9200b3c387ed 133
ThomasBGill 0:9200b3c387ed 134 for(int i = si, w = si + sw; i <w; i++) {
ThomasBGill 0:9200b3c387ed 135 for (int j = sj, h = sj + sh; j <h; j++) {
ThomasBGill 0:9200b3c387ed 136 map[i][j] = FLOOR;
ThomasBGill 0:9200b3c387ed 137 }
ThomasBGill 0:9200b3c387ed 138 }
ThomasBGill 0:9200b3c387ed 139 //Create enterance in room
ThomasBGill 2:0b0311609edc 140 enx = rand()%sw + si;
ThomasBGill 2:0b0311609edc 141 eny = rand()%sh + sj;
ThomasBGill 2:0b0311609edc 142 map[enx][eny] = ENTER;
ThomasBGill 2:0b0311609edc 143 }
ThomasBGill 2:0b0311609edc 144
ThomasBGill 2:0b0311609edc 145 void SetExit()
ThomasBGill 2:0b0311609edc 146 {
ThomasBGill 2:0b0311609edc 147 do {
ThomasBGill 2:0b0311609edc 148 exx = enx + rand()%30 +15;
ThomasBGill 2:0b0311609edc 149 exy = eny + rand()%20 +15;
ThomasBGill 2:0b0311609edc 150 } while(map[exx][exy] == WALL && exx > 84 && exy > 48);
ThomasBGill 2:0b0311609edc 151
ThomasBGill 2:0b0311609edc 152 map[exx][exy] = EXIT;
ThomasBGill 2:0b0311609edc 153
ThomasBGill 0:9200b3c387ed 154 }
ThomasBGill 0:9200b3c387ed 155
ThomasBGill 1:0f774d41584c 156 void MinePoint()
ThomasBGill 0:9200b3c387ed 157 {
ThomasBGill 0:9200b3c387ed 158 //Find start location
ThomasBGill 0:9200b3c387ed 159 for(int i=0; i<84; i++) {
ThomasBGill 0:9200b3c387ed 160 for (int j=0; j<48; j++) {
ThomasBGill 1:0f774d41584c 161
ThomasBGill 0:9200b3c387ed 162 int r = rand()%6;
ThomasBGill 1:0f774d41584c 163
ThomasBGill 0:9200b3c387ed 164 if(map[i][j] == FLOOR && map[i+1][j] == WALL && r == 0) {
ThomasBGill 0:9200b3c387ed 165 dir = RIGHT;
ThomasBGill 0:9200b3c387ed 166 sx = i;
ThomasBGill 0:9200b3c387ed 167 sy = j;
ThomasBGill 0:9200b3c387ed 168 break;
ThomasBGill 0:9200b3c387ed 169 } else if(map[i][j] == FLOOR && map[i-1][j] == WALL && r == 0) {
ThomasBGill 0:9200b3c387ed 170 dir = LEFT;
ThomasBGill 0:9200b3c387ed 171 sx = i;
ThomasBGill 0:9200b3c387ed 172 sy = j;
ThomasBGill 0:9200b3c387ed 173 break;
ThomasBGill 0:9200b3c387ed 174 } else if(map[i][j] == FLOOR && map[i][j+1] == WALL && r == 0) {
ThomasBGill 0:9200b3c387ed 175 dir = UP;
ThomasBGill 0:9200b3c387ed 176 sx = i;
ThomasBGill 0:9200b3c387ed 177 sy = j;
ThomasBGill 0:9200b3c387ed 178 break;
ThomasBGill 0:9200b3c387ed 179 } else if(map[i][j] == FLOOR && map[i][j-1] == WALL && r == 0) {
ThomasBGill 0:9200b3c387ed 180 dir = DOWN;
ThomasBGill 0:9200b3c387ed 181 sx = i;
ThomasBGill 0:9200b3c387ed 182 sy = j;
ThomasBGill 0:9200b3c387ed 183 break;
ThomasBGill 0:9200b3c387ed 184 }
ThomasBGill 0:9200b3c387ed 185 }
ThomasBGill 0:9200b3c387ed 186 }
ThomasBGill 1:0f774d41584c 187 }
ThomasBGill 1:0f774d41584c 188
ThomasBGill 1:0f774d41584c 189 void MineCorridorBuilder()
ThomasBGill 1:0f774d41584c 190 {
ThomasBGill 1:0f774d41584c 191 MinePoint();
ThomasBGill 1:0f774d41584c 192
ThomasBGill 0:9200b3c387ed 193 //Get length
ThomasBGill 0:9200b3c387ed 194 int l = rand()%5 + 5;
ThomasBGill 0:9200b3c387ed 195
ThomasBGill 0:9200b3c387ed 196 //Check direction of corridor
ThomasBGill 0:9200b3c387ed 197 if(dir == RIGHT) {
ThomasBGill 0:9200b3c387ed 198 for(int i = l; i>0; i--) {
ThomasBGill 1:0f774d41584c 199 if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL && sx < 83) {
ThomasBGill 0:9200b3c387ed 200 sx++;
ThomasBGill 0:9200b3c387ed 201 map[sx][sy] = FLOOR;
ThomasBGill 0:9200b3c387ed 202 } else
ThomasBGill 0:9200b3c387ed 203 break;
ThomasBGill 0:9200b3c387ed 204 }
ThomasBGill 0:9200b3c387ed 205 } else if(dir == LEFT) {
ThomasBGill 0:9200b3c387ed 206 for(int i = l; i>0; i--) {
ThomasBGill 1:0f774d41584c 207 if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL && sx > 1) {
ThomasBGill 0:9200b3c387ed 208 sx--;
ThomasBGill 0:9200b3c387ed 209 map[sx][sy] = FLOOR;
ThomasBGill 1:0f774d41584c 210 } else
ThomasBGill 0:9200b3c387ed 211 break;
ThomasBGill 0:9200b3c387ed 212 }
ThomasBGill 0:9200b3c387ed 213 } else if(dir == UP) {
ThomasBGill 1:0f774d41584c 214 for(int i = l; i>0; i--) {
ThomasBGill 1:0f774d41584c 215 if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL && sy < 47) {
ThomasBGill 1:0f774d41584c 216 sy++;
ThomasBGill 1:0f774d41584c 217 map[sx][sy] = FLOOR;
ThomasBGill 1:0f774d41584c 218 } else
ThomasBGill 0:9200b3c387ed 219 break;
ThomasBGill 0:9200b3c387ed 220 }
ThomasBGill 0:9200b3c387ed 221 } else if(dir == DOWN) {
ThomasBGill 1:0f774d41584c 222 for(int i = l; i>0; i--) {
ThomasBGill 1:0f774d41584c 223 if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL && sy > 1) {
ThomasBGill 1:0f774d41584c 224 sy--;
ThomasBGill 1:0f774d41584c 225 map[sx][sy] = FLOOR;
ThomasBGill 1:0f774d41584c 226 } else
ThomasBGill 0:9200b3c387ed 227 break;
ThomasBGill 0:9200b3c387ed 228 }
ThomasBGill 0:9200b3c387ed 229 }
ThomasBGill 0:9200b3c387ed 230 }
ThomasBGill 0:9200b3c387ed 231
ThomasBGill 1:0f774d41584c 232 void MineRoomBuilder()
ThomasBGill 1:0f774d41584c 233 {
ThomasBGill 1:0f774d41584c 234 MinePoint();
ThomasBGill 2:0b0311609edc 235
ThomasBGill 1:0f774d41584c 236 //Get length
ThomasBGill 1:0f774d41584c 237 int sw = rand()%5 + 5;
ThomasBGill 1:0f774d41584c 238 int sh = rand()%5 + 5;
ThomasBGill 0:9200b3c387ed 239
ThomasBGill 1:0f774d41584c 240 int b = 0;
ThomasBGill 1:0f774d41584c 241
ThomasBGill 1:0f774d41584c 242 if(dir == RIGHT) {
ThomasBGill 1:0f774d41584c 243 sx++;
ThomasBGill 1:0f774d41584c 244 //Check each space. +1 to variable if wall. If total = w*h then build room
ThomasBGill 1:0f774d41584c 245 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 1:0f774d41584c 246 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 1:0f774d41584c 247 if(map[i][j] == WALL) {
ThomasBGill 1:0f774d41584c 248 b++;
ThomasBGill 1:0f774d41584c 249 }
ThomasBGill 1:0f774d41584c 250 }
ThomasBGill 1:0f774d41584c 251 }
ThomasBGill 1:0f774d41584c 252 if(b == sw*sh) {
ThomasBGill 1:0f774d41584c 253 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 1:0f774d41584c 254 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 255 if(i < 84 && j < 48) {
ThomasBGill 2:0b0311609edc 256 map[i][j] = FLOOR;
ThomasBGill 2:0b0311609edc 257 }
ThomasBGill 1:0f774d41584c 258 }
ThomasBGill 1:0f774d41584c 259 }
ThomasBGill 1:0f774d41584c 260 }
ThomasBGill 1:0f774d41584c 261 }
ThomasBGill 1:0f774d41584c 262 if(dir == LEFT) {
ThomasBGill 1:0f774d41584c 263 sx--;
ThomasBGill 1:0f774d41584c 264 //Check each space. +1 to variable if wall. If total = w*h then build room
ThomasBGill 1:0f774d41584c 265 for(int i = sx; i > sx - sw; i--) {
ThomasBGill 1:0f774d41584c 266 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 1:0f774d41584c 267 if(map[i][j] == WALL) {
ThomasBGill 1:0f774d41584c 268 b++;
ThomasBGill 1:0f774d41584c 269 }
ThomasBGill 1:0f774d41584c 270 }
ThomasBGill 1:0f774d41584c 271 }
ThomasBGill 1:0f774d41584c 272 if(b == sw*sh) {
ThomasBGill 1:0f774d41584c 273 for(int i = sx; i > sx - sw; i--) {
ThomasBGill 1:0f774d41584c 274 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 275 if(i < 84 && j < 48) {
ThomasBGill 2:0b0311609edc 276 map[i][j] = FLOOR;
ThomasBGill 2:0b0311609edc 277 }
ThomasBGill 1:0f774d41584c 278 }
ThomasBGill 1:0f774d41584c 279 }
ThomasBGill 1:0f774d41584c 280 }
ThomasBGill 1:0f774d41584c 281 }
ThomasBGill 1:0f774d41584c 282 if(dir == UP) {
ThomasBGill 1:0f774d41584c 283 sy++;
ThomasBGill 1:0f774d41584c 284 //Check each space. +1 to variable if wall. If total = w*h then build room
ThomasBGill 1:0f774d41584c 285 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 1:0f774d41584c 286 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 1:0f774d41584c 287 if(map[i][j] == WALL) {
ThomasBGill 1:0f774d41584c 288 b++;
ThomasBGill 1:0f774d41584c 289 }
ThomasBGill 1:0f774d41584c 290 }
ThomasBGill 1:0f774d41584c 291 }
ThomasBGill 1:0f774d41584c 292 if(b == sw*sh) {
ThomasBGill 1:0f774d41584c 293 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 1:0f774d41584c 294 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 295 if(i < 84 && j < 48) {
ThomasBGill 2:0b0311609edc 296 map[i][j] = FLOOR;
ThomasBGill 2:0b0311609edc 297 }
ThomasBGill 1:0f774d41584c 298 }
ThomasBGill 1:0f774d41584c 299 }
ThomasBGill 1:0f774d41584c 300 }
ThomasBGill 1:0f774d41584c 301 }
ThomasBGill 1:0f774d41584c 302 if(dir == DOWN) {
ThomasBGill 1:0f774d41584c 303 sy--;
ThomasBGill 1:0f774d41584c 304 //Check each space. +1 to variable if wall. If total = w*h then build room
ThomasBGill 1:0f774d41584c 305 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 1:0f774d41584c 306 for(int j = sy; j > sy - sh; j--) {
ThomasBGill 1:0f774d41584c 307 if(map[i][j] == WALL) {
ThomasBGill 1:0f774d41584c 308 b++;
ThomasBGill 1:0f774d41584c 309 }
ThomasBGill 1:0f774d41584c 310 }
ThomasBGill 1:0f774d41584c 311 }
ThomasBGill 1:0f774d41584c 312 if(b == sw*sh) {
ThomasBGill 1:0f774d41584c 313 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 1:0f774d41584c 314 for(int j = sy; j > sy - sh; j--) {
ThomasBGill 2:0b0311609edc 315 if(i < 84 && j < 48) {
ThomasBGill 2:0b0311609edc 316 map[i][j] = FLOOR;
ThomasBGill 2:0b0311609edc 317 }
ThomasBGill 2:0b0311609edc 318 }
ThomasBGill 2:0b0311609edc 319 }
ThomasBGill 2:0b0311609edc 320 }
ThomasBGill 2:0b0311609edc 321 }
ThomasBGill 2:0b0311609edc 322 }
ThomasBGill 2:0b0311609edc 323
ThomasBGill 2:0b0311609edc 324 void DungeonRoomBuilder()
ThomasBGill 2:0b0311609edc 325 {
ThomasBGill 2:0b0311609edc 326 sx = rand()%84;
ThomasBGill 2:0b0311609edc 327 sy = rand()%48;
ThomasBGill 2:0b0311609edc 328
ThomasBGill 2:0b0311609edc 329 //Get length
ThomasBGill 2:0b0311609edc 330 int sw = rand()%5 + 5;
ThomasBGill 2:0b0311609edc 331 int sh = rand()%5 + 5;
ThomasBGill 2:0b0311609edc 332
ThomasBGill 2:0b0311609edc 333 int b = 0;
ThomasBGill 2:0b0311609edc 334
ThomasBGill 2:0b0311609edc 335 //Check each space. +1 to variable if wall. If total = w*h then build room
ThomasBGill 2:0b0311609edc 336 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 2:0b0311609edc 337 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 338 if(map[i][j] == WALL) {
ThomasBGill 2:0b0311609edc 339 b++;
ThomasBGill 2:0b0311609edc 340 }
ThomasBGill 2:0b0311609edc 341 }
ThomasBGill 2:0b0311609edc 342 }
ThomasBGill 2:0b0311609edc 343 if(b == sw*sh) {
ThomasBGill 2:0b0311609edc 344 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 2:0b0311609edc 345 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 346 if(i < 84 && j < 48) {
ThomasBGill 1:0f774d41584c 347 map[i][j] = FLOOR;
ThomasBGill 1:0f774d41584c 348 }
ThomasBGill 1:0f774d41584c 349 }
ThomasBGill 1:0f774d41584c 350 }
ThomasBGill 1:0f774d41584c 351 }
ThomasBGill 1:0f774d41584c 352 }
ThomasBGill 1:0f774d41584c 353
ThomasBGill 2:0b0311609edc 354 void Maze()
ThomasBGill 2:0b0311609edc 355 {
ThomasBGill 2:0b0311609edc 356
ThomasBGill 3:1a25939df22a 357 for(int i = 0; i < 84; i+=2) {
ThomasBGill 3:1a25939df22a 358 for(int j = 0; j < 48; j+=2) {
ThomasBGill 2:0b0311609edc 359
ThomasBGill 3:1a25939df22a 360 map[i][j] = FLOOR;
ThomasBGill 2:0b0311609edc 361
ThomasBGill 3:1a25939df22a 362 dir = rand()%2; //South or east
ThomasBGill 2:0b0311609edc 363
ThomasBGill 3:1a25939df22a 364 if(dir == SOUTH && j < 47) {
ThomasBGill 3:1a25939df22a 365 map[i][j+1] = FLOOR;
ThomasBGill 2:0b0311609edc 366 }
ThomasBGill 3:1a25939df22a 367 if(dir == EAST && i < 84) {
ThomasBGill 3:1a25939df22a 368 map[i+1][j] = FLOOR;
ThomasBGill 2:0b0311609edc 369 }
ThomasBGill 2:0b0311609edc 370 }
ThomasBGill 2:0b0311609edc 371 }
ThomasBGill 3:1a25939df22a 372
ThomasBGill 3:1a25939df22a 373 for(int del = rand()%250 + 151; del > 0; del--) {
ThomasBGill 3:1a25939df22a 374
ThomasBGill 3:1a25939df22a 375 int i = rand()% 84;
ThomasBGill 3:1a25939df22a 376 int j = rand()% 48;
ThomasBGill 3:1a25939df22a 377
ThomasBGill 3:1a25939df22a 378 if(rand()%2 == 0) {
ThomasBGill 3:1a25939df22a 379 map[i][j] = FLOOR;
ThomasBGill 3:1a25939df22a 380 }
ThomasBGill 3:1a25939df22a 381 }
ThomasBGill 3:1a25939df22a 382
ThomasBGill 3:1a25939df22a 383
ThomasBGill 3:1a25939df22a 384
ThomasBGill 3:1a25939df22a 385
ThomasBGill 2:0b0311609edc 386 }
ThomasBGill 2:0b0311609edc 387
ThomasBGill 1:0f774d41584c 388 void MineBuilder()
ThomasBGill 0:9200b3c387ed 389 {
ThomasBGill 2:0b0311609edc 390 Walls();
ThomasBGill 2:0b0311609edc 391
ThomasBGill 0:9200b3c387ed 392 FirstRoom();
ThomasBGill 0:9200b3c387ed 393
ThomasBGill 1:0f774d41584c 394 int fn = rand()%20 + 20;
ThomasBGill 0:9200b3c387ed 395
ThomasBGill 1:0f774d41584c 396 for(int i = fn; i>0; i--) {
ThomasBGill 1:0f774d41584c 397 int f = rand()% 5;
ThomasBGill 1:0f774d41584c 398 if(f == 0) {
ThomasBGill 1:0f774d41584c 399 MineRoomBuilder();
ThomasBGill 1:0f774d41584c 400 } else {
ThomasBGill 1:0f774d41584c 401 MineCorridorBuilder();
ThomasBGill 0:9200b3c387ed 402 }
ThomasBGill 0:9200b3c387ed 403 }
ThomasBGill 2:0b0311609edc 404
ThomasBGill 2:0b0311609edc 405 SetExit();
ThomasBGill 0:9200b3c387ed 406 }
ThomasBGill 0:9200b3c387ed 407
ThomasBGill 2:0b0311609edc 408 void DungeonBuilder()
ThomasBGill 0:9200b3c387ed 409 {
ThomasBGill 1:0f774d41584c 410
ThomasBGill 2:0b0311609edc 411 int rn = rand()%20 + 20;
ThomasBGill 2:0b0311609edc 412
ThomasBGill 2:0b0311609edc 413 for(int i = rn; i>0; i--) {
ThomasBGill 2:0b0311609edc 414 DungeonRoomBuilder();
ThomasBGill 2:0b0311609edc 415 }
ThomasBGill 2:0b0311609edc 416
ThomasBGill 3:1a25939df22a 417 DrawMap();
ThomasBGill 3:1a25939df22a 418 wait(1.0);
ThomasBGill 2:0b0311609edc 419
ThomasBGill 3:1a25939df22a 420 Maze();
ThomasBGill 3:1a25939df22a 421
ThomasBGill 2:0b0311609edc 422 }
ThomasBGill 2:0b0311609edc 423
ThomasBGill 2:0b0311609edc 424 void World()
ThomasBGill 2:0b0311609edc 425 {
ThomasBGill 2:0b0311609edc 426 Walls();
ThomasBGill 3:1a25939df22a 427 //MineBuilder();
ThomasBGill 3:1a25939df22a 428 DungeonBuilder();
ThomasBGill 0:9200b3c387ed 429 DrawMap();
ThomasBGill 1:0f774d41584c 430
ThomasBGill 0:9200b3c387ed 431 }
ThomasBGill 0:9200b3c387ed 432
ThomasBGill 0:9200b3c387ed 433 int main()
ThomasBGill 0:9200b3c387ed 434 {
ThomasBGill 1:0f774d41584c 435 //Power Saving
ThomasBGill 1:0f774d41584c 436 PHY_PowerDown ();
ThomasBGill 1:0f774d41584c 437 int result = semihost_powerdown();
ThomasBGill 1:0f774d41584c 438
ThomasBGill 1:0f774d41584c 439 //Generate random seed
ThomasBGill 0:9200b3c387ed 440 srand(Noise*1000000);
ThomasBGill 1:0f774d41584c 441
ThomasBGill 1:0f774d41584c 442 //Initilize screen
ThomasBGill 0:9200b3c387ed 443 lcd.init();
ThomasBGill 0:9200b3c387ed 444
ThomasBGill 1:0f774d41584c 445 //Game loop
ThomasBGill 0:9200b3c387ed 446 while(1) {
ThomasBGill 3:1a25939df22a 447 World();
ThomasBGill 3:1a25939df22a 448 wait(10.0);
ThomasBGill 0:9200b3c387ed 449 }
ThomasBGill 0:9200b3c387ed 450 }