Thomas Gill / Mbed 2 deprecated LabyrinthOfTheMinotaur

Dependencies:   N5110 PowerControl mbed

Committer:
ThomasBGill
Date:
Fri Apr 17 01:26:50 2015 +0000
Revision:
6:ca5db4353c95
Parent:
5:9bd276652111
Child:
7:cd799c701997
Mine algorithm removed (not needed)

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 5:9bd276652111 145 void ExitRoom()
ThomasBGill 5:9bd276652111 146 {
ThomasBGill 6:ca5db4353c95 147 //Create exit room
ThomasBGill 5:9bd276652111 148 int si = rand()%50 + 30;
ThomasBGill 5:9bd276652111 149 int sj = rand()%25 + 20;
ThomasBGill 5:9bd276652111 150
ThomasBGill 5:9bd276652111 151 int sw = rand()%5+5;
ThomasBGill 5:9bd276652111 152 int sh = rand()%5+5;
ThomasBGill 5:9bd276652111 153
ThomasBGill 5:9bd276652111 154 for(int i = si, w = si + sw; i <w; i++) {
ThomasBGill 5:9bd276652111 155 for (int j = sj, h = sj + sh; j <h; j++) {
ThomasBGill 5:9bd276652111 156 map[i][j] = FLOOR;
ThomasBGill 5:9bd276652111 157 }
ThomasBGill 5:9bd276652111 158 }
ThomasBGill 6:ca5db4353c95 159 //Create exit in room
ThomasBGill 5:9bd276652111 160 enx = rand()%sw + si;
ThomasBGill 5:9bd276652111 161 eny = rand()%sh + sj;
ThomasBGill 5:9bd276652111 162 map[enx][eny] = EXIT;
ThomasBGill 5:9bd276652111 163 }
ThomasBGill 5:9bd276652111 164
ThomasBGill 2:0b0311609edc 165 void DungeonRoomBuilder()
ThomasBGill 2:0b0311609edc 166 {
ThomasBGill 2:0b0311609edc 167 sx = rand()%84;
ThomasBGill 2:0b0311609edc 168 sy = rand()%48;
ThomasBGill 2:0b0311609edc 169
ThomasBGill 2:0b0311609edc 170 //Get length
ThomasBGill 2:0b0311609edc 171 int sw = rand()%5 + 5;
ThomasBGill 2:0b0311609edc 172 int sh = rand()%5 + 5;
ThomasBGill 2:0b0311609edc 173
ThomasBGill 2:0b0311609edc 174 int b = 0;
ThomasBGill 2:0b0311609edc 175
ThomasBGill 2:0b0311609edc 176 //Check each space. +1 to variable if wall. If total = w*h then build room
ThomasBGill 2:0b0311609edc 177 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 2:0b0311609edc 178 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 179 if(map[i][j] == WALL) {
ThomasBGill 2:0b0311609edc 180 b++;
ThomasBGill 2:0b0311609edc 181 }
ThomasBGill 2:0b0311609edc 182 }
ThomasBGill 2:0b0311609edc 183 }
ThomasBGill 2:0b0311609edc 184 if(b == sw*sh) {
ThomasBGill 2:0b0311609edc 185 for(int i = sx; i < sx + sw; i++) {
ThomasBGill 2:0b0311609edc 186 for(int j = sy; j < sy + sh; j++) {
ThomasBGill 2:0b0311609edc 187 if(i < 84 && j < 48) {
ThomasBGill 1:0f774d41584c 188 map[i][j] = FLOOR;
ThomasBGill 1:0f774d41584c 189 }
ThomasBGill 1:0f774d41584c 190 }
ThomasBGill 1:0f774d41584c 191 }
ThomasBGill 1:0f774d41584c 192 }
ThomasBGill 1:0f774d41584c 193 }
ThomasBGill 1:0f774d41584c 194
ThomasBGill 4:6482ceb08dc8 195 void Neighbours()
ThomasBGill 4:6482ceb08dc8 196 {
ThomasBGill 4:6482ceb08dc8 197
ThomasBGill 4:6482ceb08dc8 198 //Check neighbours
ThomasBGill 4:6482ceb08dc8 199 n = 0;
ThomasBGill 4:6482ceb08dc8 200
ThomasBGill 4:6482ceb08dc8 201 if(map[sx+1][sy] == FLOOR) {
ThomasBGill 4:6482ceb08dc8 202 n++;
ThomasBGill 4:6482ceb08dc8 203 }
ThomasBGill 4:6482ceb08dc8 204 if(map[sx-1][sy] == FLOOR) {
ThomasBGill 4:6482ceb08dc8 205 n++;
ThomasBGill 4:6482ceb08dc8 206 }
ThomasBGill 4:6482ceb08dc8 207 if(map[sx][sy+1] == FLOOR) {
ThomasBGill 4:6482ceb08dc8 208 n++;
ThomasBGill 4:6482ceb08dc8 209 }
ThomasBGill 4:6482ceb08dc8 210 if(map[sx][sy-1] == FLOOR) {
ThomasBGill 4:6482ceb08dc8 211 n++;
ThomasBGill 4:6482ceb08dc8 212 }
ThomasBGill 4:6482ceb08dc8 213 }
ThomasBGill 4:6482ceb08dc8 214
ThomasBGill 4:6482ceb08dc8 215 void DeadEnds()
ThomasBGill 4:6482ceb08dc8 216 {
ThomasBGill 5:9bd276652111 217 for (int del = 20; del > 0; del--) {
ThomasBGill 4:6482ceb08dc8 218 for(int i = 0; i < 84; i++) {
ThomasBGill 4:6482ceb08dc8 219 for(int j = 0; j < 48; j++) {
ThomasBGill 4:6482ceb08dc8 220
ThomasBGill 4:6482ceb08dc8 221 sx = i;
ThomasBGill 4:6482ceb08dc8 222 sy = j;
ThomasBGill 4:6482ceb08dc8 223
ThomasBGill 4:6482ceb08dc8 224 Neighbours();
ThomasBGill 4:6482ceb08dc8 225
ThomasBGill 4:6482ceb08dc8 226 if(n == 1) {
ThomasBGill 4:6482ceb08dc8 227 map[i][j] = WALL;
ThomasBGill 4:6482ceb08dc8 228 }
ThomasBGill 4:6482ceb08dc8 229 }
ThomasBGill 4:6482ceb08dc8 230 }
ThomasBGill 4:6482ceb08dc8 231 }
ThomasBGill 4:6482ceb08dc8 232 }
ThomasBGill 4:6482ceb08dc8 233
ThomasBGill 5:9bd276652111 234 void Border()
ThomasBGill 5:9bd276652111 235 {
ThomasBGill 5:9bd276652111 236
ThomasBGill 5:9bd276652111 237 for(int i = 0; i < 84; i++) {
ThomasBGill 5:9bd276652111 238 for(int j = 0; j < 48; j++) {
ThomasBGill 5:9bd276652111 239
ThomasBGill 5:9bd276652111 240 if(i == 0 || i == 83 || j == 0 || j == 47) {
ThomasBGill 5:9bd276652111 241
ThomasBGill 5:9bd276652111 242 map[i][j] = WALL;
ThomasBGill 5:9bd276652111 243
ThomasBGill 5:9bd276652111 244 }
ThomasBGill 5:9bd276652111 245 }
ThomasBGill 5:9bd276652111 246 }
ThomasBGill 5:9bd276652111 247 }
ThomasBGill 4:6482ceb08dc8 248
ThomasBGill 2:0b0311609edc 249 void Maze()
ThomasBGill 2:0b0311609edc 250 {
ThomasBGill 3:1a25939df22a 251 for(int i = 0; i < 84; i+=2) {
ThomasBGill 3:1a25939df22a 252 for(int j = 0; j < 48; j+=2) {
ThomasBGill 2:0b0311609edc 253
ThomasBGill 5:9bd276652111 254 if(map[i][j] == WALL) {
ThomasBGill 5:9bd276652111 255 map[i][j] = FLOOR;
ThomasBGill 5:9bd276652111 256 }
ThomasBGill 2:0b0311609edc 257
ThomasBGill 3:1a25939df22a 258 dir = rand()%2; //South or east
ThomasBGill 2:0b0311609edc 259
ThomasBGill 4:6482ceb08dc8 260
ThomasBGill 5:9bd276652111 261 if(dir == SOUTH && j < 47 && map[i][j+1] == WALL) {
ThomasBGill 3:1a25939df22a 262 map[i][j+1] = FLOOR;
ThomasBGill 2:0b0311609edc 263 }
ThomasBGill 5:9bd276652111 264 if(dir == EAST && i < 84 && map[i+1][j] == WALL) {
ThomasBGill 3:1a25939df22a 265 map[i+1][j] = FLOOR;
ThomasBGill 2:0b0311609edc 266 }
ThomasBGill 2:0b0311609edc 267 }
ThomasBGill 2:0b0311609edc 268 }
ThomasBGill 3:1a25939df22a 269
ThomasBGill 4:6482ceb08dc8 270 for(int space = rand()%50 + 51; space > 0; space--) {
ThomasBGill 3:1a25939df22a 271
ThomasBGill 3:1a25939df22a 272 int i = rand()% 84;
ThomasBGill 3:1a25939df22a 273 int j = rand()% 48;
ThomasBGill 3:1a25939df22a 274
ThomasBGill 5:9bd276652111 275 if(rand()%2 == 0 && map[i][j] == WALL) {
ThomasBGill 3:1a25939df22a 276 map[i][j] = FLOOR;
ThomasBGill 3:1a25939df22a 277 }
ThomasBGill 3:1a25939df22a 278 }
ThomasBGill 4:6482ceb08dc8 279
ThomasBGill 5:9bd276652111 280 Border();
ThomasBGill 5:9bd276652111 281
ThomasBGill 4:6482ceb08dc8 282 DeadEnds();
ThomasBGill 3:1a25939df22a 283
ThomasBGill 2:0b0311609edc 284 }
ThomasBGill 2:0b0311609edc 285
ThomasBGill 2:0b0311609edc 286 void DungeonBuilder()
ThomasBGill 0:9200b3c387ed 287 {
ThomasBGill 1:0f774d41584c 288
ThomasBGill 4:6482ceb08dc8 289 FirstRoom();
ThomasBGill 5:9bd276652111 290 ExitRoom();
ThomasBGill 4:6482ceb08dc8 291
ThomasBGill 2:0b0311609edc 292 int rn = rand()%20 + 20;
ThomasBGill 2:0b0311609edc 293
ThomasBGill 2:0b0311609edc 294 for(int i = rn; i>0; i--) {
ThomasBGill 2:0b0311609edc 295 DungeonRoomBuilder();
ThomasBGill 2:0b0311609edc 296 }
ThomasBGill 2:0b0311609edc 297
ThomasBGill 5:9bd276652111 298
ThomasBGill 4:6482ceb08dc8 299
ThomasBGill 3:1a25939df22a 300 DrawMap();
ThomasBGill 4:6482ceb08dc8 301 wait(2.0);
ThomasBGill 2:0b0311609edc 302
ThomasBGill 3:1a25939df22a 303 Maze();
ThomasBGill 3:1a25939df22a 304
ThomasBGill 2:0b0311609edc 305 }
ThomasBGill 2:0b0311609edc 306
ThomasBGill 2:0b0311609edc 307 void World()
ThomasBGill 2:0b0311609edc 308 {
ThomasBGill 2:0b0311609edc 309 Walls();
ThomasBGill 6:ca5db4353c95 310
ThomasBGill 3:1a25939df22a 311 DungeonBuilder();
ThomasBGill 6:ca5db4353c95 312
ThomasBGill 0:9200b3c387ed 313 DrawMap();
ThomasBGill 1:0f774d41584c 314
ThomasBGill 0:9200b3c387ed 315 }
ThomasBGill 0:9200b3c387ed 316
ThomasBGill 0:9200b3c387ed 317 int main()
ThomasBGill 0:9200b3c387ed 318 {
ThomasBGill 1:0f774d41584c 319 //Power Saving
ThomasBGill 1:0f774d41584c 320 PHY_PowerDown ();
ThomasBGill 1:0f774d41584c 321 int result = semihost_powerdown();
ThomasBGill 1:0f774d41584c 322
ThomasBGill 1:0f774d41584c 323 //Generate random seed
ThomasBGill 0:9200b3c387ed 324 srand(Noise*1000000);
ThomasBGill 1:0f774d41584c 325
ThomasBGill 1:0f774d41584c 326 //Initilize screen
ThomasBGill 0:9200b3c387ed 327 lcd.init();
ThomasBGill 0:9200b3c387ed 328
ThomasBGill 1:0f774d41584c 329 //Game loop
ThomasBGill 0:9200b3c387ed 330 while(1) {
ThomasBGill 3:1a25939df22a 331 World();
ThomasBGill 4:6482ceb08dc8 332 wait(4.0);
ThomasBGill 0:9200b3c387ed 333 }
ThomasBGill 0:9200b3c387ed 334 }