-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentAI.h
More file actions
94 lines (73 loc) · 1.58 KB
/
Copy pathStudentAI.h
File metadata and controls
94 lines (73 loc) · 1.58 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
#ifndef STUDENTAI_H
#define STUDENTAI_H
#include "AI.h"
#include "Board.h"
#include <cmath>
#pragma once
//DECIDE textbook says this is theoretically best but people often try many
const double C = sqrt(2);
bool movesEqual(const Move& m1, const Move& m2);
//TODO test DESTRUCTOR?
class Node
{
public:
Move move;
unsigned visits;
unsigned parentWins;
std::vector<Node*> children;
Node()
: move(), visits{0}, parentWins{0}, children()
{
}
Node(const Move& move)
: move(move), visits{0}, parentWins{0}, children()
{
}
bool isLeaf() const noexcept
{
return children.empty();
}
void addChild(const Move& move)
{
children.push_back(new Node(move));
}
//undefined iff child is unvisited
double UCT(Node* child) const
{
return static_cast<double>(child->parentWins) / child->visits + C * sqrt(log(visits) / child->visits);
}
//undefined iff has no child
Node* selectChildUCT() const
{
if (!children[0]->visits)
return children[0];
Node* bestSoFar = children[0];
for (Node* child : children)
{
if (!child->visits)
return child;
if (UCT(child) > UCT(bestSoFar))
bestSoFar = child;
}
return bestSoFar;
}
~Node()
{
for (Node* child : children)
delete child;
}
};
//The following part should be completed by students.
//Students can modify anything except the class name and exisiting functions and varibles.
class StudentAI :public AI
{
public:
Board board;
StudentAI(int col, int row, int p);
virtual Move GetMove(Move board);
Node* root;
std::vector<Node*> stack;
bool ourFirstMove;
~StudentAI();
};
#endif //STUDENTAI_H