Skip to content

Commit

Permalink
finished tree func 'createChildren'
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsantelicesl committed Apr 7, 2024
1 parent 2882e42 commit 6b58ec2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
26 changes: 22 additions & 4 deletions ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,35 @@ float minimax(int board[]){
}

/*
Process: Creates children for a node based on actions
Process: Creates children for a node based on actions. Then calls upon itself recursively until tree has been built.
Input:
actions: vector containing possible actions.
head: The node to be expanded.
player: player's turn
Output: No output. Creates node children.
*/
void createChildren(vector<int> actions, Node *head, bool player) {
void createChildren(vector<int> possibleActions, Node *head, bool player) {

for(int i = 0; i < static_cast<int>(actions.size()); i++) {
int *arr = result(head->board, actions[i], player);
for(int i = 0; i < static_cast<int>(possibleActions.size()); i++) {
int *arr = result(head->board, possibleActions[i], player);
head->addChild(new Node(arr));

char state = winner(head->children[i]->board);

if (!state) {
createChildren(actions(head->children[i]->board), head->children[i], getPlayer(player));
}
else {
if(state == 'X') {
head->children[i]->setValue(1);
}
else if(state == 'O') {
head->children[i]->setValue(-1);
}
else if(state == 'D') {
head->children[i]->setValue(0);
}
}

}
}
12 changes: 10 additions & 2 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Input : 1 dimensional board array
Output : returns X or O depending on winner
*/
char winner(int board[]) {

char retVal = 'D'; // Need it for draw check

// Define winning patterns indexes
int winning_patterns[8][3] = {
{0, 1, 2}, // Row 1
Expand All @@ -55,7 +58,7 @@ char winner(int board[]) {
};

// Check each winning pattern
for (int i = 0; i < 8; ++i) {
for (int i = 0; i < 8; ++i) {
int a = winning_patterns[i][0];
int b = winning_patterns[i][1];
int c = winning_patterns[i][2];
Expand All @@ -69,8 +72,13 @@ char winner(int board[]) {
}
}

// Need to check if draw for creating tree
if(board[i] == 0 || board[8] == 0) {
retVal = '\0';
}

}
return '\0'; // Return null character to indicate no winner yet
return retVal; // Return null character to indicate no winner

}

Expand Down

0 comments on commit 6b58ec2

Please sign in to comment.