-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.h
71 lines (52 loc) · 1.83 KB
/
player.h
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Abstract interface for game player (AI or human)
//
// Created by eitan on 12/2/15.
//
#pragma once
#include "move.h"
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace grandeur {
class Player {
public:
Player(player_id_t pid) : pid_(pid) {}
virtual ~Player() = default;
// Main interface Player must satisfy: pick a game move for a given board.
virtual GameMove getMove(const Board& board, const Moves& legal) const = 0;
player_id_t pid_;
};
using Players = std::vector<const Player*>;
///////////////////////////////////////////////////////////
// Abstract player factory to generate Player* from name.
// This class is a singleton.
class PlayerFactory {
public:
using creator_t = std::function<const Player* (player_id_t)>;
using name_t = std::string;
// We register Player classes with a unioque name, and a callback that
// creates (and allocates) a new concrete Player instance
void registerPlayer(name_t name, creator_t creator);
// Create a new Player with a given player ID.
// Returns nullptr if the name can't be found in the registry.
const Player* create(name_t, player_id_t);
// Return a list of all Player names registered to factory
std::vector<name_t> names() const;
static PlayerFactory& instance()
{
static PlayerFactory singleton;
return singleton;
}
// Use this struct with a static dummy variable to automatically register your Player
struct Registrator {
Registrator(name_t name, creator_t creator) { instance().registerPlayer(name, creator); }
};
private:
struct Impl;
std::unique_ptr<Impl, void (*)(Impl*)> pImpl_;
PlayerFactory();
PlayerFactory(const PlayerFactory&) = delete;
PlayerFactory& operator=(const PlayerFactory&) = delete;
};
} // namespace