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 left or right children for the debugger tests
#ifndef GAMEDECISIONTREEH
#define GAMEDECISIONTREEH
#include
#include
#include
#include
#include "Node.h
#include "Story.h
template
class GameDecisionTree
private:
Node root;
std::unorderedmap nodes;
public:
GameDecisionTree : rootnullptr
Function to load story data from a text file and build the binary tree
void loadStoryFromFileconst std::string& filename, char delimiter
std::ifstream filefilename;
if file.isopen
std::cerr "Could not open file filename std::endl;
return;
std::string line;
while std::getlinefile line
std::stringstream ssline;
int eventNumber;
int leftEventNumber;
int rightEventNumber;
std::string description;
ss eventNumber;
std::getliness description, delimiter;
ss leftEventNumber rightEventNumber;
std::cout "loaded event eventNumber left leftEventNumber right: rightEventNumber std::endl;
Story storydescription eventNumber, leftEventNumber, rightEventNumber;
Node node new Nodestory;
nodeseventNumber node;
if eventNumber
root node;
if root nullptr
std::cout "your root node is set to event rootdata.eventNumber : rootdata.description std::endl;
else
std::cout "your root node is nullptr : std::endl;
for auto& pair : nodes
Node node pair.second;
int leftNum nodedata.leftEventNumber;
int rightNum nodedata.rightEventNumber;
if leftNum && nodes.findleftNum nodes.end
nodeleft nodesleftNum;
std::cout "Node nodedata.eventNumber s left child: nodeleftdata.eventNumber std::endl;
else
std::cout "Node nodedata.eventNumber has no left child." std::endl;
if rightNum && nodes.findrightNum nodes.end
noderight nodesrightNum;
std::cout "Node nodedata.eventNumber s right child: noderightdata.eventNumber std::endl;
else
std::cout "Node nodedata.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 currentNodedata.description std::endl;
if currentNodeleft && currentNoderightcheck 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 currentNoderight
currentNode currentNoderight;
std::cout "You went right." std::endl;
else
std::cout "Right path invalid" std::endl;
else if letter l letter L
if currentNodeleft
currentNode currentNodeleft;
std::cout "You went left." std::endl;
else
std::cout "Left path invalid" std::endl;
else
std::cout "Invalid input." std::endl;
;
#endif GAMEDECISIONTREEH
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
ie shared child nodes
TextBased 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.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
