Game similar to Plinko

This game works very similar to plinko. Except every time the ball/disk falls it picks up points. (meaning points are picked up along the path not just at the end.) Anyway, the maze it goes through is a pyramid and for my assignment the the number of rows in the maze can be as high as INT_MAX. My question is the code crashes once I go over 4 rows. Side note, it actually still calculates the right answer with over 4 rows but it crashes. Any help?

Also another question but I dont know if this is the place for it. I crashed my code too many times trying to fix this code and now my Fault tolerant heap shim always keeps it from crashing but i have to know if it crashes. Anyone know how to disable it on windows 10?


#include <iostream>
#include <vector>



using namespace std;

class CIS14
{
public:
int getMaxPoints(vector<vector<int>> &maze)
{
if (maze.empty()){
return 0;
}
for (int i = maze.size(); i > 0; --i)
{
for (int j = 0; j < (i - 1); ++j)
{
int x = maze[i-1].size();
int y = maze[i-2].size();

if (&maze[i-2][j]==nullptr)
{
return 0;
}
if (y >= x)
{
return 0;
}
else if (x > y)
{
maze[i-2][j] = maze[i-2][j] + std::max(maze[i-1][j], maze[i-1][j+1]);
}

else
return 0;
}
}
return maze[0][0];

}

};

int main()
{
CIS14 cis14;
vector<vector<int>> maze1 = {{2}, {4,1}, {5,3,8}, {1,6,7,3}, {1,2,3,4,5}, {1,2,3,4,5,6}};
vector<vector<int>> maze2 = {{2}, {4,1}, {0,0,0}, {0,0,0,0}};
vector<vector<int>> maze3 = {};
vector<vector<int>> maze4 = {{}, {}, {}};
vector<vector<int>> maze5 = {{2}, {4,1,5}, {5,3}, {1,6,7,3}};
cout << cis14.getMaxPoints(maze1) << endl;
cout << cis14.getMaxPoints(maze2) << endl;
cout << cis14.getMaxPoints(maze3) << endl;
cout << cis14.getMaxPoints(maze4) << endl;
cout << cis14.getMaxPoints(maze5) << endl;
return 0;
}

Guys Im sorry this site wont let me post with a formatted code. It says error if I try.
Last edited on
If you're getting crashes while working with vectors/arrays, then chances are you are going to an out-of-bounds index.

If you use my_vector.at(index) instead of my_vector[index], C++ will do bounds checking for you, similar to languages like Java. This will allow you to locate exactly when the out-of-bounds indexing happens.

if (&maze[i-2][j]==nullptr)
What are you trying to do here...? I can almost guarantee this check is not doing what you think it's doing.

So for example, if you were to change your std::max line to
maze.at(i-2].at(j) = maze.at(i-2).at(j) + std::max(maze.at(i-1).at(j), maze.at(i-1).at(j+1)); along with every other place you have []'s, the program will at least crash at a specific instant it tries to call out-of-bound indices. You also can capture this exception by wrapping the whole code in a try-catch block. http://www.cplusplus.com/doc/tutorial/exceptions/

Your first exception seems to be happening in maze4's call to the function.

I know nothing about Fault Tolerant Heap or any problems associated with turning if off, but the directions from Microsoft are here: https://msdn.microsoft.com/en-us/library/windows/desktop/dd744764(v=vs.85).aspx (you have to use regedit to edit registry values).
Last edited on
Topic archived. No new replies allowed.