-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameMode.cpp
182 lines (153 loc) · 4.32 KB
/
GameMode.cpp
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
#include "GameMode.h"
#include <algorithm>
//#include <format>
#include <iostream>
using namespace std;
void UGameMode::StartGame()
{
Initialize();
wcout << L"------------------------------------------------\n";
wcout << L"The Game '" << Name << "' has started!\n";
Status();
Loop();
}
void UGameMode::FinishGame()
{
const char* form = AnsweredQuestions == 1 ? "!\n" : "s!\n";
wcout << L"------------------------------------------------\n";
wcout << "The game has ended\n\n";
wcout << "You answered: " << AnsweredQuestions << " question" << form;
wcout << "And earned: " << ExperiencePoints << " point" << form;
wcout << L"------------------------------------------------\n";
const char* result = HasWon() ? "YOU WON!!!\n" : "You lost...\n";
wcout << result;
wcout << L"................................................\n\n\n";
}
UGameMode::UGameMode(const wstring& name, const ItemMap& item_map)
: Name{name}
, Items{item_map}
{
dre = default_random_engine{ random_device{}()};
}
void UGameMode::Initialize()
{
// in case when game mode has more rounds than actual items
AnsweredQuestions = 0;
ExperiencePoints = 0;
Rounds = min(Rounds, Items.size());
GenerateQuestions(Rounds, ItemsPerQuestion, Items, QuestionsQueue);
}
void UGameMode::Loop()
{
while (ShouldStep())
{
Step();
}
FinishGame();
}
void UGameMode::Step()
{
const auto question = GetNextQuestion();
const auto result = AskQuestion(question);
if (result)
{
IncreaseEP(1);
++AnsweredQuestions;
}
else
{
ReduceHP(1);
ReturnQuestion(question);
}
Status();
}
void UGameMode::Status()
{
wcout << L"------------------------------------------------\n";
wcout << "Your XP: " << ExperiencePoints << " \n";
wcout << "Your HP: " << HealthPoints << " \n";
wcout << L"------------------------------------------------\n";
}
bool UGameMode::AskQuestion(QuestionP question)
{
wcout << *question.get();
wcin >> *question.get();
return question->IsValidAnswer();
}
const QuestionP UGameMode::GetNextQuestion()
{
const auto question = QuestionsQueue.front();
QuestionsQueue.pop();
return question;
}
void UGameMode::ReturnQuestion(const QuestionP question)
{
QuestionsQueue.emplace(question);
}
void UGameMode::ReduceHP(const size_t amount)
{
if (HealthPoints <= amount)
{
HealthPoints = 0;
}
else
{
HealthPoints -= amount;
}
}
void UGameMode::IncreaseEP(const size_t amount)
{
ExperiencePoints += amount;
}
ItemVector UGameMode::GetAvailableVariants(const ItemMap& items, const ItemP& item)
{
ItemVector result;
for (const auto& pair : items)
{
if (pair.first != item->Name)
{
result.emplace_back(pair.second);
}
}
return result;
}
ItemP UGameMode::GenerateAnswer(ItemVector& items, default_random_engine& in_dre)
{
shuffle(items.begin(), items.end(), in_dre);
const auto answer = items.back();
items.pop_back();
return answer;
}
ItemMap UGameMode::GenerateQuestionVariants(ItemVector& items, const ItemP& answer, const size_t itemsPerQuestion, default_random_engine& in_dre)
{
shuffle(items.begin(), items.end(), in_dre);
ItemVector temp{ items.end() - itemsPerQuestion, items.end() };
temp.emplace_back(answer);
shuffle(temp.begin(), temp.end(), in_dre);
items.erase(items.end() - itemsPerQuestion, items.end());
ItemMap result;
for (const auto& item : temp)
{
result[item->Name] = item;
}
shuffle(items.begin(), items.end(), in_dre);
return result;
}
void UGameMode::GenerateQuestions(const size_t amount, const size_t itemsPerQuestion, const ItemMap& items, queue<QuestionP>& out)
{
queue<QuestionP> output;
ItemVector leftItems;
for (const auto& item : items)
{
leftItems.emplace_back(item.second);
}
for (size_t i = 0; i < amount; ++i)
{
const auto answer = GenerateAnswer(leftItems, dre);
auto candidates = GetAvailableVariants(items, answer);
const auto variants = GenerateQuestionVariants(candidates, answer, itemsPerQuestion - 1, dre);
QuestionP result = make_shared<UQuestion>( UQuestion{answer, variants });
output.push(result);
}
swap(out, output);
}