-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinMax.cpp
163 lines (146 loc) · 5.02 KB
/
MinMax.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// MinMax.cpp for ChessIA in /home/wilmot_p/PROG/C++/ChessIA
//
// Made by WILMOT Pierre
// Login <wilmot@epitech.net>
//
// Started on Wed Dec 26 01:52:52 2012 WILMOT Pierre
//
#include <iostream>
#include <algorithm>
#include <boost/iterator/indirect_iterator.hpp>
#include "MinMax.hpp"
#include "LogManager.hpp"
MinMax::MinMax(int depth)
:m_depth(depth)
{
m_piecesVal[GameData::Pawn] = 1;
m_piecesVal[GameData::Bishop] = 3;
m_piecesVal[GameData::Knight] = 3;
m_piecesVal[GameData::Rook] = 5;
m_piecesVal[GameData::Queen] = 9;
m_piecesVal[GameData::King] = 0;
}
MinMax::~MinMax()
{
}
void MinMax::setDepth(int d)
{
m_depth = d;
}
Move const & MinMax::getBestMove(ChessBoard const & current, GameData::team t) const
{
(void)t;
GameData n(current);
static Move ret(n, 3, 3, 3, 3);
RatedMove *m;
m = max(current.getGameData(), t, m_depth);
ret.setGameData(n);
ret.setSX(m->getSX());
ret.setSY(m->getSY());
ret.setDX(m->getDX());
ret.setDY(m->getDY());
ret.apply();
LogManager::getInstance()->log("Found Best Move", LogManager::MinMax);
return (ret);
}
RatedMove *MinMax::min(GameData const &gd, GameData::team t, int d) const
{
RatedMove *ret;
ChessBoard board(gd);
const std::list<Move *> *successorStateList = NULL;
successorStateList = board.getSuccessors(t);
std::list<RatedMove *> *ratedSuccessorStateList = NULL;
ratedSuccessorStateList = new std::list<RatedMove *>(); // Alloc 5 (min-func)
for (std::list<Move *>::const_iterator it = successorStateList->begin() ;
it != successorStateList->end() ; ++it)
{
if (d == 0)
ratedSuccessorStateList->push_back(new RatedMove(*it, eval((*it)->getGameData(), t))); // Alloc 4 (min-func)
else
{
ret = max((*it)->getGameData(), gd.getOtherTeam(t), d - 1);
ratedSuccessorStateList->push_back(new RatedMove(*it, ret->getScore())); // Alloc 4 (max-func)
delete ret;
}
}
ret = &(*min_element(boost::make_indirect_iterator(ratedSuccessorStateList->begin()), boost::make_indirect_iterator(ratedSuccessorStateList->end())));
for (std::list<Move *>::const_iterator it = successorStateList->begin() ;
it != successorStateList->end() ; ++it)
{
delete *it; // Un-alloc 8 (min-func)
}
for (std::list<RatedMove *>::iterator it = ratedSuccessorStateList->begin() ;
it != ratedSuccessorStateList->end() ; ++it)
{
if (*it != ret)
delete *it; // Un-alloc 4 (min-func) ! except one
}
delete ratedSuccessorStateList; // Un-alloc 5 (min-func)
delete successorStateList; // Un-alloc 2 (min-func)
return (ret);
}
RatedMove *MinMax::max(GameData const &gd, GameData::team t, int d) const
{
RatedMove *ret;
ChessBoard board(gd);
const std::list<Move *> *successorStateList = NULL;
successorStateList = board.getSuccessors(t);
std::list<RatedMove *> *ratedSuccessorStateList = NULL;
ratedSuccessorStateList = new std::list<RatedMove *>(); // Alloc 5 (max-func)
for (std::list<Move *>::const_iterator it = successorStateList->begin() ;
it != successorStateList->end() ; ++it)
{
if (d == 0)
ratedSuccessorStateList->push_back(new RatedMove(*it, eval((*it)->getGameData(), t))); // Alloc 4 (max-func)
else
{
ret = min((*it)->getGameData(), gd.getOtherTeam(t), d - 1);
ratedSuccessorStateList->push_back(new RatedMove(*it, ret->getScore())); // Alloc 4 (max-func)
delete ret;
}
}
ret = &(*max_element(boost::make_indirect_iterator(ratedSuccessorStateList->begin()), boost::make_indirect_iterator(ratedSuccessorStateList->end())));
for (std::list<Move *>::const_iterator it = successorStateList->begin() ;
it != successorStateList->end() ; ++it)
{
delete *it; // Un-alloc 8 (max-func)
}
for (std::list<RatedMove *>::iterator it = ratedSuccessorStateList->begin() ;
it != ratedSuccessorStateList->end() ; ++it)
{
if (*it != ret)
delete *it; // Un-alloc 4 (max-func) !exepct one
}
delete ratedSuccessorStateList; // Un-alloc 5 (max-func)
delete successorStateList; // Un-alloc 2 (max-func)
return (ret);
}
float MinMax::eval(GameData const &gd, GameData::team t) const
{
static int counter(0);
ChessBoard c(gd);
float pieceVal[2] = {0, 0};
const std::list<PieceInfo *> *pieces;
pieces = c.getPieces(t);
for (std::list<PieceInfo *>::const_iterator it = pieces->begin() ; it != pieces->end() ; ++it)
{
pieceVal[t] += m_piecesVal[(*it)->getPiece()];
delete *it; // Un-alloc 9
}
delete pieces; // Un-alloc 2
pieces = c.getPieces(gd.getOtherTeam(t));
for (std::list<PieceInfo *>::const_iterator it = pieces->begin() ; it != pieces->end() ; ++it)
{
pieceVal[gd.getOtherTeam(t)] += m_piecesVal[(*it)->getPiece()];
delete *it; // Un-alloc 9
}
delete pieces; // Un-alloc 2
if ((pieceVal[t] - pieceVal[gd.getOtherTeam(t)]) != 0 && false)
{
std::cout << gd << std::endl;
std::cout << (pieceVal[t] - pieceVal[gd.getOtherTeam(t)]) << " = " << pieceVal[t] << " - " << pieceVal[gd.getOtherTeam(t)] << std::endl;
}
counter++;
return (pieceVal[t] - pieceVal[gd.getOtherTeam(t)]);
}