Skip to content

Consider extracting a function for calculating minimum/maximum score in minimax.c #2

@rodikh

Description

@rodikh

In the minimax.c file, inside the minimax function, you have two very similar for loops, at line 28 and 58.
consider extracting that whole code to another function, so that you don't need to write the same code twice, or, you can turn it into only one loop, and parameterising the differences between them.

i.e. instead of having two for loops, have one that does different things according to parameters.
so instead of
score = minimax(..., ..., ..., 0);
you can use
score = minimax(..., ..., ..., !is_max_turn);

and you can add both the if (score < min_val){ and if (score > max_val){ lines by turning them into one parameter val, and adding another condition like so:

if (is_max_turn && score > val) {
   val = score;
   best_move_index = i;
}
if (!is_max_turn && score < val){
    val = score;
    best_move_index = i;
}

This will surely make the entire file shorter, and easier to understand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions