-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
224 lines (172 loc) · 4.87 KB
/
Copy pathmain.cpp
File metadata and controls
224 lines (172 loc) · 4.87 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// main.cpp
// testing game
//
// Created by MAQ on 29/07/25.
//
#include <iostream>
#include <fstream>
#include <sstream>
#include "Scene.hpp"
#include "Linker.hpp"
using namespace std;
// function declarations
int countScenes(const string& filename); // count no of scenes
Scene* loadScenes(const string& filename, int sceneCount); // load a scene
Scene* runScene(Scene* sceneArray, int index, bool bonus = false); // to run a and display scene
void closeFile(ifstream&); // to close the file
int main()
{
string filename = "story.txt";
bool bonus = false;
bool hasGottenBonus = false;
ifstream file(filename);
if (!file)
{
cout << "Cannot open story file."<<endl;
return 1;
}
else
{
cout << "File Open Successful!"<<endl;
}
int totalScenes = countScenes(filename);
if (totalScenes == 0)
{
cerr << "No scenes found."<<endl;
return 1;
}
Scene* story = loadScenes(filename, totalScenes);
if (!story)
{
cerr << "Failed to load story."<<endl;
return 1;
}
Linker::connectScenes(story, totalScenes); // setup the links
int currentIndex = 0;
while (!story[currentIndex].isEndingScene())
{
if(currentIndex==12)
hasGottenBonus = true;
if(currentIndex >= 12 && hasGottenBonus==true )
bonus = true;
Scene* next = runScene(story, currentIndex,bonus);
if (!next)
break;
currentIndex = next->getSceneNumber();
}
cout << endl <<"The End"<<endl;
closeFile(file);
delete[] story; // deleting for freeing space
return 0;
}
// Count @s#
int countScenes(const string& filename)
{
ifstream file(filename);
string line;
int count = 0;
while (getline(file, line))
{
if (line.rfind("@s", 0) == 0)
count++;
}
return count;
}
// Load scenes
Scene* loadScenes(const string& filename, int sceneCount)
{
ifstream file(filename);
string line;
Scene* scenes = new Scene[sceneCount];
int currentSceneIndex = -1;
bool isReadingDesc = false;
string description = "";
while (getline(file, line))
{
if (line.empty())
continue;
if (line.substr(0, 2) == "@s")
{
if (isReadingDesc && currentSceneIndex >= 0)
{
scenes[currentSceneIndex].setDescription(description);
description = "";
isReadingDesc = false;
}
currentSceneIndex++;
int num;
istringstream(line.substr(2)) >> num;
scenes[currentSceneIndex] = Scene(num);
}
else if (line.rfind("@b", 0) == 0)
{
getline(file, line); // next line is bonus
scenes[currentSceneIndex].setBonusContent(line);
}
else if (line.rfind("@p", 0) == 0)
{
string optionText = line.substr(4); // skip @p#
getline(file, line); // next line is the target scene
int targetScene = 0;
istringstream(line) >> targetScene;
scenes[currentSceneIndex].addOption(optionText, targetScene);
}
else if (line == "@0")
{
scenes[currentSceneIndex].markAsEnd(); // then isEnd becomes true if line is @0
}
else
{
isReadingDesc = true;
description += line + "\n";
}
}
if (isReadingDesc && currentSceneIndex >= 0)
{
scenes[currentSceneIndex].setDescription(description);
}
return scenes;
}
// Run a scene
Scene* runScene(Scene* sceneArray, int index,bool bonus)
{
Scene& scene = sceneArray[index];
scene.displayScene(bonus);
const vector<Scene*>& links = scene.getLinkedScenes();
if (links.empty())
return nullptr;
int choice;
cout << endl <<"Enter choice (1-" << links.size() << "): ";
cin >> choice;
if (choice < 1 || choice > static_cast<int>(links.size()) || links[choice - 1] == nullptr)
{
cout << endl <<"Invalid or broken choice. Taking default path."<<endl;
choice = 1;
}
return links[choice - 1];
}
void closeFile(ifstream& file)
{
file.close();
if(!file.is_open())
cout << "File Closed!"<<endl;
}
/*
-- notes --
Example of the engine:
@s1 This is a description of a scene in the story.
It can span
several lines.
@p1 this is the prompt for choice 1
4
@p2 this is the prompt for choice 2
3
@p3 this is the prompt for choice 3
7
Explanation:
@s1beginning of a scene and the scene number
@pfirst line is the question to prompt the user, the next line is the scene to go to.
@0 terminating scene – there are no options, only text. This is the end of the story.
For example, In the story scene above, choice 1 will jump to scene 4
*/