# Problems in a game program

• 06-06-2006
ChronoSquare
Problems in a game program
Well, it isn't really good enough to be considered an actual game - it's just a battleship and rocketship program I made for a teacher last December (in Pascal) to enter guesses for the location and display the distance from the guess to the actual location. For extra credit that I desperately need in my Visual C++ class (stupid teacher >:/), I began to reprogram this in C++.

After finding out that I couldn't do graphics like I had wanted to, I just went to using output and ASCII art to make something display. Except it won't display, as it doesn't make it through the compiler.

I've got three problems with it: The arrays used for the coordinates, this funky loop, and it seems to have a beef with my coordinate-generating function for the rocketship. (3 variables.)

Ok, first, the arrays.

I had written it to use arrays as the coordinate values, to simplify on the number of variables. So I have four arrays: Two boolean two-dimensional arrays, for the battleship's actual coordinates and the guess for the battleship's coordinates, then two boolean three-dimensional arrays for the rocket's coordinates and the guess for the rocket coordinates. But no matter how I try I can't get them to cooperate with the compiler... their use with the functions is what's going wonky. Here's the relevant array code:

Code:

```//Define the boolean values const int True = 1; const int False = 0; //Define the arrays for the grid const int GRID_SIZE = 10; typedef bool list_BSCoords[GRID_SIZE] [GRID_SIZE]; list_BSCoords BSCoords; typedef bool list_BSGuessCoords[GRID_SIZE] [GRID_SIZE]; list_BSGuessCoords BSGuessCoords; typedef bool list_RSCoords[GRID_SIZE] [GRID_SIZE] [GRID_SIZE]; list_RSCoords RSCoords; typedef bool list_RSGuessCoords[GRID_SIZE] [GRID_SIZE] [GRID_SIZE]; list_RSGuessCoords RSGuessCoords; void BSrng(bool BScoords[GRID_SIZE] [GRID_SIZE], int &XCoord, int &YCoord); void RSrng(bool RScoords[GRID_SIZE] [GRID_SIZE] [GRID_SIZE], int &XCoord, int &YCoord, int&ZCoord);```
This is where it errors, the calling of the function, where it either gives a syntax error if X isn't there, and a can't convert 'bool' to 'bool'(*)[10] error if there is something there:
Code:

```bool X; //Dummy variable to call the function   X = 0;   BSrng(BSCoords[X] [X], XCoord, YCoord); //randomize the battleship coordinates```
Code:

```BSrng(bool BSCoords[GRID_SIZE] [GRID_SIZE], int &XCoord, int &YCoord); {        //Seed the random  numbers         time_t seconds;         time(&seconds);         srand((unsigned int) seconds);         //Assign random values for the coordinates         XCoord = rand() % (High - Low + 1) + Low;         YCoord = rand() % (High - Low + 1) + Low;         //"Input" corresponding coordinates into the array         BSCoords[XCoord] [YCoord] = True; }```
And then, it's giving me all kinds of wierd errors within this do while loop in the battleship's game function:

Code:

```do   {        cout << "X Coordinate?" << endl;           cin >> XGuess; //read in an X-coordinate guess       cout << "Y Coordinate?" << endl;       cin >> YGuess; //read in a Y-coordinate guess                 BSGuessCoords[XGuess] [YGuess] = True; //Assign corresponding array to hit       If ((XGuess == 0) && (YGuess == 0))               quit = True;       Else If (BSGuessCoords[XGuess] [YGuess] && BSCoords[XGuess][YGuess])       {        cout << "You sunk my battleship!" << endl;         cout << "It took you " << counter << " tries to hit my battleship." << endl;       }       Else       {  ++counter;               distance = sqrt((XCoord - XGuess)^2 + (YCoord - YGuess)^2)         cout << "You missed me by " << setiosflags(ios::showpoint|ios::fixed)                 << setprecision(2) << distance << endl;       }   } while (!exit);```
The errors are as follows:
Call to undefined function 'If' (statement line with if requires ;)
Undefined symbol 'Else' (statment line with else requires ;)
do statement must have while.

And finally, the rocket function coordinate generator:

Code:

```RSrng(bool RSCoords[GRID_SIZE] [GRID_SIZE] [GRID_SIZE], int &XCoord, int &YCoord, int &ZCoord); {        //Seed the random  numbers         time_t seconds;         time(&seconds);         srand((unsigned int) seconds);         //Assign random values for the coordinates         XCoord = rand() % (High - Low + 1) + Low;         YCoord = rand() % (High - Low + 1) + Low;         ZCoord = rand() % (High - Low + 1) + Low;         //"Input" corresponding coordinates into the array         RSCoords[XCoord] [YCoord] [ZCoord] = True; }```
Expression syntax on the function's header line
Undefined symbol 'ZCoord' (which I had declared exactly as all the others in the rocket function) and
Compound statement missing } at the end of the function.

Please, someone help me out here... I hope so very much that it isn't anything serious, and that I can get it done before Thursday... (Teacher doesn't have the slightest clue why it's not working)
• 06-06-2006
Richie T
>>(Teacher doesn't have the slightest clue why it's not working)

The following has often been said, but I think that this is the most
appropriate case for this I have ever encountered - "Get a new
teacher." There are some pretty fundamental errors in your code,
and if your teacher didn't spot anything, then he/she either never
looked at it, or doesn't know C++ in the first place, either way
they are not in a position to teach IMO.

First things first - C++ is a case sensitive language, therefore
"If" is not the same as "if". that explains these two errors:

>>Call to undefined function 'If'
>>Undefined symbol 'Else'

these should be "if" and "else".

Next up, we have the first thing I spotted:

Code:

```//Define the boolean values const int True = 1; const int False = 0;```
no need to do this - as already mentioned, C++ is case sensitive,
and the slightly different words "true" and "false" are actually
part of the language (with the appropriate values as well).

As for the other error regarding the do.. while, well that seems to
be in order, perhaps the error is elsewhere in the code, or is
being produced by the other errors.

Next we have this function:

Code:

```BSrng(bool BSCoords[GRID_SIZE] [GRID_SIZE], int &XCoord, int &YCoord); // should have no semicolon here {        //Seed the random  numbers         time_t seconds;                 .                 .                 . }```
You have the same mistake in the RSrng function.

Next we have seeding the random number generator - you
should only do that once in your program, as opposed to seeding
every time you call a specific function - just include the following

Code:

`srand (time (0));`
[EDIT] - See my following post for correction to this!!!
Now back to that function - this parameter is illegal:
"bool BSCoords[GRID_SIZE] [GRID_SIZE],"

There should be nothing in between the [] when declaring an
array parameter. Accordingly, calling the function in this manner:

BSrng(BSCoords[X] [X], XCoord, YCoord);

the BSCoords[X] [X] is actually passing the value stored at the
location [X] [X] in the array, not the array itself.

Lastly, I'm not exactly sure as to what you are trying to do here:

Code:

```typedef bool list_BSCoords[GRID_SIZE] [GRID_SIZE]; list_BSCoords BSCoords; typedef bool list_BSGuessCoords[GRID_SIZE] [GRID_SIZE]; list_BSGuessCoords BSGuessCoords; typedef bool list_RSCoords[GRID_SIZE] [GRID_SIZE] [GRID_SIZE]; list_RSCoords RSCoords; typedef bool list_RSGuessCoords[GRID_SIZE] [GRID_SIZE] [GRID_SIZE]; list_RSGuessCoords RSGuessCoords;```
I think you should avoid using typedef in this manner, I'm not
even sure that it's legal, but I'm guessing that it isn't. I think
you should just scrap any typedefs and declare your arrays in
a more typical fashion

>>I hope so very much that it isn't anything serious, and that I
can get it done before Thursday

My guess is you are not that familiar with C++, and you have most
likely made more mistakes than these which may be revealed as
you fix these errors, but with some help you will probably be able
to get this running for Thursday.
• 06-07-2006
ChronoSquare
Ok yes, thank you for pointing out those errors. Yeah, I'd love to get a new C++ teacher (we all would), but sadly that decision is out of our hands, as he's the only one in the school.

I know a little C++... it's a semester class. But that kind of capitalization thing is typical of me - I mess up all the time like that in math. Actually, I believe I ended up doing that was because we'd not had much to program in these past few weeks, aside from that last one that gave me trouble pausing it at the end... I kinda forgot about the non-capitalization; I think I was thinking of VB syntax there - capitalize stuff.

So now it's just the arrays that need work.

Where is the proper place to declare the arrays? Because in the function declaration, without the GRID_SIZE in the brackets, these are the errors I get:
Size of the type 'bool[]' is unknown or zero or Size of the type 'bool[][]' is unknown or zero (in function declaration and anywhere in the functions)
Expression syntax (in calling the functions)
Type mismatch in redeclaration of 'BSrng(bool (*)[], int &, int &)'
Type mismatch in redeclaration of 'RSrng(bool (*)[10][]. int &, int &, int &'

According to the book we use, with two-dimensional arrays, the first dimension should be defined in the function declaration. Extending that idea out to three dimensions, I'd define the first two dimensions. Except when I do that, it still returns errors of the type 'bool[]' unknown or zero - just less of them than otherwise.
• 06-07-2006
Richie T
I was wrong about how to pass the arrays, but I've done a little
research and I've come up with this quick example:

Code:

```#include <iostream> #include <ctime> #define ROWS 10 #define COLS 5 using namespace std; void Print2d (int arr[][COLS], const int x, const int y); int main (void) {         int my2d [ROWS][COLS];         srand (time (0));         for (int i = 0; i < ROWS; i++)         {                 for (int j = 0; j < COLS; j++)                 {                         my2d [i][j] = rand () % 10; //just for something to look at                 }         }         Print2d (my2d, ROWS, COLS);         return 0; } void Print2d (int arr[][COLS], const int rows, const int cols) {         for (int i = 0; i < rows; i++)         {                 for (int j = 0; j < cols; j++)                 {                         cout << arr [i][j] << " ";                 }                 cout << endl;         } }```
The same logic should apply to larger dimensions of arrays. Hope
this clarifies the matter, it does for me anyway - I've editted my
previous post to highlight this point.

As for your teacher, poor teachers are a fact of life unfortunately,
but in cases like this you are better ignoring much of what your
teacher tells you until you can confirm the information from a
more reliable source!
• 06-08-2006
ChronoSquare
NOTE: I wonder, now that the code errors are fixed, that this should be moved to the games board? :confused:

Yay! Thank you so much for your help, I've got it working now! :D

Well, ALMOST working. In a run through the program, once the coordinates are guessed, then in any subsequent runs through the SAME coordinates are there. So say I enter 5,5 and it says I'm 3 off. I enter 4, 6 and I get it. I play again, enter 4, 6 and hit it. Testing this further, I would enter 5, 5 and get 3.16 away and 4, 6 still works. Enter 5, 5 again and I'd get 4.64, enter 4, 6 and it still counts it as a hit.

I think it's something to do with the value of the boolean array - originally, I notice now I had no reset to the array, and now it accepts the same values for it.

I have it set up where both the guess array and the coordinate array are reset if the user quits or if the user gets it right, but that still doesn't work...

EDIT: Here's the code where it evaluates if the guess coordinates and the actual coordinates are both the same, and thus a hit.

Code:

```do   {  do           {        cout << "X Coordinate?" << endl;                   cin >> XGuess; //read in an X-coordinate guess       } while ((XGuess < 0) || (XGuess > 10)); //protect from invalid entries       do       {        cout << "Y Coordinate?" << endl;               cin >> YGuess; //read in a Y-coordinate guess       } while ((YGuess < 0) || (XGuess > 10)); //protect from invalid entries                 BSGuessCoords[XGuess][YGuess] = true; //Assign corresponding array to hit       ++counter; //there's been another guess at the coordinates       if ((XGuess == 0) && (YGuess == 0))       {        quit = true;               cout << "My battleship was located at: (" << XCoord << ", " << YCoord                 << ")." << endl;                   //Reset old coordinates and guess                   BSCoords[XCoord][YCoord] = false;         BSGuessCoords[XGuess][YGuess] = false;       }       else if (BSGuessCoords[XGuess][YGuess] && BSCoords[XGuess][YGuess])       {        cout << "You sunk my battleship!" << endl;                 cout << "It took you " << counter << " tries to hit my battleship." << endl;                 quit = true;                   //Reset old coordinates and guess                   BSCoords[XCoord][YCoord] = false;         BSGuessCoords[XGuess][YGuess] = false;       }       else       {  distance = (XCoord - XGuess)*(XCoord - XGuess)+(YCoord - YGuess)*(YCoord - YGuess);               distance = sqrt(distance);         cout << "You missed me by " << setiosflags(ios::showpoint|ios::fixed)                 << setprecision(2) << distance << endl;         //Reset the guess's validation         BSGuessCoords[XGuess][YGuess] = false;       }   } while (!quit);```
The code is basically the same for the Rocket function, with the array names changed and the size of the array expanded to 3 dimensions.