-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.h
49 lines (36 loc) · 1.17 KB
/
AI.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
#pragma once
#include "player.h"
class AI : public player {
public:
AI(game g, piece p) : player(g, p) {}
virtual int next(int move) {
if (move != NO_MOVE)
_g.do_move(move);
move = minimax(_g, _p, NO_MOVE).first;
_g.do_move(move); // update board
return move;
}
private:
// construct sub - tree
static pair<int, int> minimax(game g, piece p, int move) {
if (move != NO_MOVE)
g.do_move(move); // update game
// game over, nothing to do
if (g.winner() != NONE || g.tie())
return pair<int, int>(NO_MOVE, g.winner());
vector<int> poss = g.get_available_squares();
pair<int, int> res = minimax(g, (piece)(-p), poss[0]); // get result of first move
res.first = poss[0]; // substitute son's move with current move
for (vector<int>::iterator it = ++poss.begin(); it != poss.end(); ++it) {
if (res.second == p) // best move possible, (alpha-beta pruning)
return res;
// try new son
pair<int, int> temp = minimax(g, (piece)(-p), *it);
temp.first = *it; // substitue son's move for current move
// got better move
if (temp.second > res.second && p == O || temp.second < res.second && p == X)
res = temp;
}
return res;
}
};