-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamesolver.h
79 lines (64 loc) · 1.68 KB
/
gamesolver.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
#ifndef GAMESOLVER_H
#define GAMESOLVER_H
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <map>
#include <set>
#include "game.h"
#define UNSPECIFIED -420
using namespace std;
class GameSolver {
public:
explicit GameSolver() {
}
void solveAllStates() {
data.clear();
tt.clear();
getScore(1);
}
map<vector<int>,float> getData() {
return data;
}
private:
Game game;
map<vector<int>,float> data;
map<int,float> tt;
float getScore(int color) {
int hash = game.getHash();
if (tt.count(hash)) {
return color * tt[hash];
}
float output = UNSPECIFIED;
for (auto & move : game.getMoves()) {
float score;
game.makeMove(move);
if (game.isOver()) {
int winner = game.getWinner();
if (winner == PLAYER_X) {
score = 1;// - 0.05 * game.rounds;
} else if (winner == PLAYER_O) {
score = -1;// + 0.05 * game.rounds;
} else {
score = 0;
}
score *= color;
} else {
score = -getScore(-color);
}
data[game.getTuples()] = color * score;
tt[game.getHash()] = color * score;
game.undoMove();
if (output == UNSPECIFIED) {
output = score;
} else {
output = max(output, score);
}
}
data[game.getTuples()] = color * output;
tt[game.getHash()] = color * output;
return output;
}
};
#endif // GAMESOLVER_H