Hey. I'm working on a project which i have posted different parts about. What it does, is the program inputs a notepad file which holds a binary map made of 0s and 1s. The 0s are traversible and the 1s are walls. My map looks like this :
My program takes that data and puts it into a 2d matrix. Then, it initializes a start position 1,1 being the upper left 0. It then scans to its right, left, up and down and decides if there is an opening. If there is, it sets that decision to the value 1. I then generate a random number between 1 and 4. If the decision is available and the random number cooresponding to that decision is generated, then xpos and ypos adjust accordingly. The process stops when xpos and ypos both equal 9, the bottom right 0 of the map. Here is the code. I think i may have two problems. 1. i think that a loop may not be acting right but i just cant figure it out. and 2. maybe i dont have the coordinants numbered right, like 9,9 doesnt coorespond to the bottom left 0. Take a look and let me know if you have any thoughts.
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
usingnamespace std;
//
//
//
void printmatrix(char print[10][10]){
for (int i=0;i<10;i++){
for(int k=0;k<10;k++){
cout<<print[i][k];
}
cout <<"\n";}
}
int main(int argc, char** argv) {
//start copying file to a 2d array
string rowbuild;
char maze[10][10];
int rownum=0;
//begin file io
ifstream mazefile ("maze.txt");
while(rownum<10){
getline(mazefile,rowbuild);
for(int n=0;n<10;n++){
maze[rownum][n]=rowbuild[n];}
rownum++;
}
printmatrix(maze);
cout << maze[1][1];
int xpos=1; //provides lateral position init at 1
int ypos=1; //provides vertical position init at 1
while (xpos!=9&&ypos!=9){
//provides random decision point
int dec1=0;
int dec2=0;
int dec3=0;
int dec4=0;
int foundmatch=0;
//this snippet generates the possible decisions
if (maze[xpos+1][ypos]==0){
dec1=1;
}
if(maze[xpos-1][ypos]==0){
dec2=1;
}
if(maze[xpos][ypos+1]==0){
dec3=1;
}
if(maze[xpos][ypos-1]){
dec4=1;
}
//end getting number of decisions
do{
int dectake;
srand ( time(NULL) );
dectake= rand() % 4 + 1;
if(dectake==1&&dec1==1){
foundmatch=1;
xpos++;
}
if(dectake==2&&dec2==1){
foundmatch=1;
xpos--;
}
if(dectake==3&&dec3==1){
foundmatch=1;
ypos++;
}
if(dectake==4&&dec4==1){
foundmatch=1;
ypos--;
}
} while (foundmatch!=1);
}
cout << "I made it to the end of the maze!";
return (EXIT_SUCCESS);
}