【发布时间】:2018-10-02 04:00:34
【问题描述】:
我的功能:
int checkSE(disk board[][SIZE], disk hypotheticalDisk)
{
int i;
int j;
int row;
int col;
int player;
int opponent;
int checkSEflag;
player = hypotheticalDisk.type;
(player == 0) ? (opponent = 1) : (opponent = 0);
row = hypotheticalDisk.pos.row;
col = hypotheticalDisk.pos.col;
checkSEflag = 0;
for (i = row + 2, j = col + 2; ((i < SIZE) && (j < SIZE) && (checkSEflag == 0)); i++, j++)
{
if (board[i][j].type == player)
{
for (--i, --j; board[i][j].type == opponent; i--, j--)
{
if (i == row && j == col)
{
checkSEflag = 1;
break;
}
}
}
printf("\n%d and %d and %d", i, j, checkSEflag);
}
return checkSEflag;
}
我的输出:
2 和 3 和 0
2 和 3 和 0
2 和 3 和 0
2 和 3 和 0
2 和 3 和 0
。 . .
而且还在继续……
我希望 i 和 j 都增加,直到它们等于 SIZE(SIZE 预定义为 8)或直到 checkSEflag 被分配为等于 1。
看起来 i 和 j 的值并没有改变...... 我尝试将它们从循环条件中取出,而是将它们放置 在循环体中,虽然这并没有改变任何东西。
我怀疑后增量运算符刚刚决定不工作,所以我一定做错了什么,有什么想法吗?
【问题讨论】:
-
(player==0)?(opponent=1):(opponent=0);- WTF - 使用 if 语句使其可读 -
LOL 每个人似乎都讨厌 ?: 运算符
-
我不讨厌
?运算符。它有它的位置 -
opponent = (player==0)做同样的事情并且是可读的。 -
@Jean-FrançoisFabre - 我同意你的观点,我博学的朋友
标签: c infinite-loop post-increment