-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturn_minimax.cpp
More file actions
67 lines (59 loc) · 1.44 KB
/
turn_minimax.cpp
File metadata and controls
67 lines (59 loc) · 1.44 KB
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
#include <iostream>
#include <climits>
#include "Node.h"
#include "turn_minimax.h"
#include "Turn.h"
using namespace std;
int turn_minimax(Node<Turn> *tree) {
int *useMove = new int(0);
int *depth = new int(0);
int v = max_value(tree, INT_MIN, INT_MAX);
tree->setMinimaxValue(&v);
return v;
}
int max_value(Node<Turn> *tree, int alpha, int beta) {
if (!tree->hasChildren()){
int *i = new int(tree->getValue()->getValue());
tree->setMinimaxValue(i);
return tree->getValue()->getValue();
}
Node<Turn> *temp = tree->getChildren();
int v = INT_MIN;
while (temp != NULL) {
v = max(v, min_value(temp, alpha, beta));
if (v <= alpha) {
return v;
}
beta = min(beta, v);
temp = temp->getNext();
}
// cout << "Setting minimax value " << endl;
// cout << v << endl;
int *i = new int(v);
tree->setMinimaxValue(i);
delete i;
return v;
}
int min_value(Node<Turn> *tree, int alpha, int beta) {
if (!tree->hasChildren()){
int *i = new int(tree->getValue()->getValue());
tree->setMinimaxValue(i);
return tree->getValue()->getValue();
}
Node<Turn> *temp = tree->getChildren();
int v = INT_MAX;
while (temp != NULL) {
v = min(v, max_value(temp, alpha, beta));
if (v <= alpha) {
return v;
}
beta = max(beta, v);
temp = temp->getNext();
}
// cout << "Setting minimax value " << endl;
// cout << v << endl;
int *i = new int(v);
tree->setMinimaxValue(i);
delete i;
return v;
}