-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tree.h
136 lines (89 loc) · 3.56 KB
/
Tree.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
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
131
132
133
134
#include <iostream>
#include <string>
#include <vector>
#include <limits>
#include "Board.h"
#ifndef Tree_H
#define Tree_H
using namespace std;
class Tree
{
private:
//side length of the Othello board
static const int size = 8;
//percentage of the "neuron" that gets increment if its path results in a good outcome
// static constexpr double perc = 0.25;
struct node
{
//uses 2D char array instead of 2D char pointer array, because pointer arrays take up 4 times as much space as char arrays.
char board[size+1][size+1] = {"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000",
"00000000"};
char piece = '0';
//heuristic value
float h = 0;
short level = 0;
//weights for reinforement learning
float good = 0;
float bad = 0;
//for Monte Carlo Tree Search
int num_wins = 0;
int num_plays = 0;
//children
vector<node*> next;
//parent
node* prev;
};
public:
Board board_obj;
char piece = '0';
char AI_piece = '0';
char player_piece = '0';
//the piece that will get the worse heuristic. 0 by default.
char worse_heuristic_piece = '0';
//root node of the tree
node * root = NULL;
//current position in the tree
node * ptr;
//depth should be odd so as to not give advance to the side in minimax that wants to maximize or minimize.
int max_depth = 4;
int max_h_depth = 4;
int good_weight = 100;
int num_nodes = 0;
Tree();
void eraseParentNodes(node * ptr);
void eraseBranch(node * ptr, node * ptr_to_keep);
void resetTree();
void eraseTree(node * ptr);
//links a new node to ptr, with initialized board
void newNode(node * ptr, char (&new_board)[size+1][size+1], char piece);
void determinePossibleMoves(node* ptr, char piece);
void monteCarlo(node* ptr, int num_runs);
double negamax(node* start, node* ptr, int depth_left, double alpha, double beta, int is_maximizing);
double getMinHeuristic(node* start, node * ptr, double alpha, double beta, int depth_left);
double getMaxHeuristic(node* start, node * ptr, double alpha, double beta, int depth_left);
double calculateHeuristic(node* start, node* ptr);
bool hasLegalMoves(node* ptr);
//returns index of child with min heuristic
int getIndexMinHeuristic(node* ptr);
int getIndexMaxHeuristic(node* ptr);
void iterateTreeDepth(node* ptr, char piece, int cur_depth, int max_depth);
//returns a new board containing Tree's next move
void AIMove(int col, int row);
void playerMove(int col, int row);
//moves to child located at child_index of ptr
void move(int child_index);
//reinforcement methods
void reinforceGood(node * ptr);
void reinforceBad(node * ptr);
void printNode(node * ptr, int indent=0);
void printNet(node * ptr, int indent=0);
char getOtherPiece(char piece);
int numChildren(node * ptr);
};
#endif