-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstructions_intro_leaderboard.cpp
More file actions
121 lines (109 loc) · 3.42 KB
/
instructions_intro_leaderboard.cpp
File metadata and controls
121 lines (109 loc) · 3.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
#include "instructions_intro_leaderboard.h"
/*
The goal of this function is to take an integer input 1 or 2,
if the input is 1 the function displays the introduction message
else it displays the game rules/instructions.
The introduction message is displayed from the file "intro.txt" which is located in the "themes" folder.
The rules/instructions are displayed from the file "instructions.txt" which is located in the "themes" folder.
Function Made By:
Muhammad Shahab Hasan (3035961992)
*/
void instructions_intro(int input){
if (input == 1){
string line;
ifstream file("themes/intro.txt");
if (file.is_open()){
while (getline(file, line)){
cout << line << endl;
}
file.close();
} else {
cout << "Unable to open file" << endl;
}
} else if (input == 2){
string line;
ifstream file("themes/instructions.txt");
if (file.is_open()){
while (getline(file, line)){
cout << line << endl;
}
file.close();
} else {
cout << "Unable to open file" << endl;
}
} else if (input == 2){
string line;
ifstream file("themes/instructions.txt");
if (file.is_open()){
while (getline(file, line)){
cout << line << endl;
}
file.close();
} else {
cout << "Unable to open file" << endl;
}
} else {
cout << "Invalid input" << endl;
}
}
/*
The goal of this function is to output the leaderboard of the game.,
The leaderboard is displayed from the files in the "porfiles" folder.
We also use a custom struct to store the user data, allowing easy manuplation.
The leaderboard is sorted based on the number of puzzles solved.
Function Made By:
Muhammad Shahab Hasan (3035961992) & Dongwoo Kang (3036029777)
*/
struct UserData
{
string name;
int puzzlesSolved;
int hintsPoints;
int hintsUsed;
};
bool compareUserData(const UserData &a, const UserData &b)
{
return a.puzzlesSolved > b.puzzlesSolved;
}
void displayLeaderboard()
{
string folderName = "profiles/";
vector<UserData> users;
// Open directory
DIR *dir;
struct dirent *entry;
if ((dir = opendir(folderName.c_str())) != NULL)
{
// Iterate over files in directory
while ((entry = readdir(dir)) != NULL)
{
// Check if file is a .txt file
if (entry->d_type == DT_REG && string(entry->d_name).find(".txt") != string::npos)
{
// Open file and read user data
string fileName = folderName + entry->d_name;
ifstream file(fileName);
UserData user;
getline(file, user.name);
file >> user.puzzlesSolved >> user.hintsPoints >> user.hintsUsed;
users.push_back(user);
file.close();
}
}
closedir(dir);
}
else
{
// Error opening directory
cout << "Error: Could not open directory " << folderName << endl;
return;
}
// Sort users based on puzzles solved
sort(users.begin(), users.end(), compareUserData);
// Display leaderboard
cout << "Leaderboard:" << endl;
for (int i = 0; i < users.size(); i++)
{
cout << "#" << i + 1 << " " << users[i].name << " \"" << users[i].puzzlesSolved << "\"" << endl;
}
}