NegaMax Algorithm and TicTacToe...What's wrong?
I am new to game tree algorithms, and i've tried to implement a simple tictactoe game which utilizes the NegaMax algorithm to evaluate tile scores for the computer AI player. However, the AI behaves not in a smart way (i can win all the time, because the AI does not block my moves), and i think i implemented the NegaMax algorithm wrongly.
If anyone could help me with the following code, it would be great. I spent such a long time trying different things out, but i never got it to work.
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <limits.h>
#include "game_board.h"
/**
* Constructs a new game board.
*
* @param size the size of the board (the length of fields)
* @return a new GameBoard
*/
GameBoard* game_board_new(int size)
{
GameBoard* board = (GameBoard*) calloc(1, sizeof(GameBoard));
board->size = size;
board->turn = WHITE;
board->fields = (enum Player*) calloc(size, sizeof(enum Player));
return board;
}
/**
* Releases a game board.
*
* @param board the GameBoard to release
*/
void game_board_free(GameBoard* board)
{
free(board->fields);
free(board);
}
/**
* Copies a game board.
*
* @param original the GameBoard to copy
* @return a new GameBoard which is copied from the original
*/
GameBoard* game_board_copy(GameBoard* original)
{
GameBoard* copy;
int i;
copy = game_board_new(original->size);
copy->size = original->size;
copy->turn = original->turn;
for (i = 0; i < original->size; i++)
{
copy->fields[i] = original->fields[i];
}
return copy;
}
/**
* Returns the winner for the current game or EMPTY if
* there is no winner, yet.
*
* @param board the GameBoard to check
* @return the winning Player or EMPTY
*/
enum Player game_board_check_winner(GameBoard* board)
{
enum Player* f;
f = board->fields;
// Columns
if (f[0] != EMPTY && f[0] == f[3] && f[0] == f[6])
return f[0];
if (f[1] != EMPTY && f[1] == f[4] && f[1] == f[7])
return f[1];
if (f[2] != EMPTY && f[2] == f[5] && f[2] == f[8])
return f[2];
// Rows
if (f[0] != EMPTY && f[0] == f[1] && f[1] == f[2])
return f[0];
if (f[3] != EMPTY && f[3] == f[4] && f[4] == f[5])
return f[3];
if (f[6] != EMPTY && f[6] == f[7] && f[7] == f[8])
return f[6];
// Diagonal
if (f[0] != EMPTY && f[0] == f[4] && f[4] == f[8])
return f[0];
if (f[2] != EMPTY && f[2] == f[4] && f[4] == f[6])
return f[2];
return EMPTY;
}
void game_board_toggle_player(GameBoard* board)
{
if (board->turn == WHITE)
board->turn = BLACK;
else if (board->turn == BLACK)
board->turn = WHITE;
}
int game_board_is_ended(GameBoard* board)
{
return game_board_check_winner(board) || game_board_is_full(board);
}
static int evaluate(GameBoard* board)
{
enum Player winner = game_board_check_winner(board);
if (winner != EMPTY && winner == board->turn)
{
return 1;
}
else if (winne开发者_开发知识库r != EMPTY && winner != board->turn)
{
return -1;
}
else
{
return 0;
}
}
int negamax(GameBoard* board, int depth)
{
int bestvalue = INT_MIN;
int i;
int value;
enum Player winner = game_board_check_winner(board);
if (winner != EMPTY || depth == 0)
return evaluate(board);
for (i = 0; i < board->size; i++)
{
if (board->fields[i] == EMPTY)
{
GameBoard* copy = game_board_copy(board);
game_board_make_move(copy, i);
game_board_toggle_player(board);
value = -negamax(copy, depth-1);
bestvalue = max(value, bestvalue);
game_board_free(copy);
}
}
return bestvalue;
}
/**
* Evaluates the current board according to NegaMax
* algorithm.
*
* @param board the GameBoard to evaluate
* @return a NegaMax score
*/
int game_board_evaluate(GameBoard* board, int* best_pos)
{
int i;
int maxVal;
int val;
maxVal = INT_MIN;
for (i = 0; i < board->size; i++)
{
if (board->fields[i] == EMPTY)
{
val = negamax(board, 9);
if (val > maxVal)
{
maxVal = val;
printf("Best move: %i\n", i);
(*best_pos) = i;
}
}
}
return maxVal;
}
int game_board_is_full(GameBoard* board)
{
int i;
for (i = 0; i < board->size; i++)
{
if (board->fields[i] == 0)
{
return 0;
}
}
return 1;
}
void game_board_make_move(GameBoard* board, int pos)
{
board->fields[pos] = board->turn;
}
/**
* Prints a game board.
*
* @param board the GameBoard to print
*/
void game_board_print(GameBoard* board)
{
int i;
for (i = 0; i < board->size; i++)
{
printf("%i (%i)\t", board->fields[i], i);
if (i == 2 || i == 5)
printf("\n");
}
printf("\n");
}
In a NegaMax framework the evaluation function has to look different. Try following evaluation function:
int evaluateNegaMax(GameBoard* board)
{
if (board->turn == MAXPLAYER)
return evaluate(board);
else
return -evaluate(board);
}
The chessprogramming site explains why:
In order for negaMax to work, your Static Evaluation function must return a score relative to the side to being evaluated
Shouldn't
game_board_toggle_player(board);
be
game_board_toggle_player(copy);
?
The usual error is to start with MIN at each level rather than passing values down the tree. I haven't studied your code in detail to compare with the paradigmatic negamax algorithm, but it seems that you may be making that error.
精彩评论