Question: why wont my code update the binary tree. It keeps saying 0 left or right children for the debugger tests #ifndef GAMEDECISIONTREE _ H #define

why wont my code update the binary tree. It keeps saying 0 left or right children for the debugger tests
#ifndef GAMEDECISIONTREE_H
#define GAMEDECISIONTREE_H
#include
#include
#include
#include
#include "Node.h"
#include "Story.h"
template
class GameDecisionTree {
private:
Node* root;
std::unordered_map*> nodes;
public:
GameDecisionTree() : root(nullptr){}
// Function to load story data from a text file and build the binary tree
void loadStoryFromFile(const std::string& filename, char delimiter){
std::ifstream file(filename);
if (!file.is_open()){
std::cerr "Could not open file " filename std::endl;
return;
}
std::string line;
while (std::getline(file, line)){
std::stringstream ss(line);
int eventNumber;
int leftEventNumber;
int rightEventNumber;
std::string description;
ss >> eventNumber;
std::getline(ss, description, delimiter);
ss >> leftEventNumber >> rightEventNumber;
std::cout "loaded event " eventNumber ", left " leftEventNumber ", right: " rightEventNumber std::endl;
Story story(description, eventNumber, leftEventNumber, rightEventNumber);
Node* node = new Node(story);
nodes[eventNumber]= node;
if (eventNumber ==1){
root = node;
}
}
if (root != nullptr){
std::cout "your root node is set to event " root->data.eventNumber ": " root->data.description std::endl;
}
else {
std::cout "your root node is nullptr :(" std::endl;
}
for (auto& pair : nodes){
Node* node = pair.second;
int leftNum = node->data.leftEventNumber;
int rightNum = node->data.rightEventNumber;
if (leftNum !=-1 && nodes.find(leftNum)!= nodes.end()){
node->left = nodes[leftNum];
std::cout "Node " node->data.eventNumber "'s left child: " node->left->data.eventNumber std::endl;
}
else {
std::cout "Node " node->data.eventNumber " has no left child." std::endl;
}
if (rightNum !=-1 && nodes.find(rightNum)!= nodes.end()){
node->right = nodes[rightNum];
std::cout "Node " node->data.eventNumber "'s right child: " node->right->data.eventNumber std::endl;
}
else {
std::cout "Node " node->data.eventNumber " has no right child." std::endl;
}
}
file.close();
}
// Function to start the game and traverse the tree based on user input
void playGame(){
Node* currentNode = root;
while (currentNode != nullptr){
std::cout currentNode->data.description std::endl;
if (!currentNode->left && !currentNode->right){//check if its the end of the tree (leaf)
std::cout "End of game." std::endl;
break;
}
std::cout "Enter L or R" std::endl;
char letter;
std::cin >> letter;
if (letter =='r'|| letter =='R'){
if (currentNode->right){
currentNode = currentNode->right;
std::cout "You went right." std::endl;
}
else {
std::cout "Right path invalid" std::endl;
}
}
else if (letter =='l'|| letter =='L'){
if (currentNode->left){
currentNode = currentNode->left;
std::cout "You went left." std::endl;
}
else {
std::cout "Left path invalid" std::endl;
}
}
else {
std::cout "Invalid input." std::endl;
}
}
}
};
#endif // GAMEDECISIONTREE_H
Requirements:
Node Class Template
Create a templated Node T > class where T is the Story class.
The Node class should have pointers to its left child and right child.
Story Class
Define a Story class containing:
string description: The text representing what happens at this point
in the game.
int eventNumber: A unique identifier for each event.
int leftEventNumber: The event number of the next event if the
player chooses the left path.
int rightEventNumber: The event number of the next event if the
player chooses the right path.
GameDecisionTree Class Template
Create a templated GameDecisionTree class where T is the Story class.
The GameDecisionTree class should have methods for:
Loading the story data from a file and creating the tree.
Traversing the decision tree based on player input.
Binary Tree Structure
Use a binary decision tree structure to represent game events. Each node will
have two possible outcomes (left and right child).
Implement one exception where multiple events may lead to the same outcome
(i.e., shared child nodes).
Text-Based RPG with External File Input
The game story must be loaded from a text file. Each line in the file should
contain the event description, event number, left child event number, and right
child event number, separated by a delimiter.
The game will start by loading these events into a binary tree, allowing the player
to make decisions and follow paths through the tree based on their choices.
Main Function: The main function of the program should call:
The load file function to load the story into the decision tree.
The play game function to begin the game.
why wont my code update the binary tree. It keeps

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!