dimanche 7 février 2021

Recursive function doesn't return the value of the cancel condition

I have a recursive function that simulates a Bashni-Game (kind of checkers game). My function gets the field and my colour. It checks if the game is over, which is the cancel condition of my recursion. For -1 it means the game isnt over so it calls itself recursively with changed colour and the new gameboard. If the game is over it either returns 0 or 1.

int simulate(int myColour, char field[WIDTH][HEIGTH][MAX_TOWER_LENGTH]){
    int cancel = checkForEndOfGame(field); 
    //int returnValue=50;
    struct moveArray moves;
    memset(&moves, 0, sizeof(moves)); 
    printf("DEBUG: CANCEL-Value: %i\n", cancel);
    if(cancel==-1){
        moves=calculatePossibleMoves(moves, field, myColour);
        int indexMove = rand() % moves.count;
        makeMove(field, moves.moves[indexMove]);
        printGameboard(field);
        int colourChange;
        if(myColour==WHITE){
            colourChange=BLACK;
        } else colourChange=WHITE;
        simulate(colourChange, field);
    }
    if(cancel==0){
        printGameboard(field);
        return 1;
    }
    if(cancel==1){
        printGameboard(field);
        return 0;
    }
    return 13;
}

Even if cancel has the value 1 (what means I should get the return 0), my function returns 13. I put the return 13 just because otherwise my compiler (gcc) gives my the error:

error: non-void function does not return a value in all control paths
      [-Werror,-Wreturn-type]

Aucun commentaire:

Enregistrer un commentaire