Reputation: 112
So I have a function which controls the turn taking of a player, and I want it to return FAILURE when the player unsuccessfully takes a turn. (The game is connect 4). I obviously want it to return success when the turn is valid, however.... Even when I return SUCCESS it's requiring two return SUCCESS to return a single success. Here is the code:
enum input_result take_turn(struct player * current,
enum cell_contents board[][BOARDWIDTH])
{
/***************** Logic for Human player ******************/
if(current->type == HUMAN)
{
printf("human");
return SUCCESS;
}
/**************** Logic for Computer player ****************/
else if(current->type == COMPUTER)
{
printf("computer");
return SUCCESS;
}
return SUCCESS;
}
And it's called by this:
struct player * play_game(struct player * human ,
struct player* computer)
{
while(counter < 30)
{
take_turn(current, board);
/* Only swap if take turn was success */
if(take_turn(current, board) == SUCCESS)
{
swap_players(¤t, &other);
display_board(board);
}
counter++;
}
return NULL;
}
The only thing I can think that may be ruining this is my:
enum input_result
{
/**
* the input was valid
**/
SUCCESS,
/**
* the input was not valld
**/
FAILURE,
/**
* the user requested to return to the menu either by pressing enter on
* an empty line or pressing ctrl-d on an empty line
**/
RTM=-1
};
I'm unsure why take_turn(current, board) is being called twice when the only possible outcome is for the if statement to be entered as every possible return value is SUCCESS. Does anyone have any idea why this may happen? My output is printing:
HumanHuman
ComputerComputer
HumanHuman
ComputerComputer
and so on... showing that it's going through it twice before a success is registered.
Upvotes: 0
Views: 71
Reputation: 3360
Did you notice that you are calling take_turn twice in your loop? Once without looking for the return value, and another in your if statement. get rid of the first one.
Upvotes: 1
Reputation: 39837
Your problem is here:
while(counter < 30)
{
take_turn(current, board);
/* Only swap if take turn was success */
if(take_turn(current, board) == SUCCESS)
You're calling take_turn()
twice.
Upvotes: 1