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;
}
}
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;
}
}