• 02-09-2003
lithium
hey im having some problems with an overloaded operator:

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 << " * ";                 }         } }```
prints infinte stars!!!
• 02-09-2003
Polymorphic OOP
Quote:

Originally posted by lithium
hey im having some problems with an overloaded operator:

Code:

```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 << " * ";                 }         } }```

for (int j = 0; i < cols; j++)

should be

for (int j = 0; j < cols; j++)
• 02-09-2003
Polymorphic OOP
also, are you sure you want to subtract 1 from the number of rows and number of columns? you probably don't...
• 02-09-2003
lithium
Re: Re: overloaded << AP classes
#### too much time in front of computer didnt see that, anyways its not infinte now but its not formatted correctly either....10 10 would make three rows

thx
• 02-09-2003
Polymorphic OOP
in the comparison you accidently used an i instead of a j

Also, as is, you can get the same result be just using a SINGLE loop printing out numberrows * numbercols asterisks, though I'm guessing you want a new line for every row, in which case you forgot to put that in.
• 02-09-2003
lithium
oh ok, this will work for 10 10:

Code:

```void operator << (ostream &os, apmatrix<int> &v) {         int rows = v.numrows() - 1;         int cols = v.numcols();         for (int i = 0; i < rows; i++)         {                 for (int j = 0; j < cols; j++)                 {                         if (j == 10)                         {                                 os << endl;                         }                         else                         {                                 os << " * ";                         }                 }         } }```
but i dont think apmatrix have something that i can replace the if j == 10 with so i can detect if its at the end of the row.
• 02-09-2003
Polymorphic OOP
You're making it way more complex than it should be, and you're missing my point.

Code:

```int rows = v.numrows(); int cols = v.numcols(); for (int i = 0; i < rows; i++) // Prints all the rows {     for (int j = 0; j < cols; j++) // Prints all the columns     {         os << " * ";     }     os << endl; // new line and flush after every row }```
also, don't return void, return a reference to os
• 02-09-2003
lithium
hey thanx man it works now
sorry i didnt get what youwere saying, im not that advanced...

now all i need is som way to display all stars except where a non bomb coordinate was guessed..me thinks this will be hard. clues?
• 02-10-2003
Polymorphic OOP
Code:

```int rows = v.numrows(); int cols = v.numcols(); for (int i = 0; i < rows; i++) {     for (int j = 0; j < cols; j++)     {         os << ( v[i][j] ?  "  " : " * " );     }     os << endl; }```
• 02-10-2003
lithium
no thats not what i meant...perhaps an example:

first shows
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

then lets say that 0 0 is not a bomb, the user would enter 0 0 and would see:
S * * * *
* * * * *
* * * * *
* * * * *
* * * * *

if he then entered 5 5 and that was also not a bomb:
S * * * *
* * * * *
* * * * *
* * * * *
* * * * S

see where i'm going with this. like in regular minesweeper, the ones he clicked which are safe are then marked. thats why i think itll be hard because the << would show stars and im not sure how to get it to make exceptions to showing stars which in this case is safe coordinates(S). this would prolly be really different.
• 02-10-2003
ripper079
Shouldn´t the operator << (overloaded in apmatrix) return a reference to os like

Code:

```ostream& 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 << " * ";                 }         }                 return os; }```
???
• 02-10-2003
lithium
well i got the same result with a void, but ok i changed it.
• 02-11-2003
Polymorphic OOP
The reason you return a reference to an ostream is so that you can string it together like

cout << Blah << MoreBlah << "asdfgad";