Evaluate these funtions

Can anyone explain me a this code line by line and word to word that what is

going on in this code and what is the purpose of each variable ?? how im

getting best move ?? why used INFINITY?

// returns best move for the current computer player
int MiniMax(char _board[9], player _player) {
int best_val = -INFINITY, index = 0;
std::list<int> move_list;
char best_moves[9] = {0};
generate_moves(_board, move_list);
while(!move_list.empty()) {
_board[move_list.front()] = _player.symbol;
cSymbol = _player.symbol;
int val = MinMove(_board, _player);
if(val > best_val) {
best_val = val;
index = 0;
best_moves[index] = move_list.front() + 1;
} else if(val == best_val) {
best_moves[++index] = move_list.front() + 1;
}
_board[move_list.front()] = 0;
move_list.pop_front();
}
if(index > 0) {
index = rand() % index;
}
return best_moves[index];
}
Last edited on
I don't really like this implementation... I find it obfuscated and well, just awful... But here are some answers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int MiniMax(char _board[9], player _player) //Function
{
int best_val = -INFINITY, index = 0; //Set best_val to minus infinity, so we will always have
//a correct best move (one will always be higher than minus infinity). 
std::list<int> move_list; 
char best_moves[9] = {0}; //Set all the best moves to zero so we don't accidently
//use a move with a high value from the last evaluation... 
generate_moves(_board, move_list); //Function call, presumably finding possible moves.
while(!move_list.empty()) //While there are still possible moves remaining. 
{
_board[move_list.front()] = _player.symbol; //Make a move on the board (I think) 
cSymbol = _player.symbol;
int val = MinMove(_board, _player); //Another function call 
if(val > best_val) //If the value of this move is better than the best move found so far. 
{ 
best_val = val;//Make this the new best move. 
index = 0; 
best_moves[index] = move_list.front() + 1;
} else if(val == best_val) { //If the value of this move is equal to the best move found.
best_moves[++index] = move_list.front() + 1;
}
_board[move_list.front()] = 0;
move_list.pop_front();
}
if(index > 0)
{
index = rand() % index;
}
return best_moves[index]; //Return this best move. 
}
If you have better function which i can understand easily than write it here please ..
Also evaluate these functions briefly so i can understand how these functions are working ...

// finds best move for 'min player'
int MinMove(char _board[9], player _player) {
int pos_value = evaluate_position(_board, _player);
if(pos_value != -1) {
return pos_value;
}
int best_val = +INFINITY;
std::list<int> move_list;
generate_moves(_board, move_list);
while(!move_list.empty()) {
_player.symbol == 'X' ? cSymbol = 'O' : cSymbol = 'X';
_board[move_list.front()] = cSymbol;
int val = MaxMove(_board, _player);
if(val < best_val) {
best_val = val;
}
_board[move_list.front()] = 0;
move_list.pop_front();
}
return best_val;
}

// finds best move for 'max player'
int MaxMove(char _board[9], player _player) {
int pos_value = evaluate_position(_board, _player);
if(pos_value != -1) {
return pos_value;
}
int best_val = -INFINITY;
std::list<int> move_list;
generate_moves(_board, move_list);
while(!move_list.empty()) {
_player.symbol == 'X' ? cSymbol = 'X' : cSymbol = 'O';
_board[move_list.front()] = cSymbol;
int val = MinMove(_board, _player);
if(val > best_val) {
best_val = val;
}
_board[move_list.front()] = 0;
move_list.pop_front();
}
return best_val;
}
Topic archived. No new replies allowed.