Browse Source

Initial commit

master
Gregory Rudolph 4 years ago
commit
bfbce31ffc
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 3
      .gitignore
  2. 106
      Game.cpp
  3. 3
      stories/first.csv

3
.gitignore vendored

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
Game
*~
#*

106
Game.cpp

@ -0,0 +1,106 @@ @@ -0,0 +1,106 @@
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct level {
int id;
string title;
string body;
int choiceCount;
vector<int> choiceIDs;
vector<string> choices;
}lvl;
// Separator for 'csv'
string separator = ",,";
// Global game_csv is the file to load
string game_csv;
// load level for int
level GotoLevel(int);
int main(int argc, char **argv) {
// Refuse to start without at least a game_csv
if (argc != 2) {
game_csv = "/ftp/pub/users/rudi/CSVGame/story.csv";
} else {
// Set global game_csv
game_csv = argv[1];
}
// Initialize nextLevel to 0 (load first level)
int nextLevel = 0;
// If user supplied a level
if (argc == 3) {
// override nextLevel with that level
nextLevel = atoi(argv[2]);
}
// Setup the level from nextLevel
level l = GotoLevel(nextLevel);
while(l.choiceIDs.size() > 0) {
// Display the level info
cout << endl << l.id << "\t" << l.title << endl << endl << l.body << endl <<endl;
// Loop through choiceID vector size and print IDs and options
for (int i = 0; i < l.choiceIDs.size(); i++) {
cout << l.choiceIDs.at(i) << ": " << l.choices.at(i) << endl;
}
do {
// accept in the next level
cout << "Goto page: ";
cin >> nextLevel;
} while (!count(l.choiceIDs.begin(), l.choiceIDs.end(), nextLevel));
l = GotoLevel(nextLevel);
}
}
level GotoLevel(int num) {
// Read file, from the global game_csv converted to a C string (const char array)
fstream file(game_csv.c_str());
// Go to the beginning of the file
file.seekg(ios::beg);
// skip to the line we want
for(int i = 0; i < num; i++) {
// ignore the crap we don't want
file.ignore(numeric_limits<streamsize>::max(),'\n');
}
// make a new level
level l;
// make a new line
string line;
// read what we have from the file, to the string line
getline(file, line);
//LevelID,Title,Body,ChoiceCount,ChoiceID1,....ChoiceIDN,Choice1,....ChoiceN
l.id = atoi(line.substr(0, line.find(separator)).c_str());
line.erase(0, line.find(separator) + separator.length());
l.title = line.substr(0, line.find(separator));
line.erase(0, line.find(separator) + separator.length());;
l.body = line.substr(0, line.find(separator));
line.erase(0, line.find(separator) + separator.length());
l.choiceCount = atoi(line.substr(0, line.find(separator)).c_str());
line.erase(0, line.find(separator) + separator.length());
for(int i = 0; i < l.choiceCount; i++) {
int choice = atoi(line.substr(0, line.find(separator)).c_str());
l.choiceIDs.push_back(choice);
line.erase(0, line.find(separator) + separator.length());
}
for(int i = 0; i < l.choiceCount; i++) {
string choice = line.substr(0, line.find(separator));
l.choices.push_back(choice);
line.erase(0, line.find(separator) + separator.length());
}
return l;
}

3
stories/first.csv

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
0,,The Last Night In Lothlem,,All things in Lothlem were old, and few things in Lothlem were built by Men, for Men were not nearly so old as those who, long ago, came from the Valley to build them. But it was presently Men that filled the city--and strange ones at that--with queer robes and cowls that covered their faces. In shadows they walked, faces downcast in the growing darkness. The streetlights had not yet been lit, and it was because of this that they strode through the city so brazenly, for they were not citizens of Lothlem--at least, not anymore.,,1,,1,,Next Page
1,,A Light in the City,,A single light flickers to life in the belly of the darkening city. You, a boy, stand at the very top of a ladder with a match in hand. At your bidding, a streetlamp sheds its warmth upon the cobblestone lane. You flick out the match and hobble down, watching your feet as you go. You sling your ladder over your shoulder, then go on to the next lamp, and then the next, and then the next after that.,,1,,2,,Next Page
2,,Strange Happenings In Lothlem,,You are the first to see the strange figures at the edge of the growing darkness. Cloaked figures shuffle at the edge of your lamplight on the corner of every lane, dressed in neither the gilded robes of the magister's men, nor the black feathered caps of the watchmen. You recognize none of them.,,2,,3,,4,,Follow the strange figures,,Continue with your work
Loading…
Cancel
Save