-
Notifications
You must be signed in to change notification settings - Fork 0
/
minmax.cc
83 lines (71 loc) · 1.93 KB
/
minmax.cc
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
/*
* minmax.cc
*
* Created on: 26-10-2012
* Author: Krzysztof Pobiarżyn
*/
#include "includes/minmax.h"
#include "includes/game.h"
#include "includes/search_node.h"
#include "includes/helper.h"
#include <sstream>
#include <iostream>
MinMax::MinMax() :
AI(), depth(4), serchedStates(0) {
}
MinMax::MinMax(int depth) : AI(), depth(depth){
}
GameState *MinMax::compute(GameState *state) {
std::vector<GameState *> next = nextStates(state);
int bv = -10000000;
GameState* bs = 0;
serchedStates=0;
for (unsigned i = 0; i < next.size(); i++) {
serchedStates++;
std::vector<GameState *> nn = nextStates(next.at(i));
SearchNode* val = search(next.at(i), depth);
if (val->getValue() > bv) {
bs = next.at(i);
bv = val->getValue();
}else
delete val;
}
std::cout << "log: przeszukano " << serchedStates <<" stanow\n";
return (bs);
}
SearchNode* MinMax::search(GameState* state, int depth) {
serchedStates++;
std::vector<GameState *> next = nextStates(state);
if (next.size() == 0 || depth == 0){
return (reward(state));
}
SearchNode *bestMove;
if(state->player() != Game::getInstance().state()->player())
bestMove = new SearchNode(0,10000);
else
bestMove = new SearchNode(0,-10000);
if(state->player() == Game::getInstance().state()->player()) {
for (unsigned i =0; i < next.size(); i++) {
SearchNode *val = search(next.at(i), depth - 1);
if (val->getValue() > bestMove->getValue()){
bestMove->setState(val->getState());
bestMove->setValue(val->getValue());
delete val;
}
}
} else {
for (unsigned i = 0; i < next.size(); i++) {
SearchNode *val = search(next.at(i), depth - 1);
if (val->getValue() < bestMove->getValue()){
bestMove->setState(val->getState());
bestMove->setValue(val->getValue());
delete val;
}
}
}
return (bestMove);
}
void MinMax::pickState(){
GameState *newState = compute(Game::getInstance().state());
Game::getInstance().updateState(newState);
}