The program needs to read a file, in which position are mentioned in form of 0 and 1, and location (i,j), of the robot, who has to go the end point has to, generate numbers, randomly, BUT ALSO STORE THOSE LOCATIONS SO THAT THE ROBOT DON'T GOES TO THE SAME LOCATION.

This the location file:// we will delete the sentences in the file while we finish the program it, is just to give a location hint.
-- To north
0 0 0 1 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 1 0 0 1 1
0 1 1 0 0 0 1 0 0 0 1 1
1 0 1 0 0 0 1 0 0 0 0 1
0 1 0 1 0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0
--To east
0 0 0 1 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 1 1 0 1 0
0 0 0 1 1 1 1 1 1 1 0 0
1 0 1 1 1 0 0 0 0 0 0 0
1 1 0 1 1 1 0 0 0 1 1 0
0 1 1 0 0 0 0 0 1 1 0 0
--To south
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 1 0 0 1 1
0 1 1 0 0 0 1 0 0 0 1 1
1 0 1 0 0 0 1 0 0 0 0 1
0 1 0 1 0 0 0 0 1 0 1 0
--To west
0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 1 1 0 1
0 0 0 0 1 1 1 1 1 1 1 0
1 1 0 1 1 1 0 0 0 0 0 0
0 1 1 0 1 1 1 0 0 0 1 1
0 0 1 1 0 0 0 0 0 1 1 0

My code:

#include <cstdlib>
#include <iostream>
#include <fstream>


using namespace std;

// Those are global variables
int directionNorth = 0;
int directionEast = 1;
int directionSouth = 2;
int directionWest = 3;


/*
This function picks randomly the next direction to take
C++ does have function which returns (pseudo)random numbers
but we are going to write one ourselves
*/
int pickRandomly(int current){
// First pick any two random numbers of type long (some sort of int)
int a = 9813;
int b = 3142;

return (current * b) % a;
}

int pickDirection(int randomNumber){
return randomNumber%5;
}

/*
Function gets coordinates (i,j) and calculates index of position (i,j)
*/
int coordinateToIndex(int i, int j){
return j*4 + i;
}

/*
Function gets position index and returns the coordinate (i,j)
*/
void indexToCoordinate(int k, int & i, int &j){
i = k % 4;
j = k /4 ;
}

/*
(i,j) are the current coordinates
and will be updatd after the move (when it is valid)
*/
bool moveTo(int &i , int &j, int direction,
bool toNorth[],bool toEast[], bool toSouth[], bool toWest[] ){

// convert the coordinates (i,j) into position index
int k = coordinateToIndex(i,j);
if(direction==directionNorth){
if(toNorth[k]==true){
// from (i,j) it is possible to go north;
j++;
return true;
}else{
// there is a wall at north position from (i,j)
return false;
}
}else{
if(direction==directionEast){
if(toEast[k]==true){
i++;
return true;
}else{
return false;
}
}else{
if(direction==directionSouth){
if(toSouth[k]==true){
j--;
return true;
}else{
return false;
}
}else{
if(toWest[k]==true){
i--;
return true;
}else{
return false;
}
}
}
}

}


int main(int argc, char *argv[])
{
int i, j, k;

bool toNorth[16];
/*
toNorth[k] is true when from position k it is possible to north
toNorth[k] is false when we cannot go north
*/

bool toEast[16];
bool toSouth[16];
bool toWest[16];



ifstream fin;

ofstream fout;

int exitPosition=13;
int direction;
bool validMove;
int counter = 0;
int myRand;
int wallAhead=0;

fin.open("maze.txt"); // open file
if(fin.fail()){
cout << "cannot find the file " << endl;
system("PAUSE");
return -1;
}
for(k=0;k<16;k++){
fin >> toNorth[k];
}
for(k=0;k<16;k++){
fin >> toEast[k];
}
for(k=0;k<16;k++){
fin >> toSouth[k];
}
for(k=0;k<16;k++){
fin >> toWest[k];
}
fin.close();

// The four arrays (describing the maze structure) are now set

// request the initial position
do{
cout << "Enter initial position (i,j)";
cin >> i >> j;
}while( !(i>=0 && i<=3 && j>=0 && j<=3));



// the initial coordinates are valid
k = coordinateToIndex(i,j);


fout<< "Starting from : " << i << " "<<j <<endl;

myRand=109;


while( k!= exitPosition){

direction = pickDirection(myRand);

if(direction==directionNorth){
fout << "Pick randomly : North"<<endl;
}
if(direction==directionEast){
fout << "Pick randomly : East"<<endl;
}
if(direction==directionWest){
fout << "Pick randomly : West"<<endl;
}
if(direction==directionSouth){
fout << "Pick randomly : South"<<endl;
}


validMove = moveTo(i,j, toNorth, toEast, toSouth, toWest, direction);
if(validMove==true){
fout << "New coodinates=" << i << "," << j<<endl;
k = coordinateToIndex(i,j);
counter++;
}else{
wallAhead++;
fout << "There is a wall ahead"<<endl;
}
}


fout << "The robot is at the exit after "<<counter << " steps." <<endl;
fout << "Number of times, it hits the wall " << wallAhead<< endl;
fout.close();

cout << "All results are in output file"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

There are some errors in the program, which I am not able to figure out, and I am not able to store the position that the robot already took, and thus for example, computer generates(2,4), the computer generates the same number again and again, which shoudn't be happening.

Edited 6 Years Ago by Nick Evan: Added code-tags

This is an aweful lot of code for someone to read through looking for who knows what. You might get better responses if you narrow down the problem a bit and ask specific questions.

This article has been dead for over six months. Start a new discussion instead.