Continuation, the previous post was too long:
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//The nodes structure for the binary tree. A left node is another option, a right node is the scene that option could lead to
/*
O0
|
O1 - S1O0 - S2O0
| | |
O2 S1O1 S2O1 - S4O0
| | |
O3 S1O2 - S3O0 S4O1
|
S3O1
|
S3O2
*/
struct sceneNode {
char output[256];
struct sceneNode *nextScene;
struct sceneNode *nextOption;
};
//insertOption and insertScene do the exact same thing and can be merged. The only exist as a duplicate for ease of code reading
//What they do is if the current node that you are passing is null (non existant, not allocated) it will allocate memory and set it up.
//This might be where you would want to also pass your images and music buffers so they can be added to the structure.
void insertOption(char *output, struct sceneNode **nodeOption) {
if(*nodeOption == NULL) {
*nodeOption = (struct sceneNode*)malloc(sizeof(struct sceneNode));
strncpy((*nodeOption)->output, output, 256);
(*nodeOption)->nextScene = NULL;
(*nodeOption)->nextOption = NULL;
}
}
void insertScene(char *output, struct sceneNode **nodeScene) {
if(*nodeScene == NULL) {
*nodeScene = (struct sceneNode*)malloc(sizeof(struct sceneNode));
strncpy((*nodeScene)->output, output, 256);
(*nodeScene)->nextScene = NULL;
(*nodeScene)->nextOption = NULL;
}
}
//This function recursivly iterates the left nodes (options) printing their output text
void printOptions(struct sceneNode *scene) {
if(scene != NULL) {
printf("%s", scene->output);
printOptions(scene->nextOption);
}
}
//This function changes to a specific scene N options away from the route recursivly
//For example if you are at O0 and want to go to the scene pointed to at O1, it would travel 1 left node, then 1 right node and return that node.
struct sceneNode *changeScene(int option, struct sceneNode *scene) {
if(scene != NULL) {
if(option == 0)
return scene->nextScene;
scene = scene->nextOption;
option -= 1;
return changeScene(option, scene);
}
return NULL;
}
int main() {
//Initialize the root of the scene
//You can use O0 as a pretext for the scene, or as an option itself
struct sceneNode *rootScene = malloc(sizeof(struct sceneNode));
strncpy(rootScene->output, "You wake up from a dream only to realize you are late for school!\n\n", 256);
rootScene->nextScene = NULL; printf("\n-------------------------------------------------------\n");
rootScene->nextOption = NULL;
//Setup a character buffer so we can dynamically create strings via snprintf if we need to, we can simply use char * strings if we wanted
char output[256] = {0};
//Create some placeholders, there is bound to be a better way I was tired
struct sceneNode *currentOption = rootScene;
struct sceneNode *currentScene = rootScene;
struct sceneNode *temp = NULL;
//The next bulk of code down to where it starts printing options should be in its own method
//The method should read a file for inputs/scenes, probably tab delimited or something
//All the options will be on the same level, then a new scene and its options would be indented once, and the next twice and so forth.
snprintf(output, 256, "%s\n", "1) Ugh, I should just fall back asleep.\n");
insertOption(output, ¤tOption->nextOption);
temp = currentOption;
snprintf(output, 256, "%s\n", "You awake 4 hours later.");
insertScene(output, ¤tOption->nextScene);
currentOption = currentOption->nextScene;
snprintf(output, 256, "%s\n", "1) I wonder who is online on animebytes...\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
snprintf(output, 256, "%s\n", "2) I should probably check on my guild on my mmo...\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
snprintf(output, 256, "%s\n", "3) I wonder what is in the fridge for lunch.\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
currentOption = temp->nextOption;
snprintf(output, 256, "%s\n", "2) Looks like I won't be having breakfast this morning... again.\n");
insertOption(output, ¤tOption->nextOption);
temp = currentOption;
snprintf(output, 256, "%s\n", "You arrive at school on time in a frantic rush.");
insertScene(output, ¤tOption->nextScene);
currentOption = currentOption->nextScene;
snprintf(output, 256, "%s\n", "1) I should go check the message board for updates on my school clubs.\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
snprintf(output, 256, "%s\n", "2) If I don't hurry up I will be late for first period.\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
currentOption = temp->nextOption;
snprintf(output, 256, "%s\n", "3) I wonder if binary-tree-senpai will notice me today?\n");
insertOption(output, ¤tOption->nextOption);
temp = currentOption;
snprintf(output, 256, "%s\n", "You leave the bathroom grin with a blushed grin on your face.");
insertScene(output, ¤tOption->nextScene);
currentOption = currentOption->nextScene;
snprintf(output, 256, "%s\n", "1) I feel so guilty, but that felt too good.\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
snprintf(output, 256, "%s\n", "2) If binary-tree-senpai found out I could never face her again.\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
snprintf(output, 256, "%s\n", "3) If I go to school now I will be late, but at least binary-tree-senpai will be there!\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
snprintf(output, 256, "%s\n", "4) ...\n");
insertOption(output, ¤tOption->nextOption);
currentOption = currentOption->nextOption;
//Start at the beginning of our tree
currentScene = rootScene;
//Print our options and then look for user input
printOptions(currentScene);
printf("\n-------------------------------------------------------\n");
scanf("%d", &i);
//Change to that user inputted next scene and print the options
currentScene = changeScene(i-1, currentScene);
printOptions(currentScene);
printf("\n-------------------------------------------------------\n");
return 0;
}