所以我有一个控制玩家转身的功能,当玩家转弯失败时我希望它返回FAILURE。 (游戏连接4)。我显然希望它在转弯有效时返回成功,但是......即使我返回SUCCESS,它也需要两次返回SUCCESS才能返回一次成功。这是代码:
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;
}
它被称为:
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;
}
我能想到的唯一可能是破坏这一点的是我的:
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
};
我不确定为什么take_turn(current,board)被调用两次,唯一可能的结果是输入if语句,因为每个可能的返回值都是SUCCESS。有谁知道为什么会这样? 我的输出是打印:
HumanHuman
ComputerComputer
HumanHuman
ComputerComputer
依此类推......表明在注册成功之前它会经历两次。
答案 0 :(得分:1)
你的问题在这里:
while(counter < 30)
{
take_turn(current, board);
/* Only swap if take turn was success */
if(take_turn(current, board) == SUCCESS)
您已经两次致电take_turn()
。
答案 1 :(得分:1)
您是否注意到您在循环中两次调用take_turn?一次没有查找返回值,而另一个在if语句中。摆脱第一个。