hey im having some problems with an overloaded operator:
prints infinte stars!!!Code:/********************************/ /* MineSweeper 0.0.1 */ /********************************/ #include <iostream.h> #include "apmatrix.h" #include "randgen.h" #include "randgen.cpp" class minesweeper { public: apmatrix<int> create(int x, int y); //Creates a matrix with the x and y grid size void randfill(apmatrix<int> &v); //Fills the matrix with random integers bool checkifbomb(apmatrix<int> &p, int x, int y); // Checks if position (x,y) in apmatrix p is a bomb (0 is bomb) void gameloop(apmatrix<int> &p, int originalx, int originaly); //the loop that the game will go through }; void operator << (ostream &os, apmatrix<int> &v); //Free function: displays the minesweeper matrix int main() { minesweeper main; apmatrix<int> p; int x, y; cout << "****Welcome to MineSweeper 0.0.1***" << endl; cout << "Please enter the size of the grid (x y): "; cin >> x >> y; p = main.create(x,y); main.randfill(p); cout << p; main.gameloop(p, x, y); return 0; } apmatrix<int> minesweeper::create(int x, int y) { apmatrix<int> p(x+1, y+1); return p; } void minesweeper::randfill(apmatrix<int> &v) { RandGen r; int cols = v.numcols(); int rows = v.numrows(); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { v[i][j] = r.RandInt(0, 1); } } } bool minesweeper::checkifbomb(apmatrix<int> &p, int x, int y) { if (p[x][y] == 0) { return false; } else return true; } void minesweeper::gameloop(apmatrix<int> &p, int originalx, int originaly) { int x, y; bool gameover = false; while (gameover == false) { cout << "Enter the coordinates to check (x y): "; cin >> x >> y; while ((x >originalx) || (y > originaly)) { cout << "Coordinates larger than the grid! Enter new ones: "; cin >> x >>y; } if (checkifbomb(p,x,y) == true) { cout << p; cout <<"You have stepped on a bomb! Game Over!" << endl; gameover = true; } else { cout << p; cout << "Safe!" << endl; } } } void operator << (ostream &os, apmatrix<int> &v) { int rows = v.numrows()-1; int cols = v.numcols()-1; for (int i = 0; i < rows; i++) { for (int j = 0; i < cols; j++) { os << " * "; } } }



LinkBack URL
About LinkBacks


