All pastes #2088478 Raw Edit

Mine

public cpp v1 · immutable
#2088478 ·published 2011-10-09 21:29 UTC
rendered paste body
void Maze::hayStack(){	char cur_char; //The current character	int new_row, new_col; //temporary coordinates to use for evaluation next position	bool go_back = false;	Stack* path = new Stack; //The stack we are using to keep track of the path	Square cur_square; //The current square at the top of the stack	Square new_square; //The new square we are evaluating. Will become cur_square	//Set up the cur_square and push it. This is the start square	cur_square.setColNum(0);	cur_square.setRowNum(0);	cur_square.setPrevDir(UP);	if(!path->isFull()){		path->push(cur_square);	}else{		cerr << "Error 1. Stack was unexpectedly full" << endl;	}	while(cur_char != 'E'){		cur_char = map[cur_square.getRowNum()][cur_square.getColNum()]; //Set the current character		go_back = false;		while(!go_back && cur_char != 'E'){			switch(new_square.getPrevDir()){			case UP:				new_row = cur_square.getRowNum() - 1;				new_col = cur_square.getColNum();				if(new_row < 0 || new_row >= MAZE_HEIGHT ||	//Check to see if new_row is in bounds					new_col < 0 || new_col >= MAZE_WIDTH || //Check to see if new_col is in bounds					map[new_row][new_col] == '#' || 		//Checks to see if the new position is a wall					cur_square.getPrevDir() == DOWN){		//Checks to see if this direction is going backwards					new_square.setPrevDir(RIGHT);			//Cycle through to the next direction				}else{					new_square.setRowNum(new_row);			//If the new position is a valid position, set the row					new_square.setColNum(new_col);			//and the column to new_row and new_column					if(!path->isFull()){						//check to see if stack is full						path->push(new_square);				// push the new_square to the stack					}else{						cerr << "Error 1. Stack was unexpectedly full" << endl; //output error message if stack is full						return;					}					cur_square = new_square;				//Copes new_square to cur_square					Square tmp;								//Creates a new Square					new_square = tmp;						//Copies tmp to new_square				}				break;			case RIGHT:				new_row = cur_square.getRowNum();				new_col = cur_square.getColNum() + 1;				if(new_row < 0 || new_row >= MAZE_HEIGHT ||	//Check to see if new_row is in bounds					new_col < 0 || new_col >= MAZE_WIDTH || //Check to see if new_col is in bounds					map[new_row][new_col] == '#' || 		//Checks to see if the new position is a wall					cur_square.getPrevDir() == LEFT){		//Checks to see if this direction is going backwards					new_square.setPrevDir(DOWN);			//If it's a wall, cycle through to the next direction				}else{					new_square.setRowNum(new_row);			//If the new position is a valid position, set the row					new_square.setColNum(new_col);			//and the column to new_row and new_column					if(!path->isFull()){						//check to see if stack is full						path->push(new_square);				// push the new_square to the stack					}else{						cerr << "Error 1. Stack was unexpectedly full" << endl; //output error message if stack is full						return;					}					cur_square = new_square;				//Copes new_square to cur_square					Square tmp;								//Creates a new Square					new_square = tmp;						//Copies tmp to new_square				}				break;			case DOWN:				new_row = cur_square.getRowNum() + 1;				new_col = cur_square.getColNum();				if(new_row < 0 || new_row >= MAZE_HEIGHT ||	//Check to see if new_row is in bounds					new_col < 0 || new_col >= MAZE_WIDTH || //Check to see if new_col is in bounds					map[new_row][new_col] == '#' || 		//Checks to see if the new position is a wall					cur_square.getPrevDir() == UP){		//Checks to see if this direction is going backwards					new_square.setPrevDir(LEFT);			//If it's a wall, cycle through to the next direction				}else{					new_square.setRowNum(new_row);			//If the new position is a valid position, set the row					new_square.setColNum(new_col);			//and the column to new_row and new_column					if(!path->isFull()){						//check to see if stack is full						path->push(new_square);				// push the new_square to the stack					}else{						cerr << "Error 1. Stack was unexpectedly full" << endl; //output error message if stack is full						return;					}					cur_square = new_square;				//Copes new_square to cur_square					Square tmp;								//Creates a new Square					new_square = tmp;						//Copies tmp to new_square				}				break;			case LEFT:				new_row = cur_square.getRowNum();				new_col = cur_square.getColNum() - 1;				if(new_row < 0 || new_row >= MAZE_HEIGHT ||	//Check to see if new_row is in bounds					new_col < 0 || new_col >= MAZE_WIDTH || //Check to see if new_col is in bounds					map[new_row][new_col] == '#' || 		//Checks to see if the new position is a wall					cur_square.getPrevDir() == RIGHT){		//Checks to see if this direction is going backwards					new_square.setPrevDir(NONE);			//If it's a wall, cycle through to the next direction				}else{					new_square.setRowNum(new_row);			//If the new position is a valid position, set the row					new_square.setColNum(new_col);			//and the column to new_row and new_column					if(!path->isFull()){						//check to see if stack is full						path->push(new_square);				// push the new_square to the stack					}else{						cerr << "Error 1. Stack was unexpectedly full" << endl; //output error message if stack is full						return;					}					cur_square = new_square;				//Copes new_square to cur_square					Square tmp;								//Creates a new Square					new_square = tmp;						//Copies tmp to new_square				}				break;			case NONE:	//This should only be reached if we're having to back up a bit			default:				path->pop(new_square);		//Point new_square to the top of the stack				path->pop(cur_square);		//Set cur_square to the new top of the stack				path->push(cur_square);		//Put it back on the stack				switch(new_square.getPrevDir()){	//Advance new_square's direction to the next one				case UP:					new_square.setPrevDir(RIGHT);					break;				case RIGHT:					new_square.setPrevDir(DOWN);					break;				case DOWN:					new_square.setPrevDir(LEFT);					break;				case LEFT:					new_square.setPrevDir(NONE);					break;				case NONE:				default:					go_back = true;					break;				}				break;			}			cur_char = map[cur_square.getRowNum()][cur_square.getColNum()]; //Set the current character			if(cur_char == 'E'){				Square tmp;				while(!path->isEmpty()){					path->pop(tmp);					cout << "(" << tmp.getRowNum() << "," << tmp.getColNum() << ")" << endl;				}			}		}	}	if(cur_char == 'E'){		Square tmp;		while(!path->isEmpty()){			path->pop(tmp);			cout << "(" << tmp.getRowNum() << "," << tmp.getColNum() << ")" << endl;		}	}	delete path;}