//recursive function (with end conditions and backtracing) to find the solution path (recall that y and x are swapped due to how apmatrixes work) bool findpath(apmatrix<char> &grid, int y, int x, int &turns){ //evaluates whether the tile stepped on is valid, invalid or the goal. if (grid[y][x] == 'X') return false; if (grid[y][x] == '#') return false; if (grid[y][x] == '+') return false; if (grid[y][x] == 'G') return true; if (grid[y][x] != 'S') grid[y][x] = '+'; turns++; display(grid); //first checks if the move will be outside of the maze, if not - check to see if its valid if (y != 0) if (findpath(grid,y-1,x,turns) == true) return true; if (x+1 < grid.numcols()) if (findpath(grid,y,x+1,turns) == true) return true; if (y+1 < grid.numrows()) if (findpath(grid,y+1,x,turns) == true) return true; if (x != 0) if (findpath(grid,y,x-1,turns) == true) return true; //backtraces and marks invalid paths with an X. grid[y][x] = 'X'; turns--; return false; }
//Initializes allegro and the graphical window. void init(apmatrix<char>matrix) { allegro_init(); install_keyboard(); install_mouse(); install_timer(); set_color_depth(desktop_color_depth()); set_gfx_mode(GFX_AUTODETECT_WINDOWED,matrix.numcols()*20, matrix.numrows()*20, 0, 0); set_window_title("A-Maze-ing Recursion - Kevin Dai"); }
//Uses allegro to output the maze and mark the walls,valid path, invalid path and ETC. int display(apmatrix<char> omatrix) { for (int i=1 ; i < omatrix.numrows()+1 ; i++ ) { for (int j=1 ; j < omatrix.numcols()+1 ; j++){ if (omatrix[i-1][j-1] == '#') floodfill(buffer,j*20-5,i*20-5,makecol(0,0,0)); //paint the walls else if (omatrix[i-1][j-1] == '.')floodfill(buffer,j*20-5,i*20-5,makecol(255,255,255)); //paint the open else if (omatrix[i-1][j-1] == 'X')textout_centre_ex(buffer, font, "X", j*20-9, i*20-12, makecol(255, 0, 0), -1); //paint the start else if (omatrix[i-1][j-1] == 'S'){ floodfill(buffer,j*20-5,i*20-5,makecol(255,255,255)); //paint the open textout_centre_ex(buffer, font, "S", j*20-9, i*20-12, makecol(255, 0, 0), -1); //paint the start } else if (omatrix[i-1][j-1] == 'G'){ floodfill(buffer,j*20-5,i*20-5,makecol(255,255,255)); //paint the open textout_centre_ex(buffer, font, "G", j*20-9, i*20-12, makecol(0, 0, 255), -1); //paint the end } else if (omatrix[i-1][j-1] == '+')textout_centre_ex(buffer, font, "+", j*20-9, i*20-12, makecol(0, 0, 255), -1); //paint the succesful path } } blit(buffer, screen, 0,0,0,0,omatrix.numcols()*20,omatrix.numrows()*20); Sleep(1000); return 0; }
// Show all stops inside a certain radius apvector <ID> withinOneRadius(apvector <apstring> & id, apmatrix <int> & dist, double rad, apstring theid) { int n = id.length(); // double rad; // cout << "Enter the desired radius to be searched in: "; // ?? should radius be *100 or not?? // cin >> rad; // apstring theid; // we hate getline // cout << "Enter the stop ID for which you would like the radii: "; // getline(cin, theid); // theid = "2051051WB007 "; // now should search the stopID file for the value of the node in id int row = 0; while ( theid != id[row] ) // assumes that id1 was entered correctly { // cout << id[row]; row++; } apvector <ID> withinRad(n + 1); ID temp; int col, count = 0; for ( col = 0; col < dist.numcols(); col++) { if ( convertDistance(dist[row][col]) <= rad ) { temp.stopID = id[col]; temp.distance = convertDistance(dist[row][col]); // the distance is dist[row][col], will eventually be added to a data structure? // cout << id[col] << " is within the radius with distance of " << convertDistance(dist[row][col]) << endl; withinRad[count] = temp; count ++; } } withinRad.resize(count + 1); // resize the array to be smaller and not wasteful return withinRad; }
// Show all stops inside a certain radius for all stops apvector <apvector <ID> > withinAllRadius(apvector <apstring> & id, apmatrix <int> & dist, double rad) { int n = id.length(); // double rad; // cout << "Enter the desired radius to be searched in: "; // ?? should radius be *100 or not?? // cin >> rad; int row = 0; int col; int count = 0; apvector <ID> withinRad(n + 1); ID temp; apvector <apvector <ID> > allRadii(n + 1); for ( row = 0; row < dist.numrows(); row++) { for ( col = 0; col < dist.numcols(); col++) { if ( convertDistance(dist[row][col]) <= rad && row != col) { temp.stopID = id[col]; temp.distance = convertDistance(dist[row][col]); // the distance is dist[row][col], will eventually be added to a data structure? // cout << convertDistance(dist[row][col]) << '\t'; withinRad[count] = temp; count ++; } } allRadii[row] = withinRad; // cout << endl << "--------------------------------------------" << endl; } return allRadii; }