-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpectimax_strategy_player.cpp
65 lines (59 loc) · 2.13 KB
/
expectimax_strategy_player.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
/*
Copyright (C) 2017 Meritxell Jordana
Copyright (C) 2017 Marc Sanchez
*/
#include <math.h>
#include "expectimax_strategy_player.h"
ExpectimaxStrategyPlayer::ExpectimaxStrategyPlayer(Map *map) : ExpectimaxStrategy(map) {}
double ExpectimaxStrategyPlayer::max_value(Map map, CellType agent, int depth) {
if (terminalTest(map, depth)) {
return utility(map);
}
list<Direction> legalMoves = map.getLegalMoves(agent);
double v = -30000000;
for (list<Direction>::iterator a = legalMoves.begin(); a != legalMoves.end(); ++a) {
v = fmax(v, min_value(result(map, agent, *a), Enemy, depth));
}
return v;
}
double ExpectimaxStrategyPlayer::min_value(Map map, CellType agent, int depth) {
if (terminalTest(map, depth)) {
return utility(map);
}
list<Direction> legalMoves = map.getLegalMoves(agent);
double v = 0;
for (list<Direction>::iterator a = legalMoves.begin(); a != legalMoves.end(); ++a) {
if (agent == Enemy) {
v += max_value(result(map, agent, *a), Player, depth-1);
} else {
v += min_value(result(map, agent, *a), Enemy, depth);
}
}
return v/legalMoves.size();
}
Direction ExpectimaxStrategyPlayer::minimax_decision(int depth) {
double v = -3000000;
list<Direction> legalMoves = map->getLegalMoves(Player);
list<Direction> moves;
for (list<Direction>::iterator a = legalMoves.begin(); a != legalMoves.end(); ++a) {
double u = min_value(result(*map, Player, *a), Enemy, depth);
if (u == v) {
moves.push_back(*a);
} else if (u > v) {
v = u;
moves.clear();
moves.push_back(*a);
}
}
return getRandomDirection(moves);
}
double ExpectimaxStrategyPlayer::evaluationFunction(Map &map) {
double totalScore = map.getEatedFoodByPlayer() * 10;
Position playerPosition = map.getPlayerPosition();
set<Position> foodCells = map.getFoodCells();
for (set<Position>::iterator a = foodCells.begin(); a != foodCells.end(); ++a) {
double d = minDistance(*a, playerPosition);
totalScore += 1.0/(d*d);
}
return totalScore;
}