-1

I try to implement the minimax algorithm into my connect four game. I´m done with the evaluation-function and halfway done with the algorithm-function.

I just can`t find the solution for the "last" problem. Here are my functions:

void minimax(field f){
int i;
field c;
convert_1D_to_2D(f, c);
for(i=0;i<COLS;i++) {
    if(can_throw(c, i) == 0) {
        throw(f, i);
        convert_1D_to_2D(f, c);
        if((is_winner(c) == 0) && (is_board_full(f) == 0)) { //no winner, board not full
            minimax(f);
        }
        else if(is_winner(c) == 1) { //there is a winner
            evaluate_turn(f);
            //compare evaluation
            undo_turn(f);
        }
        else if(is_winner(c) == 0 && (is_board_full(f) == 1)) { //no winner, board full
            evaluate_turn(f);
            //compare evaluation
            undo_turn(f);
        }
    }
}

The field is an array with f[COLS*ROWS+1], where f[0] is the depth and the other elements save in which columns were thrown. the "c"-board represents the "graphical" board with 0 for free, 1 for player 1 and 2 for player 2.

static int evaluate_turn(field f) {
field c;
convert_1D_to_2D(f, c);
if (((f[0] % 2) == 1) && (current_player == 1) && (is_winner(c) == 1) ) { //player 1 won, max for him || +1
    return 1;
}
else if (((f[0] % 2) == 2) && (current_player == 2) && (is_winner(c) == 1) ) { //player 2 won, max for him || +1
    return 1;
}
if (((f[0] % 2) == 1) && (current_player == 2) && (is_winner(c) == 1) ) { //player 2 won, counting for 1 || -1
    return -1;
}
else if (((f[0] % 2) == 2) && (current_player == 1) && (is_winner(c) == 1) ) { //player 1 won, counting for 2 || -1
    return -1;
}
else if ((is_board_full(f) == 1) && (is_winner(c) == 0)) { //draw || 0
    return 0;
}

So my problem is, that i can't think of a clean solution to compare the evaluation bottom to top. I really think, that I don't need to introduce a new datastructure (which would get way too big). It's like the solution is right in front of me but i can't grab it.

Is it possible to just compare the evaluation on the "way back" of the recursion? If yes, how?

Or do I really need to introduce something new more complex? Or maybe I'm missing off something completely?

Thanks!

whatever.idc
  • 92
  • 1
  • 1
  • 10

1 Answers1

1

Or do I really need to introduce something new more complex? Or maybe I'm missing off something completely?

Unfortunately the answer is the latter. Minimax is not a void function. It returns the value of the node that it represents. That is how evaluation is compared. You are also missing another fundamental concept. Your function only considers terminal nodes to be those where the game is won or the board is full. While this is technically true, no real minimax function works that way. The number of nodes would be around 7^48, so your function would literally take upwards of ten years to terminate on a modern pc. What real world minimax functions do is set a maximum depth for the search to reach (unless you add tree pruning expect this to be 5 or 6), and consider all nodes at that depth to be terminal and evaluate them using a heuristic (inexact guess) evalation function. In connect four this could be based on something like the number of three in a rows. Another mistake you made is calling your eval function if you know there is a winner. If you know which player won than return the proper value straight out, no need to call the expensive eval function. You also cannot stream line your function for both min and max as you did. You must either create a seperate function for min and max, or use the negamax variant.

My advise: It seems you don't really understand how the algorithm should be implemented. Read up on minimax and negamax psuedocode.

chessprogrammer
  • 768
  • 4
  • 15