I have this code for AlphaBeta implementation and have created versions of the required interfaces that work perfectly (I have debugged them quite thoroughly). Unfortunately my algorithm keeps returning an empty vector and score of 0. Here is the code:
#include <vector>
#include <limits.h>
using namespace std;
typedef vector<unsigned char> GameMove;
struct MoveScore
{
GameMove move;
int score;
};
class IGameState
{
public:
virtual IGameState &execute(GameMove)=0;//execute the move
virtual IGameState &undo(GameMove)=0;//undo the move
virtual unsigned char *data()=0;
virtual bool gameOver()=0;
};
class IGamePlayer
{
public:
virtual int evaluate(IGameState&)=0;//evaluate the move
virtual vector<GameMove> getMoves(IGameState&)=0;
};
MoveScore AlphaBeta(IGameState ¤t, IGamePlayer &us, IGamePlayer &them, int ply, int low=-INT_MAX, int high=INT_MAX)
{
//RND r;
MoveScore best={GameMove(),0};
if (ply==0||current.gameOver())
{
best.score=us.evaluate(current);
return best;
}
vector<GameMove> moves=us.getMoves(current);
// for (int i=0; i<moves.size(); i++)
// {
// int swap=r.RAND<int>()%moves.size();
// GameMove tmp=moves[i];
// moves[i]=moves[swap];
// moves[swap]=tmp;
// }
for (int i=0; i<moves.size(); i++)
{
current.execute(moves[i]);
MoveScore tmp=AlphaBeta(current,them,us,ply-1,-high,-low);
current.undo(moves[i]);
if ((-tmp.score)>best.score)
{
low=-tmp.score;
best.move=moves[i];
best.score=low;
}
if (low>=high)
{
return best;
}
}
return best;
}
Can anybody see where I went wrong?