Skip to content

HOWTO Player Algorithms

lucasdemarchi edited this page Sep 13, 2010 · 15 revisions

This page describe how you can create a Player Algorithm. First of all it’s worth noting the system Trissa uses to detect players algorithms: the object PlayerFactory (instantiated at @Game@’s construction) scan all libraries in a given path (its constructor’s parameter) and try opening it and searching for predefined set of classes/functions which compose a valid Player module. This set is formed by:

  1. A class derived from Player, part of Trissa’s core. As this is an abstract class, you have to implement it. Besides it, you have to define one static parameter: static char* name;
  2. A function that instantiates an object from this class;
  3. A function that returns the player’s name. This is necessary to PlayerFactory to be able to list all available players without having to instantiate them.

The later two functions should never be written by the author of a Player algorithm. Instead, there’s a macro defined in Player.hREGISTER_PLAYER – that automatize their definition and should be used in order to keep your algorithm compatible between Trissa’s releases. Unless there’s a big change in Trissa’s core (not likely to happen in near future), this way of making a new algorithm and also the use of this macro, is of great importance.

Following, a skeleton of a Player algorithm:

<pre> #include "Player.h"

using namespace std;

class MyPlayer : public trissa::Player {
public:
static char* name;
MyPlayer(int dimension, trissa::PlayerType player_type)
: trissa::Player(dimension, player_type){
//(…)
//Put your constructor’s implementation here
//(…)
}
~MyPlayer(){
//Put your destructor’s implementation here
}
virtual trissa::Move* play(trissa::Cube const& board, trissa::Move const& opponentMove){
//(…)
//Your implementation of deciding in which position to put a piece
//(…)
}
virtual trissa::Move* firstPlay(){
//(…)
//Your implementation of deciding in which position to put a piece when your player
//is the first one to play.
//(…)
}
virtual const char * getName() const {
return name;
}
/This function is here for possible future use.
*By now, you can use it at player’s constructor to control if your algorithm
*knows how to play in a given board dimension. If it doesn’t know how to play,
*constructor must raise an exception as defined in Player.h
*/
virtual bool isKnownBoard(unsigned int dimension) const {
//(…)
return false;
//(…)
return true;
//(…)
}
};
char
MyPlayer::name = (char*)“My Player Name”;

REGISTER_PLAYER(MyPlayer) // <— Note that there isn’t a semicolon here

As you see above, it’s not so difficult to create a player algorithm. You can use this skeleton for creating your own player, or just navigate through source code and take, for instance, RandomPlayer.cc as a start point.

Clone this wiki locally