我正在编写一个函数,用于检查"noughts and crosses"游戏中是否有赢家,或者"连续三次".
游戏的工作原理是首先在命令提示符中绘制3 x 3个方块.然后用户在方块之间移动光标,然后按Enter键以放置"X".下一个选定的方格中放置一个"O",然后再放一个"X",依此类推.在第5轮(第一个可能的转弯可能有胜利者)之后,该程序检查在每个回合之后是否有赢家,如果在9转之后没有找到(当所有方块都有某些东西时)该程序宣布平局.
但是,我为检查获胜者而编写的函数在被调用时总是返回1,这意味着有一个胜利者(更具体地说是X,因为那是最后一次移动的那个).因此,无论方块包含什么,游戏都在第5轮结束.
这是我的代码:
int control(char p[3][3]) {
int i;
for (i = 0; i <= 3; i++) //Checks if any of the horizontals have 3 of the same markers in a row
if (p[i][1] == p[i][2] && p[i][2] == p[i][3])
return 1;
for (i = 0; i <= 3; i++) //Checks if any of the vertical columns have 3 of the same markers
if (p[1][i] == p[2][i] && p[2][i] == p[3][i])
return 1;
else if …Run Code Online (Sandbox Code Playgroud)