Knight's Tour C++ using stack - c++

I am currently working on Knight tour Chessboard game in c++ using Stack to store my move. I encounter a weird loop that does not end the program. Can anybody help me with the code?
#include <iostream>
#include <stack>
#include <map>
#include <cstdlib>
using namespace std;
struct whereIam
{
int row, col;
};
struct L_shape_pattern
{
int Lrow[8]={1,1,2,2,-1,-1,-2,-2};
int Lcol[8]={2,-2,1,-1,2,-2,1,-1};
};
bool check_if_valid(int row, int col)
{
if ((row >= 0 && col >= 0) && (row < 8 && col < 8))
{
// cout << "here in valid " <<endl;
return true ;
}
else
return false;
}
bool check_empty(bool board[8][8], whereIam position)
{
// if (board[position.row][position.col] == false)
// return false;
// else
// return true;
if (board[position.row][position.col] == true)
{
// cout << "here in check empty" <<endl;
return true;
}
else
return false;
}
bool isReady(whereIam &position,bool board[8][8])
{
// cout << "here" << endl;
int ready = 0;
for (int i = 0 ; i < 8 ; i ++)
{
for (int j = 0 ; j < 8 ; j++)
{
if(board[i][j] == false)
{
ready += 1;
}
}
}
cout << "ready: " <<ready << endl;
if (ready == 64)
{
cout << "done" << endl;
return true;
}
else
return false;
}
void findspot(whereIam &position,bool board[8][8], stack<whereIam> &sequence)
{
L_shape_pattern Lshape;
// stack<whereIam> initial;
stack<int> counter;
for (int j = 0 ; j< 9 ;j++)
{
//nothing is assign here
if (check_if_valid(position.row+Lshape.Lrow[j],position.col+Lshape.Lcol[j]) /*&& check_empty(board,position)*/)
{
// cout << "here in valid in spot " <<endl;
whereIam hello;
hello.row = position.row+Lshape.Lrow[j];
hello.col = position.col+Lshape.Lcol[j];
// cout << hello.row << " " << hello.col << endl;
if (check_empty(board,hello))
{
// cout << "here in empty" <<endl;
// int possible_row = position.row+Lshape.Lrow[j];
// int possible_col = position.col+Lshape.Lcol[j];
// position.row = possible_row;
// position.col = possible_col;
position.row = hello.row;
position.col = hello.col;
sequence.push(position);
// initial.push(position);
// cout << position.row << " " << position.col << endl;
counter.push(j);
board[position.row][position.col] = false;
j = -1;
if (isReady(position,board) == true)
{
cout << "in if ready" << endl;
exit(0);
}
}
}
if (j == 8 )
{
// cout << "here in j = 8" <<endl;
board[position.row][position.col] = true;
// cout << " pop board " << position.row <<" " << position.col << endl;
sequence.pop();
position = sequence.top();
// increment to the position where it need to be backtracking and it increment by one
j = counter.top();
counter.pop();
if (isReady(position,board) == true)
{
cout << "in if ready" << endl;
exit(0);
}
}
}
}
//bool movetheKnight(whereIam &position,bool board[8][8], stack<whereIam> &sequence)
//{
//}
void open_all_spot( bool board[8][8])
{
for (int i = 0 ; i< 8 ; i++)
for (int j= 0 ; j <8 ; j++)
{
board[i][j] = true;
}
}
int main()
{
bool board[8][8];
open_all_spot(board);
whereIam position;
stack<whereIam> sequence;
cout << "Enter the initial position" << endl;
cout << "row : " ;
cin >> position.row;
cout << "column:";
cin >> position.col;
sequence.push(position);
//assign the initial position to be occupied already
board[position.row][position.col] = false;
findspot(position,board,sequence);
cout << "here end all" << endl;
return 0;
}
Some part I just created to debug and see how each function work so ignore those part.
The loop always goes on and never seems to end at all. I tried to track data in the stack but it does seems reasonable to me.
Any help would be appreciated.

When looking at this part of your code:
for ( int j = 0; j < 9; j++ ) {
if ( check_if_valid(position.row+Lshape.Lrow[j],position.col+Lshape.Lcol[j]) /*&& check_empty(board,position)*/) {
// code...
if ( checkEmpty( board, hello ) {
// code...
j = -1;
if ( isReady(position, board) == true ) { // == true not needed
// code...
}
}
}
if ( j == 8 ) {
// code...
j = counter.top()
// code...
if ( isReady(position, board) == true ) { // == true not needed
// code...
}
}
} // for loop
Think about what happens in the 1st nested if statement within the for loop when the condition is returned true. You are changing j to be -1.
Again in the 2nd if statement within the for loop if j==8 you again are changing j to be counter.top().
This behavior will cause infinite recursion of the for loop. Here is a simple example:
#include <iostream>
#include <iomanip>
int main() {
int counter = 0;
for ( int i = 0; i < 5; i++ ) {
if ( i == 4 ) {
i = 0;
}
++counter;
std::cout << "Count the recursion: "
<< std::setw( 2 ) << counter << " " << i << '\n';
// just to stop the recursion
if ( counter == 10 ) break;
}
std::cout << "\nPress any key and enter to quit.\n";
std::cin.get();
return 0;
}
The above program without the last if statement will simulate what is happening in your program. I only included that so as to stop the loop just to show the progression of the output.
I don't know if you intentionally want an infinite recursion of a for loop or not; but if you do you need to check your counter variable(s) in a debugger to make sure that they match the value needed to execute the statement involved in exiting the loop to stop the recursion. Just as I have shown in my small example above; without the condition of the counter being equal to 10. The loop would of continued on forever.
As a side note if you do intend to have an infinite loop; it is usually better to structure them in this manner this way it is more clear as to what you intended to do.
int counter = 0;
for ( ; ; ) {
++counter;
// do some work;
if ( counter == exit condition ) break;
}
or
int counter = 0;
for ( ; ; ) {
// do some work;
if work above is valid
increment counter
else
break;
}
or you can use a while loop instead
counter = some value
while ( true ) {
check counter for condition;
do some work
increment or set counter;
}

Related

for loop with nested if statement stops iterating after first iteration becomes true. C++

I have a function that receives a character "x" from a user. If the "x" exists in a hard-coded array, "x" is pushed into another array filled with lowdashes in the same position as in the first array. Fo example:
firstArray = ["h", "e", "l", "l","o"]
//user provided character that exist in first array. "e"
lowDashesArray= ["_", "e", "l", "l", "o"]
Then, I want to check if both arrays are equal, my problem is that when the user guesses the firts element in the array (in the above example "h"), the for loop stops iteraring without checking if the other elements are equal. As a result, the block of code that expects both arrays to be equal is executed.
The function looks like so:
bool checkIfLetterExistOnWord(string word, char letter, char* lettersArray, char* lowDashesArray, bool hasWon, bool hasLost, int lives){
bool isCorrectLetter = false;
for(int i = 0; i<word.length(); i++ ){
if(letter == lettersArray[i]){
int position = i;
lowDashesArray[position] = letter;
cout << lowDashesArray;
cout << endl;
isCorrectLetter = true;
}
}
if(isCorrectLetter){
for(int j = 0; j < word.length(); j++){
if(lettersArray[j] == lowDashesArray[j]){
hasWon = true;
//here is the problem. But only when user guesses the first element of the array
}
else{
break;
}
}
if(hasWon){
cout << "You have won";
cout << endl;
cout << hasWon;
cout << endl;
cout << lowDashesArray;
cout << endl;
cout << word;
cout << endl;
return hasWon;
}
else{
cout << "good job. Guess the next letter";
cout << endl;
return hasWon;
}
}
else{
cout << "wrong";
cout << endl;
lives--;
if(lives == 0){
hasLost = true;
cout << "You lost";
return hasLost;
}
else {
cout << "You still have this lives :";
cout << endl;
cout << lives;
cout << endl;
return hasLost;
}
}
}
Both your loop logic is incorrect.
bool isCorrectLetter = false;
for(int i = 0; i<word.length(); i++ ){
if(letter == lettersArray[i]){
int position = i;
lowDashesArray[position] = letter;
cout << lowDashesArray;
cout << endl;
isCorrectLetter = true;
}
}
Here you print the array once for every occurance of the guess character. FUrthermore you do not check, if the letter "was already uncovered". You need to do the printing after the loop and depending on whether you want guessing uncovered chars to be an error, you may need to ajust the check.
for(int j = 0; j < word.length(); j++){
if(lettersArray[j] == lowDashesArray[j]){
hasWon = true;
//here is the problem. But only when user guesses the first element of the array
}
else{
break;
}
}
You're trying to check here, if every char meets a certain criterion (being a non placeholder char). In this case you cannot break early. In general the loop for this kind of check needs to look like this:
bool checkSuccess = true;
for (size_t i = 0; checkSuccess && i != length; ++i)
{
if (!Check(elements[i]))
{
checkSuccess = false;
}
}
Or in your case (using break instead):
hasWon = true;
for(int j = 0; j < word.length(); j++)
{
if(lettersArray[j] != lowDashesArray[j])
{
hasWon = false;
break;
}
}
Imho it's preferrable to separate the replacement logic from io logic. You could e.g. rewrite the logic similar to this:
constexpr char MaskChar = '_';
/**
* Replaces placeholders in \p maskedText, if they the corresponding char in \p clearText
* matches \p guess.
*
* \param[in,out] maskedText the text with some chars replaced with '_'
*
* \return true, if at least one char was replaced, false otherwise
*/
bool GuessChar(std::string const& clearText, std::string& maskedText, char const guess)
{
assert(clearText.length() == maskedText.length());
bool guessCorrect = false;
for (size_t i = 0; i != clearText.length(); ++i)
{
if (maskedText[i] == MaskChar && clearText[i] == guess)
{
guessCorrect = true;
maskedText[i] = guess;
}
}
return guessCorrect;
}
int main()
{
std::string const clearText = "hello";
std::string maskedText(clearText.length(), MaskChar);
size_t lives = 3;
while (lives > 0 && maskedText.find(MaskChar) != std::string::npos)
{
std::cout << "Word: " << maskedText
<< "\nMake your guess!\n";
char c;
std::cin >> c;
if (GuessChar(clearText, maskedText, c))
{
std::cout << "Guess correct!\n";
}
else
{
--lives;
std::cout << "Guess incorrect\n"
<< lives << " lives left\n";
}
}
if (lives > 0)
{
std::cout << "You win!\n";
}
else
{
std::cout << "You loose!\n";
}
}

How to print all permutation using backtracking?

This code prints all the permutation of N-1 items. But I could not understand one thing:
when n=N, it is returning where it is called and make flag[n-1] = false. Thus, i = N-1 and breaks the loop. But how is the rest of the permutation printing or returning when n=N-2 to 0?
void perm(int n) {
if (n == N) {
for (int i = 0; i < N ; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
for (int i = 0; i < N; i++) {
if (flag[i]) continue;
a[n] = i;
flag[i] = true;
cout<<i<<endl;
perm(n + 1);
cout<<i<<endl;
flag[i] = false;
}
}
You need to consider that function calls ended up nested.
Each indentation below shows a nested call:
main()
entering perm(0)
entering perm(1)
i = 0
entering perm(2)
let's say N is 2, this will print and return
now perm(1) continues
i becomes 1
entering perm(2)
...
Since return only returns from the current function call, not all function calls, the permutations continue printing.
In order to gain familiarity with this, try this:
void perm(int n) {
std::cout << "Entering perm " << n << std::endl;
if (n == N) {
for (int i = 0; i < N ; i++) {
cout<<a[i]<<" ";
}
cout<<endl;
std::cout << "Exiting perm " << n << std::endl;
return;
}
for (int i = 0; i < N; i++) {
if (flag[i]) continue;
a[n] = i;
flag[i] = true;
cout<<i<<endl;
std::cout << "about to call perm" << std::endl;
perm(n + 1);
std::cout << "finished call to perm" << std::endl;
cout<<i<<endl;
flag[i] = false;
}
std::cout << "Exiting perm " << n << " (2)"<< std::endl;
}

How to print a message only once in a loop

I am using for loops combined with if statements to read integers from a text file into a two-dimensional array.
This is my code:
for (int i = 0; i < MAX_ROWS;i++) {
for (int j = 0; j < MAX_COLUMNS; j++) {
inFile >> ArrB[i][j];
if (ArrB[i][j] == -1) {
bad = true;
cout << "The array does not have enough integers" << endl;
break;
}
else {
if (ArrB[i][j] < 1) {
invalidnum = true;
}
}
if (invalidnum = true) {
cout << *(*(ArrB + i) + j) << " ";
cout << "There is/are negative number(s) or zero(s) in the array imported from your text file." << endl;
}
}
}
This code will read in the first 6 integers (max_row * max_column) from a text file into ArrB.
If -1 exists in the first 6 integers, it will exit the loop and print out "The array does not have enough integers".
If there is no -1 in the first 6 integers, then it will check all 6 integers to see if there are any other negative numbers or zero.
If there are negative numbers or zero, I want it to still print out the array, then print out the error message (There is/are negative number(s) or zero(s) in the array imported from your text file) ONLY ONCE.
For example, this is my text file. As you can see, there is no -1 in the first 6 numbers, but there is a -7.
So, ideally, the result should be something like:
2 4 5 6 9 -7
There is/are negative number(s) or zero(s) in the array imported from your text file
But this is what I am getting if I run my code above:
-------------------------------------UPDATE--------------------------------------
Figured it out based on #ZedLepplin 's comment
Here is the code:
for (int i = 0; i < MAX_ROWS;i++) {
for (int j = 0; j < MAX_COLUMNS; j++) {
inFile >> ArrB[i][j];
if (ArrB[i][j] == -1) {
bad = true;
cout << "The array does not have enough integers" << endl;
break;
}
else {
if (ArrB[i][j] < 1) {
invalidnum = true;
}
}
cout << *(*(ArrB + i) + j) << " ";
}
}
if (invalidnum == true) {
cout << "There is/are negative number(s) or zero(s) in the array imported from your text file." << endl;
}
You could just set a counter, and put the message outside of the loop.
Something like :
int counter = 0;
for(int i = 0; i < MAX_ROWS ; i++) {
if(myVector[i] == -1) {
counter++;
}
else {
// Do normal stuff
}
}
if(counter > 0) {
cout << "The array contained " << counter << "negative values" << endl;
}
Ho, and I'd advise to avoid comparisons to "true". If myVar is a boolean alrady, I can just do if(myVar). No need to do if(myVar == true).
And doing if(myVar = true) is worse, as it sets myVar to true, regardless of its initial value. That's a common typo that can be hard to detect when proofreading code.
Edited version (to adapt to comments) :
bool earlyNegativeOneFound = false;
int otherNegativeCounter;
for(int i = 0; i < MAX_ROWS ; i++) {
if(i < 6 && myVector[i] == -1) {
earlyNegativeOneFound = true;
break;
}
else if(myVector[i] < 0) {
cout << myVector[i] << endl;
otherNegativeCounter++;
}
else {
// Do normal stuff
}
}
if(!earlyNegativeOneFound && otherNegativeCounter> 0) {
cout << "The array contained " << otherNegativeCounter << "negative values" << endl;
}
Put the conditional error message print after your for loop. Leave the cout for displaying the array number inside the for loop so it is output for every iteration of the loop.
for (int i = 0; i<MAX_ROWS;i++) {
for (int j = 0; j<MAX_COLUMNS; j++) {
inFile >> ArrB[i][j];
if (ArrB[i][j] == -1) {
bad = true;
cout << "The array does not have enough integers" << endl;
break;
}
else {
if (ArrB[i][j] < 1) {
invalidnum = true;
}
}
cout << * (*(ArrB + i) + j) << " ";
}
}
if (invalidnum = true) {
cout << "There is/are negative number(s) or zero(s) in the array imported from your text file." << endl;
}
I am not sure if this is what you meant
for (int i = 0; i < MAX_ROWS;i++) {
for (int j = 0; j < MAX_COLUMNS; j++) {
flag log = false;
inFile >> ArrB[i][j];
if (ArrB[i][j] == -1) {
bad = true;
cout << "The array does not have enough integers" << endl;
break;
}
else {
if (ArrB[i][j] < 1) {
invalidnum = true;
}
}
if (invalidnum = true) {
cout << *(*(ArrB + i) + j) << " ";
if(!flag)
{
cout << "There is/are negative number(s) or zero(s) in the array
imported from your text file." << endl;
flag = true;
}
}
}
}
Adding the flag boolean variable would allow the statement "there are negative numbers.." to be printed once.
Just store the message and print it where and when you want. Some psuedo-code:
std::string message;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j< M; ++j)
{
if (smth)
{
message {"Your message"};
break; // note that after break,
// you are still in the outer loop
}
else if (smth else)
{
message {"Your message"};
}
}
// print it here
}
// or here, or wherever you want to

Removing and shifting remaining elements in an array C++

List.cpp (class definitions)
I have been working on code that is suppose to help familiarize with classes. My code currently has a function that displays a menu of options for the users. Option [1] is suppose to add a string, option [2] is suppose to remove a string from the list, option [3] prints the string list, option [4] exits. My option [1] seems to work okay as the user is able to input one string at a time but I am having a hard time with the removal of a string. The strings are currently stored in an array of 10 elements. I believe the function I wrote for the string removal is okay as I have debugged it and it seems successful, however, I am not seeing results on my console window.
My array is located in a private class in my class: string items[MAX_ITEMS]; along with another variable called: int totalItems;
The class is then called in my main function using a switch case:
//This code snippet below is located in a separate cpp file with main
cout << "Please enter the text you want to remove: " << endl;
cin >> userInput;
list1.remove(userInput);
////////////////////////////////////////////////////////////////////
//preprocessor directives
#include <iostream>
#include <string>
//header files
#include "list.h"
using namespace std;
List::List()
{
//clear array prior to starting (set everything to NULL)
for (int i = 0; i < MAX_ITEMS; i++)
{
items[i] = " ";
}
totalItems = 0;
}
//void List::init()
//{
// string items[MAX_ITEMS];
// totalItems = 0;
//}
bool List::insert(const string& data)
{
//verifies that string is not empty, not in the list, and not full
if (data.empty() == true || inList(data) == true || isFull() == true)
{
return false;
}
else
{
// items[isFull)] = data;
// totalItems++;
items[totalItems++] = data;
return true;
}
}
bool List::isEmpty() const
{
//runs through loop to verify array is empty
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i].empty() != true)
{
return false;
}
}
return true;
}
//identifies whether the string is full or not
bool List::isFull() const
{
if (totalItems == MAX_ITEMS)
{
return true;
}
else
{
return false;
}
}
//identifies whether the string is already in the list or not
bool List::inList(const string& theList)
{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == theList)
{
return true;
}
}
return false;
}
bool List::remove(const string& data)
{
if (inList(data) == true || data.empty() == true)
{
return false;
}
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == data)
{
items[i] == " ";
for (int j = i; j < MAX_ITEMS; j++)
{
items[j] = items[j + 1];
items[MAX_ITEMS - 1] == " ";
break;
}
}
}
totalItems--;
return true;
}
//prints list
void List::printList()
{
for (int i = 0; i < MAX_ITEMS; i++)
{
cout << i << items[i] << '\t';
}
}
list_test.cpp (main.cpp)
#include <iostream>
#include <string>
//header files
#include "list.h"
using namespace std;
//function prototypes
int showSelection();
int main()
{
List list1;
string userInput = "";
int userChoice;
// list1.init();
userChoice = showSelection();
while (userChoice != 4)
{
switch(userChoice)
{
case 1:
cout << "Please enter the text you want to add: " << endl;
cin >> userInput;
list1.insert(userInput);
/*if (list1.inList(userInput) == false)
{
cout << "Text is already entered in the list!" << endl;
}*/
if (list1.isFull() == true)
{
cout << "You have entered the MAXIMUM amount of elements!" << endl;
}
break;
case 2:
cout << "Please enter the text you want to remove: " << endl;
cin >> userInput;
list1.remove(userInput);
break;
case 3:
cout << "Printed list: " << endl;
list1.printList();
break;
}
userChoice = showSelection();
}
cout << "Goodbye. Please press enter to exit." << endl;
//TESTING PURPOSES FOR FUNCTIONS
cout << list1.insert(userInput) << endl;
cout << list1.isEmpty() << endl;
cout << list1.isFull() << endl;
cout << list1.inList(userInput) << endl;
return 0;
}
/* ===========================================
Name: showSelection
Desc: displays menu for user to choose options
for their inputted string(s).
Args: none
Retn: none
=========================================== */
int showSelection()
{
int userChoice;
bool exit = false;
while (exit == false)
{
cout << "\nTo select an option, please enter the corresponding number: " << endl;
cout << "[1] to add a string" << endl;
cout << "[2] to remove a string" << endl;
cout << "[3] to print a string" << endl;
cout << "[4] to exit" << endl << endl;
cin >> userChoice;
cout << "You entered option: " << userChoice << endl;
cout << '\n';
if (userChoice == 1 || userChoice == 2 || userChoice == 3 || userChoice == 4)
{
exit = true;
}
else
{
cout << "Invalid selection" << endl;
}
}`enter code here`
return userChoice;
}
Here is how you code should probably look:
bool List::remove(const string& data) {
// only check if the list is empty so you don't nececarily go through it
// you shoudn't ask here if the given string is in the list
// because you will search for it anyway just below
if (data.empty())
return false;
for (int i = 0; i < MAX_ITEMS; ++i) {
if (items[i] == data) { // now if it was found
items[i] = " "; // set it to your empty value
--totalItems; // prefix -- is faster then postfix one (no copy is created)
for (int j = i; j < MAX_ITEMS - 1; ++j) {
// stop at j < MAX_ITEMS - 1 because you wouldn't want
// to swap last element with the next because there
// is none behind it
if (items[j + 1] == " ")
// if the next item is already empty you don't need to shift any more
return true;
// swap the next item with much more
// efficient std::swap function
std::swap(items[j], items[j + 1]);
}
return true; // value is removed and items shifted so you can now return
}
}
// if function gets to this point that means the value wasn't found
return false;
}
If you would like to make your code more efficient, I can give you more suggestions on how to do it. This above should answer your question.
Also using an array for a struct like this isn't optimal at all. Using linked listed would mean no shifting would be required.
Edit: replaced long text with a code example
Edit2: added return statement if shifting is no longer necessary
FOR me it's unclear what you want to do as unavailability of full code to run.
But I think this should work as of what I think yo want to do
bool List::remove(const string& data){
for (int i = 0; i < totalItems; i++)
{
if (items[i] == data)
{
for (int j = i; j < totalItems-1; j++)
{
items[j] = items[j+1];
}
totalItems--;
return true;
}
}
return false;
if (inList(data) == true || data.empty() == true)
{
return false;
}
If the data parameter is in your list object, you return without removing anything? this should be !inList(data)
Additionally, when you make it into the loop below this code this loop:
for (int j = i; j < MAX_ITEMS; j++)
{
items[j] = items[j + 1];
items[MAX_ITEMS - 1] == " ";
break;
}
will only execute for j=i, the "break" statement will stop execution of this inner loop, and go back to the outer loop.
EDIT: this is how I personally would go about this problem.
bool List::remove(const string& data)
{
bool retVal;
if (!inList(data) || data.empty())
{
retVal = false;
}
else{
for (int i = 0; i < MAX_ITEMS; i++)
{
if (items[i] == data)
{
items[i] = " ";
for (int j = i; j < (MAX_ITEMS - 1); j++)
{
items[j] = items[j + 1];
}
items[MAX_ITEMS-1] = " ";
}
}
totalItems--;
retVal = true;
}
return retVal;
}

C++ Maze Solving algorithm segmentation fault

The task is to create a randomly generated maze and then solve it, the issue is that whenever I recursively search the maze for the exit, i get a segmentation fault at runtime. Most of the cout is for debugging.
The Main File (where error persists)
#include <string>
#include <cmath>
#include <cstdlib>
#include "Main.h"
#include "Maze.h"
int main(int argc, char *argv[])
{
// Process the command line arguments and seed the random number
// generator, if necessary
unsigned int seed;
if(argc == 4)
{
seed = static_cast<unsigned int>(atoi(argv[1]));
cout << "Initializing pseudorandom number generator with seed "
<< seed << endl;
srand(seed);
}
else if(argc > 4)
{
cout << "Improper usage of Main\n";
exit(0);
}
cout << "Beginning execution\n";
// The first couple of numbers don't seem quite random,
// so "prime the pump" by calling rand() twice
(void) rand(); (void) rand();
mazeTest();
cout << "Finishing execution\n";
return 0;
}
void mazeTest()
{
int height = 0;
int width = 0;
cout << "Enter height (positive integer): ";
cin >> height;
cout << height << "\n";
cout << "Enter width (positive integer): ";
cin >> width;
cout << width << "\n";
MazeBuilder theMazeBuilder(height, width);
cout << "CREATED MAZE BUILDER\n";
Maze theMaze(theMazeBuilder);
cout << "CREATED THE MAZE\n";
cout << "The Maze:\n" << theMaze.toString();
solveMaze(theMaze);
}
void solveMaze(const Maze& theMaze)
{
cout << "ENTERED solveMaze()\n";
thePath = new int[theMaze.getHeight()*theMaze.getWidth()];
bool **wasHere;
for(int i = 0; i < theMaze.getHeight(); i++)
{
for(int j = 0; i < theMaze.getWidth(); j++)
{
wasHere[i][j] = false;
}
}
cout << "PATH INITIALIZED\n";
if(search(theMaze, theMaze.getEntranceRow(), 0, wasHere, thePath, thePathLength))
{
theMaze.toString(thePath, thePathLength);
}
}
bool search(const Maze& theMaze, int row, int col,
bool**& wasHere, int *aPath, int currentPathLength)
{
if(col == theMaze.getWidth()-1 && row == theMaze.getExitRow())
{
cout << "FOUND EXIT\n";
thePathLength = currentPathLength;
thePath = aPath;
return true;
}
if(wasHere[row][col] == true)
return false;
wasHere[row][col] = true;
if(row != 0 && theMaze.at(row,col).isWall(UP) == false)
{
if(search(theMaze, row-1, col, wasHere, aPath, currentPathLength))
{
aPath[currentPathLength] = UP;
currentPathLength++;
cout << "UP\n";
return true;
}
}
if(col != theMaze.getWidth()-1 && theMaze.at(row,col).isWall(RIGHT) == false)
{
if(search(theMaze, row, col+1, wasHere, aPath, currentPathLength))
{
aPath[currentPathLength] = RIGHT;
currentPathLength++;
cout << "RIGHT\n";
return true;
}
}
if(row != theMaze.getHeight()-1 && theMaze.at(row,col).isWall(DOWN) == false)
{
if(search(theMaze, row+1,col, wasHere, aPath, currentPathLength))
{
aPath[currentPathLength] = DOWN;
currentPathLength++;
cout << "DOWN\n";
return true;
}
}
if(col != 0 && theMaze.at(row,col).isWall(LEFT) == false)
{
if(search(theMaze, row, col-1, wasHere, aPath, currentPathLength))
{
aPath[currentPathLength] = LEFT;
currentPathLength++;
cout << "LEFT\n";
return true;
}
}
cout << "DEAD END\n----------------------------\n";
return false;
}
Important methods used...
Maze::at(int row, int col)
returns the cell at given row and column
Maze::toString() or toString(int* thePath, int thePathLength)
default: prints out just the maze using ASCII characters to cmd
with parameters: Prints out maze with solution using ASCII characters to cmd
Cell::isWall(direction)
returns whether or not there is a wall in that direction
(directions are constants declared and handled in Cell)
Other Info:
Maze is a dynamic 2d array of Cells
The maze is constructed properly (can output an unsolved maze properly)
The problem:
bool **wasHere;
for(int i = 0; i < theMaze.getHeight(); i++)
{
for(int j = 0; i < theMaze.getWidth(); j++)
{
wasHere[i][j] = false;
The reason:
wasHere is an uninitialized pointer to a pointer to a bool. You then dereference the first pointer causing undefined behavior (such as perhaps a crash...).