vendredi 15 décembre 2017

If-statements in C programming not functioning and I don't know why

this may seem embarassing but I really need your help. I am programming TicTacToe in C and the last thing I need to debug is my checkifwon function. Please see code:

    int checkifWon(int player){
    int spacecounter = 0; //used to count if we still have spaces left on the board.
    int result = 0;

    if (board[1][1] == board[1][2] && (board[1][1] == board[1][3])) {       //check first row
        return 1;
    }
    else if (board[2][1] == board[2][2] && (board[2][1] == board[2][3])) {  //check second row
        return 1;
    }
    else if (board[3][1] == board[3][2] && (board[3][1] == board[3][3])) {  //check last row
        return 1;
    }
    else if (board[1][1] == board[2][1] && (board[1][1] == board[3][1])) {  //check first column
        return 1;
    }
    else if (board[1][2] == board[2][2] && (board[1][2] == board[3][2])) {  //check second column
        return 1;
    }
    else if (board[1][3] == board[2][3] && (board[1][3] == board[3][3])) {  //check third column
        return 1;
    }
    else if (board[3][1] == board[2][2] && (board[3][1] == board[1][3])) {  //diagonal nach rechts
        return 1;
    }
    else if (board[3][3] == board[2][2] && (board[3][3] == board[1][1])) {  //diagonal nach links
        return 1;
    }

    //else tie = 1;

    return 0;

}

If I just enter a cell it automatically assumes true and ends the game. However if I delete any SEVEN of the 8 conditions and run the remaining condition, it works smoothly and terminates as it should so there must be something wrong with my else if statements. Can anyone shed some light please?

Aucun commentaire:

Enregistrer un commentaire