forked from ChaseMooncheese/COP3530-ProgrammingQuiz4-Bridges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIGNReviews.cpp
More file actions
290 lines (251 loc) · 8.42 KB
/
Copy pathIGNReviews.cpp
File metadata and controls
290 lines (251 loc) · 8.42 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "Bridges.h"
#include "DataSource.h"
#include "data_src/Game.h"
using namespace std;
using namespace bridges;
template<typename K, typename V>
struct Pair {
K first; // key pair
V second; // value pair
};
template<typename K, typename V>
class MapfromScratch {
private:
vector<Pair<K, vector<V>>> map_data;
public:
void insert(const K& key, const V& value) {
for (auto& pair : map_data) {
if (pair.first == key) {
pair.second.push_back(value);
return;
}
}
map_data.push_back({key, {value}});
}
vector<V> get(const K& key) const {
for (const auto& pair : map_data) {
if (pair.first == key) {
return pair.second;
}
}
return {};
}
bool contains(const K& key) const {
for (const auto& pair : map_data) {
if (pair.first == key) {
return true;
}
}
return false;
}
const vector<Pair<K, vector<V>>>& getAll() const {
return map_data;
}
};
class MaxHeap {
private:
vector<pair<string, double>> heap;
int getParent(int index) { return (index - 1) / 2; }
int getLeftChild(int index) { return 2 * index + 1; }
int getRightChild(int index) { return 2 * index + 2; }
void heapifyUp(int index) {
if (index && heap[getParent(index)].second < heap[index].second) {
swap(heap[index], heap[getParent(index)]);
heapifyUp(getParent(index));
}
}
void heapifyDown(int index) {
int left = getLeftChild(index);
int right = getRightChild(index);
int largest = index;
if (left < heap.size() && heap[left].second > heap[largest].second) {
largest = left;
}
if (right < heap.size() && heap[right].second > heap[largest].second) {
largest = right;
}
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
}
}
public:
void insert(const pair<string, double>& value) {
heap.push_back(value);
heapifyUp(heap.size() - 1);
}
pair<string, double> extractMax() {
if (heap.empty()) {
throw out_of_range("Heap is empty");
}
pair<string, double> maxValue = heap[0];
heap[0] = heap.back();
heap.pop_back();
heapifyDown(0);
return maxValue;
}
bool empty() const {
return heap.empty();
}
};
void searchGamesByGenre(const MapfromScratch<string, string>& genre_map) {
string genre;
cout << "Enter the genre to search for: ";
cin.ignore();
getline(cin, genre);
vector<string> games = genre_map.get(genre);
if (!games.empty()) {
cout << "Games with genre '" << genre << "':" << endl;
for (const auto& title : games) {
cout << title << endl;
}
} else {
cout << "No games found for this genre." << endl;
}
}
void searchGamesByPlatform(const MapfromScratch<string, string>& platform_map) {
string platform;
cout << "Enter the platform to search for: ";
cin.ignore();
getline(cin, platform);
vector<string> games = platform_map.get(platform);
if (!games.empty()) {
cout << "Games for platform '" << platform << "':" << endl;
for (const auto& title : games) {
cout << title << endl;
}
} else {
cout << "No games found for this platform." << endl;
}
}
void searchGame(const vector<Game>& game_list) {
string title;
cout << "Enter the title of the game to search: ";
cin.ignore();
getline(cin, title);
for (const auto& game : game_list) {
if (game.getTitle() == title) {
cout << "Game found:" << endl;
cout << "\tTitle: " << game.getTitle() << endl
<< "\tPlatform Type: " << game.getPlatformType() << endl
<< "\tRating: " << game.getRating() << endl
<< "\tGenres: ";
for (const auto& genre : game.getGameGenre())
cout << genre << ", ";
cout << endl;
return;
}
}
cout << "Game not found." << endl;
}
void listAvailablePlatforms(const MapfromScratch<string, string>& platform_map) {
cout << "Available Platforms:" << endl;
vector<string> platforms;
for (const auto& entry : platform_map.getAll()) {
if (find(platforms.begin(), platforms.end(), entry.first) == platforms.end()) {
platforms.push_back(entry.first);
cout << entry.first << endl;
}
}
}
void displayTopGames(const vector<Game>& game_list, const string& platform, const MapfromScratch<string, string>& platform_map) {
if (!platform_map.contains(platform)) {
cout << "There is no platform named '" << platform << "'" << endl;
return;
}
MapfromScratch<string, double> title_to_rating;
// Build the map of game titles to their highest ratings on the specified platform
for (const auto& game : game_list) {
if (game.getPlatformType() == platform) {
const string& title = game.getTitle();
double rating = game.getRating();
vector<double> ratings = title_to_rating.get(title);
if (!ratings.empty()) {
double existing_rating = ratings.front(); // Assuming the first rating is the highest
if (existing_rating < rating) {
title_to_rating.insert(title, rating);
}
} else {
title_to_rating.insert(title, rating);
}
}
}
MaxHeap max_heap;
// Insert all ratings for each title into the max-heap
for (const auto& entry : title_to_rating.getAll()) {
for (const auto& rating : entry.second) {
max_heap.insert({entry.first, rating});
}
}
// Extract top 10 games in order from the heap
vector<pair<string, double>> top_games;
int count = 0;
while (!max_heap.empty() && count < 10) {
const auto& top_game = max_heap.extractMax();
top_games.push_back(top_game);
++count;
}
// Display the top 10 games
cout << "Top 10 Rated Games for platform '" << platform << "':" << endl;
for (size_t i = 0; i < top_games.size(); ++i) {
cout << i + 1 << ". " << top_games[i].first << " - Rating: " << top_games[i].second << endl;
}
}
int main(int argc, char **argv) {
Bridges bridges(0, "largebug239", "798763852353");
bridges.setTitle("How to access the IGN Game Data");
DataSource ds(&bridges);
vector<Game> game_list = ds.getGameData();
MapfromScratch<string, string> genre_map;
MapfromScratch<string, string> platform_map;
for (const auto& game : game_list) {
for (const auto& genre : game.getGameGenre()) {
genre_map.insert(genre, game.getTitle());
}
platform_map.insert(game.getPlatformType(), game.getTitle());
}
int choice;
do {
cout << "Menu:" << endl;
cout << "1. Search for a game and display its information" << endl;
cout << "2. Search for games by genre" << endl;
cout << "3. Search for games by platform" << endl;
cout << "4. List all available platforms" << endl;
cout << "5. Display top 10 games by platform" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
searchGame(game_list);
break;
case 2:
searchGamesByGenre(genre_map);
break;
case 3:
searchGamesByPlatform(platform_map);
break;
case 4:
listAvailablePlatforms(platform_map);
break;
case 5: {
string platform;
cout << "Enter the platform to display top games for: ";
cin.ignore(); // to ignore any leftover newline character
getline(cin, platform);
displayTopGames(game_list, platform, platform_map);
break;
}
case 6:
cout << "Exiting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 6);
return 0;
}