Good morning everyone Currently I am attempting a small maze game, however I am tripping up at the first hurdle - generating a maze.
The following code is supposed to recursively create a maze, nothing more, nothing less. However, it randomly gives up with the emulator telling me that it has attempted to write to flash. (Attempt to write to flash (0x29c14c4) on PC=0x1728)
Can anyone seen an obvious error? a simple typo perhaps?
#include "Pokitto.h"
#include "smile.h"
// Globals for Maze
#define MAZEWIDTH 51 // Must be odd
#define MAZEHEIGHT 51 // Must be odd
uint8_t theMaze[MAZEHEIGHT*MAZEWIDTH]; // The maze itself
uint8_t mazeStack[MAZEHEIGHT*MAZEWIDTH]; // Stack for generating maze
// precalculated random list of possible directions
uint8_t posDirs[24][4]={
{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},
{0,3,1,2},{0,3,2,1},{1,0,2,3},{1,0,3,2},
{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},
{2,3,0,1},{2,3,1,0},{3,0,1,2},{3,0,2,1},
{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}
};
void recursion(int mx,int my){
Pokitto::Core::update();
wait_ms(25);
uint8_t directionSet = random(23);
for(uint8_t d=0; d<3; d++){
uint8_t direction = posDirs[directionSet][d];
switch(direction){
case 0: // up
if(my-2>0 && theMaze[mx+MAZEWIDTH*(my-2)] != 0){
my = my - 2;
theMaze[mx+MAZEWIDTH*my] = 0; // next tile clear
theMaze[mx+MAZEWIDTH*(my+1)] = 0; // path to next tie clear
Pokitto::Display::drawPixel(mx, my, 7);
Pokitto::Display::drawPixel(mx, my+1, 7);
recursion(mx,my); // keep trying
}
break;
case 1: // down
if(my+2<MAZEHEIGHT && theMaze[mx+MAZEWIDTH*(my+2)] != 0){
my = my + 2;
theMaze[mx+MAZEWIDTH*my] = 0; // next tile clear
theMaze[mx+MAZEWIDTH*(my-1)] = 0; // path to next tie clear
Pokitto::Display::drawPixel(mx, my, 7);
Pokitto::Display::drawPixel(mx, my-1, 7);
recursion(mx,my); // keep trying
}
break;
case 2: // left
if(mx-2>0 && theMaze[(mx-2)+MAZEWIDTH*my] != 0){
mx = mx - 2;
theMaze[mx+MAZEWIDTH*my] = 0; // next tile clear
theMaze[(mx+1)+MAZEWIDTH*my] = 0; // path to next tie clear
Pokitto::Display::drawPixel(mx, my, 7);
Pokitto::Display::drawPixel(mx+1, my, 7);
recursion(mx,my); // keep trying
}
break;
case 3: // right
if(mx+2<MAZEWIDTH && theMaze[(mx+2)+MAZEWIDTH*my] != 0){
mx = mx + 2;
theMaze[mx+MAZEWIDTH*my] = 0; // next tile clear
theMaze[(mx-1)+MAZEWIDTH*my] = 0; // path to next tie clear
Pokitto::Display::drawPixel(mx, my, 7);
Pokitto::Display::drawPixel(mx-1, my, 7);
recursion(mx,my); // keep trying
}
break;
default:
break;
}
}
}
void makeMaze(){
// fill maze with walls to reset it
for(int y = 0; y< MAZEHEIGHT; y++){
for(int x = 0; x<MAZEWIDTH; x++){
theMaze[x+MAZEWIDTH*y] = 1; // 1 = solid wall
}
}
// Start in random position and set that tile as open space
uint8_t startX = (random(MAZEWIDTH/2)*2)+1;
uint8_t startY = (random(MAZEHEIGHT/2)*2)+1;
theMaze[startX+MAZEWIDTH*startY] = 1;
uint8_t mx = startX;
uint8_t my = startY;
recursion(mx,my);
Pokitto::Display::drawPixel(mx, my, 5);
}
int main(){
using PC=Pokitto::Core;
using PD=Pokitto::Display;
PC::begin();
PD::persistence = true;
PD::invisiblecolor = 0;
srand(time(0));
makeMaze();
PD::drawBitmap(rand()%(PD::width-32), rand()%(PD::height-32), Smile);
while( PC::isRunning() ){
if( !PC::update() )
continue;
for(int y = 0; y< MAZEHEIGHT; y++){
for(int x = 0; x<MAZEWIDTH; x++){
Pokitto::Display::drawPixel(x, y, theMaze[x+MAZEWIDTH*y]);
}
}
//PD::drawBitmap(rand()%(PD::width-32), rand()%(PD::height-32), Smile);
}
return 0;
}