-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.h
200 lines (154 loc) · 3.64 KB
/
Node.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef IA2_NODE_H
#define IA2_NODE_H
#include <array>
#include "Board.h"
using namespace std;
class Node{
private:
Board board;
Node* parent;
char player; //'C' = Computer, 'H' = Human | Whose turn is it
char piece; //Whose piece turn is it to play
int move; //In what column was the move that originated this node
int depth;
int score; //Score of the board, depending on what piece turn is it to play and whose turn is it
array<Node*, 7> children;
public:
Node(char player_);
Node(Node& node, int column);
Node(Node& node, int column, int depth_);
~Node();
inline Board getBoard();
inline char getPlayer();
inline char getPiece();
inline int getMove();
inline int getDepth();
inline int getScore();
inline array<Node*, 7> getChildren();
void setScore(int score_);
int checkGameOver();
bool validMove(int column);
array<Node*, 7> makeDescendants(int& generatedNodes);
int utility(char myPiece);
void deleteChild(Node* child);
};
Node::Node(char firstPlayer){
board = Board();
parent = NULL;
player = firstPlayer;
//Piece 'X' is the first piece to play
piece = 'X';
move = -1;
depth = 0;
score = 0;
children = {};
}
Node::Node(Node& node, int column){
board = node.getBoard();
if(node.getPiece() == 'X'){
board.insertPiece(column, 'X');
piece = 'O';
}
else{
board.insertPiece(column, 'O');
piece = 'X';
}
parent = &node;
if(node.getPlayer() == 'C')
player = 'H';
else
player = 'C';
move = column;
depth = node.getDepth() + 1;
score = 0;
children = {};
}
Node::Node(Node& node, int column, int depth_){
board = node.getBoard();
if(node.getPiece() == 'X'){
board.insertPiece(column, 'X');
piece = 'O';
}
else{
board.insertPiece(column, 'O');
piece = 'X';
}
parent = &node;
if(node.getPlayer() == 'C')
player = 'H';
else
player = 'C';
move = column;
depth = depth_;
score = 0;
children = {};
}
Node::~Node(){
if(parent!=NULL)
parent->deleteChild(this);
for(unsigned int i=0; i<7; i++){
if(children[i]!=NULL){
delete children[i];
}
}
}
inline Board Node::getBoard(){
return board;
}
inline char Node::getPlayer(){
return player;
}
inline char Node::getPiece(){
return piece;
}
inline int Node::getMove(){
return move;
}
inline int Node::getDepth(){
return depth;
}
inline int Node::getScore(){
return score;
}
inline array<Node*, 7> Node::getChildren(){
return children;
}
inline void Node::setScore(int score_){
score = score_;
}
int Node::checkGameOver(){
return board.checkGameOver();
}
bool Node::validMove(int column){
return board.validMove(column);
}
array<Node*, 7> Node::makeDescendants(int& generatedNodes){
array<int, 7> moves = board.possibleMoves();
array<Node*, 7> l = {};
unsigned int index = 0;
for(unsigned int i=0; i<7 && moves[i]!=-1; i++){
Node *node = new Node(*this, moves[i]);
for(unsigned int i=0; i<7; i++){
if(children[i]==NULL){
children[i] = node;
break;
}
}
generatedNodes++;
l[index] = node;
index++;
}
return l;
}
int Node::utility(char myPiece){
return board.utility(myPiece, piece);
}
void Node::deleteChild(Node* child){
for(unsigned int i=0; i<7; i++){
if(children[i]==child){
children[i] = NULL;
break;
}
}
}
#endif