Mysterious bug kills my program
Today, I decided to try and recreate minesweeper in a win32 console application. I spent 2 hours coding this, and killing compiler errors in the code. Finally, I went to run it, but I got the error message in a popup dialogue box "Debug error. Abnormal termination".
I am using Visual C++ 6.0.
''''I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it.'''' -Jack Handy
Here is a portion of the code (the complete listing causes a server error, or something)
''I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it.'' -Jack Handy
[edited by - g_hempstock on March 6, 2003 5:21:58 PM]
#include <vector>#include <iostream>#include <stdlib.h>#include <time.h>using namespace std;const int MINE = -1;const char FLAG = 'F';const char UNCLICKED_SPACE = '-';bool gameRunning = true;//assumes current square isn't a mineint getNumNeighbours(vector &v, int x, int y){ int numMines = 0; if (x == 0) { if (y == 0) { if ( v.at((x + 1) + y * 5) == MINE ) numMines++; if ( v.at((x + 1) + (y + 1) * 5) == MINE ) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } else if (y == 4) { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x + 1) + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x + 1) + y * 5) == MINE ) numMines++; } else { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x + 1) + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x + 1) + y * 5) == MINE) numMines++; if ( v.at((x + 1) + (y + 1) * 5) == MINE) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } } else if (x == 4) { if (y == 0) { if ( v.at((x - 1) + y * 5) == MINE ) numMines++; if ( v.at((x - 1) + (y + 1) * 5) == MINE ) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } else if (y == 4) { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x - 1) + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x - 1) + y * 5) == MINE ) numMines++; } else { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x - 1) + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x - 1) + y * 5) == MINE) numMines++; if ( v.at((x - 1) + (y + 1) * 5) == MINE) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } } else { if ( v.at((x - 1) + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x - 1) + y * 5) == MINE) numMines++; if ( v.at((x - 1) + (y + 1) * 5) == MINE) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x + 1) + (y - 1) * 5) == MINE ) numMines++; if ( v.at((x + 1) + y * 5) == MINE) numMines++; if ( v.at((x + 1) + (y + 1) * 5) == MINE) numMines++; } return (numMines);}void endGame(vector &charField, vector &intField){ for (int i = 0; i < 25; i++) { if (intField.at(i) == MINE) charField.at(i) = '*'; } gameRunning = false; drawField(charField);}//handles rolling back when an unnumbered square is "clicked"void rollBack(vector &charField, vector &intField, int x, int y){ if (intField.at(x + y * 5) == MINE) endGame(charField, intField); else if(intField.at(x + y * 5) != 0) charField.at(x + y * 5) = '0' + intField.at(x + y * 5); else { charField.at(x + y * 5) = ' '; if (x == 0) { if (y == 0) { rollBack(charField, intField, x + 1, y); rollBack(charField, intField, x + 1, y + 1); rollBack(charField, intField, x, y + 1); } else if (y == 4) { rollBack(charField, intField, x, y - 1); rollBack(charField, intField, x + 1, y - 1); rollBack(charField, intField, x + 1, y); } else { rollBack(charField, intField, x, y - 1); rollBack(charField, intField, x + 1, y - 1); rollBack(charField, intField, x + 1, y); rollBack(charField, intField, x + 1, y + 1); rollBack(charField, intField, x, y + 1); } } else if (x == 4) { if (y == 0) { rollBack(charField, intField, x - 1, y); rollBack(charField, intField, x - 1, y + 1); rollBack(charField, intField, x, y + 1); } else if (y == 4) { rollBack(charField, intField, x, y - 1); rollBack(charField, intField, x - 1, y - 1); rollBack(charField, intField, x - 1, y); } else { rollBack(charField, intField, x, y - 1); rollBack(charField, intField, x - 1, y - 1); rollBack(charField, intField, x - 1, y); rollBack(charField, intField, x - 1, y + 1); rollBack(charField, intField, x, y + 1); } } else { rollBack(charField, intField, x - 1, y - 1); rollBack(charField, intField, x, y - 1); rollBack(charField, intField, x + 1, y - 1); rollBack(charField, intField, x + 1, y); rollBack(charField, intField, x + 1, y + 1); rollBack(charField, intField, x, y + 1); rollBack(charField, intField, x - 1, y + 1); rollBack(charField, intField, x - 1, y); } }}int main(){ srand ( time(NULL) ); //initialize random number generator vector buffer(24, 0); vector board(24, UNCLICKED_SPACE); setBoard(buffer); while (gameRunning) { clearScreen(); drawField(board); int x, y; char command; do { cout << "Enter F x y to flag or unflag a square\n"; cout << "Enter C x y to \"Click\" on a square" << endl; cin >> command >> x >> y; } while (command != 'c' || command != 'C' || command != 'f' || command != 'F' || x > 4 || x < 0 || y > 4 || y < 0); if (command == 'F' || command == 'f') { setFlag(board, x, y); } else { click(board, buffer, x, y); } } return (0);}
''I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it.'' -Jack Handy
[edited by - g_hempstock on March 6, 2003 5:21:58 PM]
run it through your debugger and see the line on which it crashes. if you don''t want to use your debugger you can go the more hassling route of putting in MessageBox(NULL, "here", "error", MB_OK); calls throughout your startup code so you can see how far you get. either way you can easily isolate it to a single line of code.
-me
-me
I did enter a couple of break points, but i got the error message before I even hit the first of the breakpoints. I even tried putting a break point on the first line of the main function, with the same result. If there is some other way to utilize the debugger that would work better, could someone please explain how to use it.
I think what you need is a JIT (Just In Time) Debugger these give you a general idea of what the problem is and where it’s coming from.
Try DrMingw and see if that helps if not you might want to do a google search and try another.
Remember to build your application with debugging information included, you normally have to do this for a JIT debugger to tell you anything useful.
Try DrMingw and see if that helps if not you might want to do a google search and try another.
Remember to build your application with debugging information included, you normally have to do this for a JIT debugger to tell you anything useful.
Okay, I''ve narrowed my problem down to the following function. I don''t see what''s wrong, however...
int getNumNeighbours(vector<int> &v, int x, int y){ int numMines = 0; if (x == 0) { if (y == 0) { if ( v.at(x + 1 + y * 5) == MINE ) numMines++; if ( v.at(x + 1 + (y + 1) * 5) == MINE ) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } else if (y == 4) { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x + 1 + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x + 1 + y * 5) == MINE ) numMines++; } else { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x + 1 + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x + 1 + y * 5) == MINE) numMines++; if ( v.at(x + 1 + (y + 1) * 5) == MINE) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } } else if (x == 4) { if (y == 0) { if ( v.at(x - 1 + y * 5) == MINE ) numMines++; if ( v.at(x - 1 + (y + 1) * 5) == MINE ) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } else if (y == 4) { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x - 1 + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x - 1 + y * 5) == MINE ) numMines++; } else { if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x - 1 + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x - 1 + y * 5) == MINE) numMines++; if ( v.at(x - 1 + (y + 1) * 5) == MINE) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; } } else { if ( v.at(x - 1 + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x - 1 + y * 5) == MINE) numMines++; if ( v.at(x - 1 + (y + 1) * 5) == MINE) numMines++; if ( v.at(x + (y + 1) * 5) == MINE ) numMines++; if ( v.at(x + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x + 1 + (y - 1) * 5) == MINE ) numMines++; if ( v.at(x + 1 + y * 5) == MINE) numMines++; if ( v.at(x + 1 + (y + 1) * 5) == MINE) numMines++; } return (numMines);}
Okay, I''ve finally figured out my problem. It was that I forgot to do a couple of coordinate checks, and was, as a result, trying to check elements of a vector that didn''t exist. Thanks to everyone for the help
''''I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it.'''' -Jack Handy
''''I can imagine a world without hate, a world without fear, a world without war. And I can imagine us attacking that world, because they would never expect it.'''' -Jack Handy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement