-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_tree_test.cpp
More file actions
130 lines (114 loc) · 3.56 KB
/
state_tree_test.cpp
File metadata and controls
130 lines (114 loc) · 3.56 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
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
//============================================================================
// Name : state_tree_test.cpp
// Author : alexander koenen
// Version : 0
// Copyright :
// Description : Test construction of a state tree
//============================================================================
#include <iostream>
#include <cstdlib>
#include "Tree.h"
#include "Node.h"
#include "State.h"
#include "minimax.h"
#define USE_ATTACK_1 1
#define USE_ATTACK_2 2
#define USE_ATTACK_3 3
#define USE_ATTACK_4 4
#define SWITCH 5
#define NUM_OF_ACTIONS 2
#define INVALID -2
using namespace std;
void GWFS(Node<State>* root);
void generateSuccessors(Node<State>* parent){
if (parent == NULL) {
cout << "NULL value passed to Successor Generation" << endl;
}
int selectedAction;
for(selectedAction = USE_ATTACK_1; selectedAction <= NUM_OF_ACTIONS; selectedAction++) {
State* nextState = parent->getValue()->nextState(selectedAction);
if (nextState != NULL) {
Node<State>* child = new Node<State>(nextState);
if( child->getValue()->whoseTurn != INVALID){
parent->addChild(child);
if(!child->getValue()->isOver) {
generateSuccessors(child);
}
}
}
}
}
void GBFS(Node<State>* root){
if (root == NULL) {
cout << "Tried to search null tree" << endl;
}
int maxValue = -100000;
Node<State>* children = root->getChildren();
Node<State>* bestChild;
if (children == NULL) {
return;
}
while(children != NULL) {
if (children->getValue()->value > maxValue) {
maxValue = children->getValue()->value;
bestChild = children;
}
children = children->getNext();
}
cout << "----------------------------------------------The best move is---------------------------------------------------------" << endl;
bestChild->getValue()->print();
GWFS(bestChild);
}
void GWFS(Node<State>* root){
if (root == NULL) {
cout << "Tried to search null tree" << endl;
}
int minValue = 100000;
Node<State>* children = root->getChildren();
Node<State>* bestChild;
if (children == NULL) {
return;
}
while(children != NULL) {
if (children->getValue()->value < minValue) {
minValue = children->getValue()->value;
bestChild = children;
}
children = children->getNext();
}
cout << "----------------------------------------------The best move is---------------------------------------------------------" << endl;
bestChild->getValue()->print();
GBFS(bestChild);
}
int main(int args, char** argv) {
cout << "start State Tree Test" << endl;
int i = 1;
int selectedAction = USE_ATTACK_1;
int value;
bool done = false;
TinyAttack* ta1 = new TinyAttack("damage", -3, 5, 3);
TinyAttack* ta2 = new TinyAttack("heal", 4, 2, 3);
Pokeman* p1 = new Pokeman(5, 5, ta1, ta2, ta1, ta1);
Pokeman* p2 = new Pokeman(*p1);
p1->print();
p2->print();
State* state = new State(p1,p2,0);
Tree<Node<State>,State> tree(state);
generateSuccessors(tree.getThis());
//tree.printClass();
cout << "Starting State" << endl;
state->print();
GBFS(tree.getThis());
cout << "---------------END GBWFS ------------------------" << endl;
// cout << "Minimax: " << minimax(tree.getThis()) << endl;
return i;
}
/*int StateEval(State* state){
if(state == NULL){
cout << "Null state provided to State Evaluation function" << endl;
return -1;
}
int myHealth = state->myPokemon->getHealth();
int hisHealth = state->thatBastardsPokemon->getHealth();
return myHealth - hisHealth;
}*/