I’m trying to design a function template which searches for the best move
for any game – of course the user of this function template has to implement
some game specific functions. What i’m trying to do is to generalize the
alpha beta search algorithm with a function template.
The declaration of this function template looks like this:
template<class GameState, class Move,
class EndGame, class Evaluate, class GetMoves, class MakeMove)
int alphaBetaMax(GameState g, int alpha, int beta, int depthleft);
Among other things the function has to:
- Determine if a game has ended:
bool EndGame(g) - Evaluate the state of a game:
int Evaluate(g) - Get the possible moves:
std::vector<Move> moves = GetMoves(g) - Make a move:
Gamestate gnew = MakeMove(g, moves[i])
Do you think the function has to many template arguments? Is there a way to
reduce the number of arguments? One idea is to extend the GameState class with members
that evaluate the gamestate or decide if the game has ended. But a alpha beta
search tree contains a lot of Gamestate instances which may leads to
unnecessary memory requirements, thus i like to keep Gamestate small. In general, is a function template actually the right way?
You could define an abstract interface say game_traits and have specialized game_traits implementation for each game:
See char_traits in the C++ standard library how it is used there.
Alternatively, you could make them just methods of the Game classes, you don’t need inheritence here from some abstract class since you supply it as a template argument. You will just get a, perhaps not so transparent, compile error when your template function tries to access, say game.has_ended(), when no such method exists. This kind of mechanism is also used a lot in the standard template library.
btw, there was a new feature planned for this; Concepts:
Unfortunately Concepts have been postponed to a future version of the standard and will not yet appear in c++0x 🙁