Taro Taro - 1 month ago 21
C Question

Two dimension array and enum

This is a evaluation function for Tic Tac Toe. There is a error around the line 14 : 'else' without a previous 'if' but I could not find out what the error is. What should I change the code??

#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;
}

Answer

You need to add braces.

#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;
}

That is because you have more than 1 instruction in your conditionnal block. It is not like a switch ;)

Comments