Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function won't return int value

Status
Not open for further replies.

mmalcolm

Programmer
Jul 2, 2000
4
US
Hey guys,

I would appreciate if you guys could help me with this code. The problem is with the "continueGame( )" function. It should return an int(0 or 1 ) and the while loop in the main runs or breaks depending on the return value. "continueGame" goes through a 8x8 grid ( which represents a game board for Othello) to see if all the spaces have been taken. Return 1 means to continue to play the game and Return 0 menas the game board is full and the game is over.

What's wrong with my code or logic? The summary of code follows. Let me know if you want the whole thing.

void main ()
{
...Some main code...

while ( continueGame( pGrid ) )
{
...Some code for the while loop...
}

...Some more main code...
}


//Function

int continueGame( char * pThisGrid[XY_MAX][XY_MAX] )
{
for (int j = 0; j < XY_MAX; j++)
{
for (int q = 0; q < XY_MAX; q++)
{
printf(&quot;\nCheck grid row:%d col:%d&quot;, j + 1, q + 1);
if ( *pThisGrid[j][q] == ' ') return 1;
}
}
return 0;
}

Thanks,
Mario
 
If the code is just the way you put it above, one problem should be the placing of the function - &quot;continueGame&quot; implemented after the main() function-... if that's the way, you should add the prototype for the function at the beginning of the program.

Does the program ever enter the function???
 
hey ,
i think there is a problem with the true=1 and false=0 of ansi C..
so while (true){.. statmts.... } now ur return of 1 is for finding ny single space.. don't u actually want to do the opposite??

-afaik,
shail
 
Hey guys, I'll try to cover all of you questions regarding my question:

Q: Does the program ever enter the function???
A: Yes it does. In the function &quot;continueGame&quot; there is a printf that prints out the current row and column being checked. I see these printf statements in the output.

Q: Now ur return of 1 is for finding any single space.. don't u actually want to do the opposite??
A: No I think this part of the logic is correct. Allow me to elaborate. The while loop in main will continue to its thing so long as there is and empty position on the board. I have the '@' character to represent the black piece. I have a 'O' character to represent the white piece. And I have the ' ' (space) character to represent a vacant space on the board so that when I print the board to the screen, the space will occupy a character space and the board will come out nice and even.
So the for the continue game function is:
Go through the whole board. If you find a space character, end the function and immediately return 1. The return 1 will allow you to run the while loop (which has the game functions for continuing). If you make it through the whole board without finding the space character (that means all the board spaces are occupied by a 'O' or a '@')then return 0, which ends the while loop in main.

Q: Here's more information on what's going wrong.
A: As I said before, the function is being entered because of the printf(&quot;\nCheck... statement. However, I failed to mention that at the last iteration of the while (that is when the if ( *pThisGrid[j][q] == ' ') return 1; statement is false for all board spaces, it should leave both for loops and return 0, so that it can exit the while loop in main.
I've tested this using a 2x2 (which has four spaces) board and what happens is that allows you to enter a piece for the first 3 entries but after the fourth, it crashes either trying to return the 0 in the function or trying to exit the while loop in main.


I'm not sure what the problem is
Thanks again guys,
 
Maybe,
while ( continueGame( pGrid ) == 1 )
{
...Some code for the while loop...
}
You can test continueGame's return vulue like this:

int ret;
while ((ret = continueGame(pGrid)) != 0) {
....
}

 
How did you define pGrid ?
if this way
char pGrid[XY_MAX][XY_MAX];
and use continueGame(gGrid);
why have you define function argument as
char *pGrid[XY_MAX][XY_MAX]
* is not in good place here :)
Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top