Thread: overloaded << AP classes

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    88

    overloaded << AP classes

    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!!!

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: overloaded << AP classes

    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++)

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    also, are you sure you want to subtract 1 from the number of rows and number of columns? you probably don't...

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    88

    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
    Last edited by lithium; 02-09-2003 at 09:06 PM.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    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.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    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?

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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;
    }
    Last edited by Polymorphic OOP; 02-10-2003 at 12:11 AM.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    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.

  11. #11
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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;
    }
    ???

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    88
    well i got the same result with a void, but ok i changed it.

  13. #13
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The reason you return a reference to an ostream is so that you can string it together like

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Friend func. and overloaded >> and <<
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2005, 01:14 AM
  3. Problem using overloaded << operator
    By Zildjian in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2004, 01:09 PM
  4. overloaded << operator doesnt work
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2002, 04:20 AM
  5. AP Classes, won't work! Anyone familiar?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2002, 12:52 AM