As far as i thought, this should work?? there is a spot or 2 i am concerned if i did it right though. Any ideas would be nice.
It fatally crashes when ran in windows, didnt try in linux due to the fact i know it wouldnt work there either im sure.
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
struct node {
node * prev;
int x;
int y;
char marker;
};
class stack {
public:
stack ();
void push (int, int, char);
void pop ();
int coordinates (int&, int&);
private:
node * top;
};
stack::stack () {
top = NULL;
top -> x = 0;
top -> y = 5;
}
void stack::push (int x, int y, char spotmarker) { // add a new node onto the stack
if (top == NULL) {
node * temp = new node;
temp -> x = x;
temp -> y = y;
temp -> marker = spotmarker;
temp -> prev = top;
top = temp;
}
else {
node * temp = new node;
temp -> x = x;
temp -> y = y;
temp -> marker = spotmarker;
temp -> prev = NULL;
top = temp;
}
}
void stack::pop () { // remove the node off the stack
node * temp = top;
top = temp -> prev;
delete temp;
}
int stack::coordinates(int &positionX, int &positionY) { // used to find current position
positionX = top -> x;
positionY = top -> y;
}
int main () {
stack myStack; // creates instance of stack class
char maze[10][10];
int x, y;
int xCurrent, yCurrent;
ifstream file;
ofstream output;
file.open ("maze.txt");
for (y = 0; y < 10; y++){
for (x = 0; x < 10; x++){
file >> maze[x][y];
cout << maze[x][y];
}
cout << endl;
}
file.close ();
cout << endl;
myStack.push('*', 0, 5);
while (xCurrent < 10 && yCurrent < 10){
(x, y) = myStack.coordinates(x, y); // IS THIS LEGAL?? IF NOT HOW DO YOU FIX IT?
if(maze[x+1][y] == 'X' || maze[x+1][y] == ' '){
x = x+1;
maze[x][y] = '*';
myStack.push('*', x, y);
}
else if(maze[x][y+1] == 'X' || maze[x][y+1] == ' '){
y = y+1;
maze[x][y] = '*';
myStack.push('*', x, y);
}
else if(maze[x][y-1] == 'X' || maze[x][y-1] == ' '){
y = y-1;
maze[x][y] = '*';
myStack.push('*', x, y);
}
else if(maze[x-1][y] == 'X' || maze[x-1][y] == ' '){
x = x-1;
maze[x][y] = '*';
myStack.push('*', x, y);
}
else{
myStack.pop();
}
xCurrent = x;
yCurrent = y;
}
output.open ("pathtaken.dat");
for (y = 0; y < 10; y++){
for (x = 0; x < 10; x++){
output << maze[x][y];
cout << maze[x][y];
}
cout << endl;
}
output.close ();
cin >> x;
return 0;
}