-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.h
More file actions
140 lines (96 loc) · 3.21 KB
/
Player.h
File metadata and controls
140 lines (96 loc) · 3.21 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef PLAYER_H
#define PLAYER_H
#include<iostream>
#include<string>
#include "Board.h"
#include "Side.h"
class Player
{
public:
Player(std::string name);
//Create a Player with the indicated name.
std::string name() const;
//Return the name of the player.
virtual bool isInteractive() const;
//Return false if the player is a computer player.Return true if the player is human.Most kinds of players will be computer players.
virtual int chooseMove(const Board& b, Side s) const = 0;
//Every concrete class derived from this class must implement this function so that if the player were to be playing side s and had to make a move given board b, the function returns the move the player would choose.If no move is possible, return −1.
virtual ~Player();
//Since this class is designed as a base class, it should have a virtual destructor.
private:
std::string m_name;
};
/////////////////////////////////////////////////////
//HumanPLayer
/////////////////////////////////////////////////////
class HumanPlayer : public Player
{
public:
HumanPlayer(std::string name);
virtual bool isInteractive() const;
virtual int chooseMove(const Board& b, Side s) const;
};
////////////////////////////////////////////////////
//BadPlayer
////////////////////////////////////////////////////
class BadPlayer : public Player
{
public:
BadPlayer(std::string name);
virtual bool isInteractive() const;
virtual int chooseMove(const Board& b, Side s) const;
//A BadPlayer is a computer player that chooses an arbitrary valid move and returns that choice.
//"Arbitrary" can be what you like: leftmost, nearest to pot, fewest beans, random, etc..
//The point of this class is to have an easy-to-implement class that at least plays legally.
};
//////////////////////////////////////////////////////
//AlarmClock
//////////////////////////////////////////////////////
#include <chrono>
#include <future>
#include <atomic>
class AlarmClock
{
public:
AlarmClock(int ms)
{
m_timedOut = false;
m_isRunning = true;
m_alarmClockFuture = std::async([=]() {
for (int k = 0; k < ms && m_isRunning; k++)
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (m_isRunning)
m_timedOut = true;
});
}
~AlarmClock()
{
m_isRunning = false;
m_alarmClockFuture.get();
}
bool timedOut() const
{
return m_timedOut;
}
AlarmClock(const AlarmClock&) = delete;
AlarmClock& operator=(const AlarmClock&) = delete;
private:
std::atomic<bool> m_isRunning;
std::atomic<bool> m_timedOut;
std::future<void> m_alarmClockFuture;
};
////////////////////////////////////////////////////
//SmartPlayer
////////////////////////////////////////////////////
class SmartPlayer : public Player
{
public:
SmartPlayer(std::string name);
virtual bool isInteractive() const;
virtual int chooseMove(const Board& b, Side s) const;
private:
void smartMove(AlarmClock& ac, const Board& b, Side s, int& besthole, int& heuristic, int depth) const;
int evaluate(Board b) const;
bool complete_move(Side startside, Side endside, int endhole, Board& newboard) const;
};
#endif // !PLAYER_H