Archived

This topic is now archived and is closed to further replies.

hawflakes

Seems like a problem with scope....

Recommended Posts

Hi, I''m working on a Battleship game and I just finished creating a working ship and grid class and was working on a driver to test them out. I hit a snag where the user is placing their ships before the game actually starts, they successfully place the ship in their grid but it seems like the grid goes out of scope and is cleared/reset every time, leaving it blank. Here is the driver code:

bool PlayerAddShipToGrid(CShip Ship, CGrid Grid);

main ()
{
        //create the grid, the only parameter is its title
	CGrid OceanGrid ("Ocean: ");

	//heres 2 sample ships and the code that places them
	CShip Carrier (5, "Carrier", ''C'');
	while ( PlayerAddShipToGrid(Carrier, OceanGrid) == false)
		PlayerAddShipToGrid(Carrier, OceanGrid);

	
	CShip Battleship (4, "Battleship", ''B'');
	while ( PlayerAddShipToGrid(Battleship, OceanGrid) == false)
	PlayerAddShipToGrid(Battleship, OceanGrid);
     
        return 0;
}



bool PlayerAddShipToGrid(CShip Ship, CGrid Grid)
{
	char Direction, AnyKey;
	int  XCoord, YCoord;

	system("cls");
	Grid.DisplayGrid(30,0); 
	cout << endl;
         
        //buncha cout/cin stuff asking them where to position it

	//adjust for border offset
	XCoord++;
	YCoord++;

	if ( Grid.AddShipToGrid( Direction, Ship.GetShipSize(), Ship.GetShipLetter(), XCoord, YCoord) != false)
	{
                //this is to tell the ship where it is
		Ship.PlaceShip (Direction, XCoord, YCoord);
                //this is to make the grid display the ship
		Grid.AddShipToGrid( Direction, Ship.GetShipSize(), Ship.GetShipLetter(), XCoord, YCoord);
		Grid.DisplayGrid(30,0);
		return true;
	}
	else
	{
		cout << "The ship does not fit that way.  Enter a key to continue: ";
		cin >> AnyKey;
		return false;
	}
}
 
The Grid class:

class CGrid
{
private:
	string mGridTitle;
	enum { GridSize = 11 };
        //HERE IS THE PART THAT IS GETTING CHANGED,
        //AND THEN RESET EACH TIME (BELOW)
	char mGridMatrix[GridSize][GridSize];

public:
	CGrid(string Title);
	void ClearGrid();
	void DisplayGrid(int XPosition, int YPosition);
	bool AddShipToGrid( char Orientation, int SizeOfShip, char ShipLetter, int XCoord, int YCoord );
	void AddCharToGrid (int XCoord, int YCoord, char Letter);
	
};
 
There is a lot more code for the grid class, I will be happy to post it if necessarry but I''m guessing this might be enough info for someone to explain whats wrong. Thanks for reading all this.

Share this post


Link to post
Share on other sites
when you call PlayerAddShipToGrid, you are passing the parameters by value, not by reference, so the runtime creates a copy of them and passes them into your function. Your function then modifies the copy and leaves the original variables declared in your main function untouched. You need to change any of your functions that change the parameters passed in, to pass them by reference, like so


bool PlayerAddShipToGrid(CShip& Ship, CGrid& Grid) (shouldn''t really require any changes to your code other than the function declarations)

or

bool PlayerAddShipToGrid(CShip* pShip, CGrid* pGrid) (and of course change the code to work with pointers)

HTH

Share this post


Link to post
Share on other sites
Thank you very much! I did know about passing variables by reference, but I guess the whole class thing threw me off. Alright, back to work :-)

Share this post


Link to post
Share on other sites
There is a new obstacle I have run into. I was using time(NULL) to seed the random number generator. However, this is a tick that doesnt change fast enough so that every number generated (with the modulus applied afterwards) comes out the same for a few seconds. This is making every computer ship face the same way! Is there any way to have a random number from 1-4 change more erratically so that there is more "randomness"? thanks

Share this post


Link to post
Share on other sites
You don''t need to re-seed the random number generator for every single number. If you keep calling rand(), it will keep spitting out different numbers. Just seed it to time(null) once at the start of the program.

Share this post


Link to post
Share on other sites
quote:
Original post by andy_fish
You don't need to re-seed the random number generator for every single number. If you keep calling rand(), it will keep spitting out different numbers. Just seed it to time(null) once at the start of the program.


I did seed it only once. And it does spit out random numbers, just not fast enough. What is happening is that the computer is constantly trying random numbers in a certain range until the ships are all aligned on the grid. It happens so fast that, with the modulus applied, the random number changes only every 1-2 seconds. This means that each ship is going to have the same heading value. They could all be facing up, left etc. but never different directions

[edited by - hawflakes on July 6, 2003 9:35:16 PM]

Share this post


Link to post
Share on other sites