-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiddleDatabase.cpp
More file actions
84 lines (61 loc) · 2.12 KB
/
Copy pathRiddleDatabase.cpp
File metadata and controls
84 lines (61 loc) · 2.12 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
////////////////////////////////////// INCLUDES & FORWARDS /////////////////////////////////////////////
#include "RiddleDatabase.h"
#include <fstream>
////////////////////////////////////////// Static Initialization /////////////////////////////////////////////
std::vector<RiddleData> RiddleDatabase::riddles;
bool RiddleDatabase::isActive = false;
////////////////////////////////////////// initialize /////////////////////////////////////////////
void RiddleDatabase::initialize()
{
if (isActive) return;
std::ifstream file("riddle.txt");
if (!file.is_open())
{
isActive = true;
return;
}
std::string line;
int riddleId = 0;
while (std::getline(file, line))
{
if (line.find("---RIDDLE---") != std::string::npos)
{
std::string question;
if (!std::getline(file, question)) break;
std::string opts[4];
for (int i = 0; i < 4; i++) if (!std::getline(file, opts[i])) break;
std::string answerLine;
if (!std::getline(file, answerLine)) break;
int correctAnswer = std::stoi(answerLine) - 1;
riddles.push_back(RiddleData(riddleId, question, opts, correctAnswer));
riddleId++;
}
}
file.close();
isActive = true;
}
////////////////////////////////////////// getRiddle /////////////////////////////////////////////
const RiddleData *RiddleDatabase::getRiddle(int riddleId)
{
initialize();
for (const RiddleData &r : riddles) if (r.riddleId == riddleId) return &r;
return nullptr;
}
////////////////////////////////////////// getTotalRiddles /////////////////////////////////////////////
int RiddleDatabase::getTotalRiddles()
{
initialize();
return riddles.size();
}
////////////////////////////////////////// addRiddle /////////////////////////////////////////////
void RiddleDatabase::addRiddle(const RiddleData &riddle)
{
riddles.push_back(riddle);
isActive = true;
}
////////////////////////////////////////// clearRiddles /////////////////////////////////////////////
void RiddleDatabase::clearRiddles()
{
riddles.clear();
isActive = false;
}