gpt4 book ai didi

c - 二维数组和枚举

转载 作者:行者123 更新时间:2023-11-30 15:04:17 28 4
gpt4 key购买 nike

这是 Tic Tac Toe 的评估函数。第 14 行周围有一个错误:“else”没有前面的“if”,但我无法找出错误是什么。我应该更改什么代码?

#include <stdio.h>
#include <stdlib.h>

enum ttt {X, O, UNMARKED};
enum ttt board[3][3];
enum evalboard{XWon, OWon, NoOneWon, NotOverYet};

void evaluation(void){
int i;

/*Check the rows*/
for(i = 0; i < 3; i = i + 1){
if( (board[i][0] == X) && (board[i][1] == X) && (board[i][2] == X) )
evalboard = XWon; break;

else if(board[i][0] == O && board[i][1] == O && board[i][0] == O )
evalboard = OWon; break;

else if(board[i][0] == UNMARKED || board[i][1] == UNMARKED || board[i][2] == UNMARKED )
evalboard = NotOverYet; break;

else if(board[i][0] != UNMARKED || board[i][1] != UNMARKED || board[i][2] != UNMARKED )
evalboard = NoOneWon; break;
}

/*Check the columns*/
for(i = 0; i < 3; i = i + 1){
if( board[0][i] == X && board[1][i] == X && board[0][i] == X )
evalboard XWon; break;

else if( board[0][i] == O && board[1][i] == O && board[0][i] == O) )
evalboard OWon; break;

else if(board[0][i] == UNMARKED || board[1][i] == UNMARKED || board[2][i] == UNMARKED )
evalboard = NotOverYet; break;

else if(board[i][0] != UNMARKED || board[i][1] != UNMARKED || board[i][2] != UNMARKED )
evalboard = NoOneWon; break;
}

/*Check the diagonals*/

else if( board[0][0] == X && board[1][1] == X && board[0][0] == X )
evalboard XWon;

else if( board[0][2] == O && board[1][1] == O && board[0][2] == O )
evalboard OWon;
}

最佳答案

您需要添加大括号。

#include <stdio.h>
#include <stdlib.h>

enum ttt {X, O, UNMARKED};
enum ttt board[3][3];
enum evalboard{XWon, OWon, NoOneWon, NotOverYet};

void evaluation(void){
int i;

/*Check the rows*/
for(i = 0; i < 3; i = i + 1){
if( (board[i][0] == X) && (board[i][1] == X) && (board[i][2] == X) )
{
evalboard = XWon;
break;
}
else if(board[i][0] == O && board[i][1] == O && board[i][0] == O )
{
evalboard = OWon;
break;
}
else if(board[i][0] == UNMARKED || board[i][1] == UNMARKED || board[i][2] == UNMARKED )
{
evalboard = NotOverYet;
break;
}
else if(board[i][0] != UNMARKED || board[i][1] != UNMARKED || board[i][2] != UNMARKED )
{
evalboard = NoOneWon;
break;
}
}

/*Check the columns*/
for(i = 0; i < 3; i = i + 1){
if( board[0][i] == X && board[1][i] == X && board[0][i] == X )
{
evalboard XWon;
break;
}
else if( board[0][i] == O && board[1][i] == O && board[0][i] == O) )
{
evalboard OWon;
break;
}
else if(board[0][i] == UNMARKED || board[1][i] == UNMARKED || board[2][i] == UNMARKED )
{
evalboard = NotOverYet;
break;
}
else if(board[i][0] != UNMARKED || board[i][1] != UNMARKED || board[i][2] != UNMARKED )
{
evalboard = NoOneWon;
break;
}
}

/*Check the diagonals*/

else if( board[0][0] == X && board[1][1] == X && board[0][0] == X )
evalboard XWon;

else if( board[0][2] == O && board[1][1] == O && board[0][2] == O )
evalboard OWon;
}

那是因为条件 block 中有超过 1 条指令。它不像开关;)

关于c - 二维数组和枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40346847/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com