-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_project3.cpp
More file actions
340 lines (306 loc) · 8.75 KB
/
game_project3.cpp
File metadata and controls
340 lines (306 loc) · 8.75 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <string>
#include <vector>
#include <iostream>
#include <chrono>
#include <fstream>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
using std::vector;
class Game{
private:
string title;
string genre;
string year;
string console;
public:
Game(string _title, string _genre, string _year, string _console);
string GetTitle();
string GetGenre();
string GetYear();
string GetConsole();
};
Game::Game(string _title, string _genre, string _year, string _console){
title = _title;
genre = _genre;
year = _year;
console = _console;
}
string Game::GetTitle(){
return title;
}
string Game::GetGenre(){
return genre;
}
string Game::GetYear(){
return year;
}
string Game::GetConsole(){
return console;
}
class GameHashMap{
int capacity;
int size;
bool sep_chaining;
vector<vector<Game*> > gamehash;
vector<Game*> gamehash_probing;
public:
GameHashMap(int _capacity, bool _sep_chaining);
int Hash(string genre, string console);
void Insert(Game* game);
vector<Game*> Search(string genre, string console);
};
GameHashMap::GameHashMap(int _capacity, bool _sep_chaining){
sep_chaining = _sep_chaining;
capacity = _capacity;
if (sep_chaining){
size = 0;
for (int i = 0; i < capacity; i++){
vector<Game*> empty;
gamehash.push_back(empty);
}
}
else{
vector<Game*> empty(capacity, nullptr);
gamehash_probing = empty;
}
}
int GameHashMap::Hash(string genre, string console){
string combined_string = genre + console;
int hash = 0;
for (char ch : combined_string) {
hash = (hash * 31) + ch; // Using a prime number multiplier
}
// Ensure the hash code is non-negative and within the vector size
int positiveHash = (hash % capacity + capacity) % capacity;
return positiveHash;
}
void GameHashMap::Insert(Game* game){
int hash = Hash(game->GetGenre(), game->GetConsole());
if (sep_chaining){
// if separate chaining is being used
for (int i = 0; i < gamehash[hash].size(); i++){
if (gamehash[hash][i]->GetTitle() == game->GetTitle()){
return;
}
}
gamehash[hash].push_back(game);
}
else{
// if linear probing is being used
bool hit = false;
for (int i = hash; i < capacity; i++){
if (gamehash_probing[i] != nullptr){
if (gamehash_probing[i]->GetTitle() == game->GetTitle()){
return;
}
}
if (gamehash_probing[i] == nullptr){
gamehash_probing[i] = game;
hit = true;
break;
}
}
if (hit == false){
for (int i = 0; i < hash; i++){
if (gamehash_probing[i] == nullptr){
gamehash_probing[i] = game;
hit = true;
break;
}
}
}
}
}
vector<Game*> GameHashMap::Search(string genre, string console){
int hash = Hash(genre, console);
vector<Game*> games_rec;
if (sep_chaining){
// if separate chaining is being used
if (gamehash[hash].empty()){
return gamehash[hash];
}
else{
for (int i = 0; i < gamehash[hash].size(); i++){
if (gamehash[hash][i]->GetGenre() == genre && gamehash[hash][i]->GetConsole() == console){
games_rec.push_back(gamehash[hash][i]);
if (games_rec.size() > 5){
break;
}
}
}
}
}
else{
// if linear probing is being used
for (int i = hash; i < capacity; i++){
if (gamehash_probing[i] != nullptr){
if (gamehash_probing[i]->GetGenre() == genre && gamehash_probing[i]->GetConsole() == console){
games_rec.push_back(gamehash_probing[i]);
if (games_rec.size() > 5){
break;
}
}
}
}
if (games_rec.size() < 3){
for (int i = 0; i < hash; i++){
if (gamehash_probing[i] != nullptr){
if (gamehash_probing[i]->GetGenre() == genre && gamehash_probing[i]->GetConsole() == console){
games_rec.push_back(gamehash_probing[i]);
if (games_rec.size() > 5){
break;
}
}
}
}
}
}
return games_rec;
}
void CSVDataToGameHashMap(const std::string& filename, GameHashMap& gameMap) {
//read from csv file
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "File did not open as expected" << std::endl;
return;
}
Game* newgame;
string id, ar_id, age_rating, gid, genre1, genre2, name, p_id, console, r_id, rh_id, year, r_date, agrate;
string line;
getline(file, line);
int count = 0;
while(getline(file, line)){
std::stringstream ss(line);
getline(ss, id, ',');
getline(ss, ar_id, ',');
getline(ss, age_rating, ',');
age_rating = age_rating.substr(1, age_rating.length() - 2);
getline(ss, gid, ',');
getline(ss, genre1, ',');
genre1 = genre1.substr(1, genre1.length() - 2);
getline(ss, genre2, ',');
getline(ss, name, ',');
name = name.substr(1, name.length() - 2);
getline(ss, p_id, ',');
getline(ss, console, ',');
console = console.substr(1, console.length() - 2);
getline(ss, r_id, ',');
getline(ss, rh_id, ',');
getline(ss, year, ',');
year = year.substr(1, year.length() - 2);
getline(ss, r_date, ',');
getline(ss, agrate, ',');
if ((genre1 != "" && genre1 != "N/A") &&
(name != "" && name != "N/A") &&
(console != "" && console != "N/A") &&
(year != "" && year != "N/A")){
Game* new_game = new Game(name, genre1, year, console);
newgame = new_game;
gameMap.Insert(newgame);
count++;
}
}
file.close();
}
string genre_selection(string choice){
if (choice == "1"){
return "fighting";
}
else if (choice == "2"){
return "adventure";
}
else if (choice == "3"){
return "arcade";
}
else if (choice == "4"){
return "puzzle";
}
else if (choice == "5"){
return "simulator";
}
else if (choice == "6"){
return "sport";
}
else if (choice == "7"){
return "role-playing-rpg";
}
else if (choice == "8"){
return "strategy";
}
else if (choice == "9"){
return "shooter";
}
return "";
}
string console_selection(string choice){
if (choice == "1"){
return "Nintendo Switch";
}
else if (choice == "2"){
return "Xbox One";
}
else if (choice == "3"){
return "PC (Microsoft Windows)";
}
else if (choice == "4"){
return "PlayStation 4";
}
else if (choice == "5"){
return "PlayStation 5";
}
else if (choice == "6"){
return "Wii";
}
return "";
}
void DisplayGames(vector<Game*> results){
//show results
if (results.empty()){
cout << "No matching games! Sorry.\n";
return;
}
for (int i = 0; i < results.size(); i++){
cout << i + 1 << ". Title: " << results[i]->GetTitle() << '\n';
cout << " Released: " << results[i]->GetYear() << '\n' << '\n';
}
}
int main(){
bool choice;
string genre;
string console;
bool keepon = true;
while (keepon){
cout << "\nWelcome to the Video Game Recommender! We can help you find your next favorite game\n\n";
cout << "What collision resolution strategy would you prefer?\n\n";
cout << "Type 1 for separate chaining, type 0 for linear probing\n";
cin >> choice;
GameHashMap gamemap(1205, choice);
CSVDataToGameHashMap("igdb_data_10k.csv", gamemap);
cout << "What is your favorite type of game? Enter a number corresponding to a category\n\n";
cout << "1: Fighting 2: Adventure 3: Arcade\n";
cout << "4: Puzzle 5: Simulator 6: Sports\n";
cout << "7: RPG 8: Strategy 9: Shooter\n";
cin >> genre;
string genre_select = genre_selection(genre);
cout << "\nWhat is your favorite console to game on? Enter a number corresponding to a console\n\n";
cout << "1: Nintendo Switch 2: Xbox One 3: PC\n";
cout << "4: PlaySation 4 5: Playstation 5 6: Nintendo Wii\n";
cin >> console;
string console_select = console_selection(console);
cout << "\nHere are your results!\n\n";
vector<Game*> results = gamemap.Search(genre_select, console_select);
DisplayGames(results);
string cont_use;
cout << "Try again? Enter Y if yes, enter anything else if no \n";
cin >> cont_use;
if (cont_use == "Y" || cont_use == "y"){
continue;
}
else {
keepon = false;
}
}
return 0;
}