作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 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/
我是一名优秀的程序员,十分优秀!