forked from lean-poker/poker-player-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.h
117 lines (90 loc) · 2.45 KB
/
strategy.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
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
#ifndef STRATEGY_H
#define STRATEGY_H
#include "player.h"
#include <initializer_list>
#include <vector>
#include <iostream>
class Action
{
public:
ActionType mType;
int mValue;
Action(ActionType type, int value = 0) : mType(type), mValue(value) {}
};
class Condition
{
ConditionType mType;
std::vector<int> mArguments;
public:
template <typename T>
Condition(ConditionType type, std::initializer_list<T> arguments)
: mType(type)
{
for (auto i: arguments)
mArguments.push_back((int)i);
if (type == ConditionType::CARDS && mArguments.size() > 0 && mArguments.back() != (int)CardType::SEPARATOR)
mArguments.push_back((int)CardType::SEPARATOR);
}
bool evaluate(const GameState& state);
};
class Trigger
{
Action mAction;
std::vector<Condition> mConditions;
public:
Trigger(Action action, std::initializer_list<Condition> conditions = {})
: mAction(action), mConditions(conditions) {}
bool evaluate(const GameState& state)
{
bool result = true;
for (Condition i: mConditions)
{
result &= i.evaluate(state);
if (!result)
return false;
}
return true;
}
Action getAction() { return mAction; }
};
class Strategy
{
Trigger mEnteringTrigger;
std::vector<Trigger> mTriggers;
public:
Strategy(Trigger enteringTrigger) : mEnteringTrigger(enteringTrigger) {}
void addTrigger(Trigger trigger) { mTriggers.push_back(trigger); }
bool evaluate(const GameState& state)
{
return mEnteringTrigger.evaluate(state);
}
Action execute(const GameState& state)
{
for (Trigger i: mTriggers)
{
if (i.evaluate(state))
return i.getAction();
}
return mEnteringTrigger.getAction();
}
};
class StrategyManager
{
std::vector<Strategy> mStrategies;
public:
void addStrategy(Strategy strategy) { mStrategies.push_back(strategy); }
Action execute(const GameState& state)
{
if (mStrategies.size() == 0)
return Action(ActionType::NO_ACTION);
for (Strategy i: mStrategies)
{
if (i.evaluate(state))
return i.execute(state);
}
return Action(ActionType::NO_ACTION);
}
};
void initStrategies(StrategyManager& manager);
bool strategyTest();
#endif // STRATEGY_H