Rat Maze

  • Thread starter pleatofthepants
  • Start date
P

pleatofthepants

How do I change the position to a different character so the rat will
not visit the same place over and over again?

/*

*/
#include <fstream>
#include <iostream>
#include "Matrix.h"
using namespace std;


struct Location
{
int r, c;
Location()
{
r = c = 0;
}
};
class Maze: public Matrix
{
private:

Location rat;




public:

Maze(int r = 10, int c = 10): Matrix(r, c){}
void moveRat(int r, int c);
ostream& draw(ostream&);
void solve(int r, int c);



};
/////////////////////////////////////////////
int main()
{
ifstream file;
Maze *maze;
int r, c;
char data;
file.open("maze.txt");
file >> r >> c;
maze = new Maze(r, c);
file >> r >> c;
maze->moveRat(r, c);

for ( r = 0; r < maze->getRows(); r++)
{
for ( c = 0; c < maze->getColumns(); c++)
{
file >> data;
(*maze)[r][c] = data-'0';
}
}

maze->draw(cout);
maze->solve(r, c);
cout << '\n';
cout << '\n';
maze->draw(cout);


return 0;
}
/////////////////////////////////////////////

void Maze :: moveRat(int r, int c)
{
rat.r = r;
rat.c = c;
}


ostream& Maze :: draw(ostream& out)
{

for ( int r = 0; r < rows; r++)
{
for ( int c = 0; c < cols; c++)
{
if(r == rat.r && c == rat.c)
{
out << 'R';
}
else
{
out << element[r][c];
}
}
out << endl;
}
//out << element[9][9];
return out;
}


void Maze :: solve(int r, int c)
{
//r = rat.r;
//c = rat.c;
if ( element[rat.r + 1][rat.c] == 0)
{
cout << "move up" << endl;
moveRat(rat.r + 1, rat.c);
//element[rat.r][rat.c] = 'V';
solve(rat.r + 1, rat.c);
}

if ( element[rat.r][rat.c + 1] == 0)
{
cout << "move right" << endl;
moveRat(rat.r, rat.c + 1);
//element[rat.r][rat.c] = 'V';
solve(rat.r, rat.c + 1);
}

if ( element[rat.r - 1][rat.c] == 0)
{
cout << "move down" << endl;
moveRat(rat.r - 1, rat.c);
//element[rat.r][rat.c] = 'V';
solve(rat.r -1, rat.c);
}

if ( element[rat.r][rat.c - 1] == 0)
{
cout << "move left" << endl;
moveRat(rat.r, rat.c - 1);
//element[rat.r][rat.c] = 'V';
solve(rat.r, rat.c - 1);
}

else
{
cout << "You have nowhere to go rat!" << endl;
}
}
 
A

adramolek

How do I change the position to a different character so theratwill
not visit the same place over and over again?

One way to do it is to keep track of where the rat has been in the
current test path, and not move there. It looks like you started to do
this but gave up. You were missing one extra step. It will be the same
for all cases but in the case of left:

if ( element[rat.r][rat.c - 1] == 0)
{
moveRat(rat.r, rat.c - 1);
// store 'V' to indicate rat has been here
element[rat.r][rat.c] = 'V';
solve(rat.r, rat.c - 1);
// reset back to 0 after solving!
element[rat.r][rat.c] = 0;
}

You were missing the reset back to 0. After you have traced a path,
the rat has no longer visited that spot in that particular path -- the
'V' marker is only temporary while traveling a given route.

Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top