-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeaderboard.cpp
More file actions
158 lines (133 loc) · 3.43 KB
/
Leaderboard.cpp
File metadata and controls
158 lines (133 loc) · 3.43 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
#include <iostream>
#include <vector>
#include <fstream>
#include "Leaderboard.h"
using namespace std;
Leaderboard::Leaderboard() {
name_ = "";
score_ = 0;
}
Leaderboard::Leaderboard(string name) {
name_ = name;
score_ = 0;
}
Leaderboard::Leaderboard(string name, int score) {
name_ = name;
score_ = score;
}
int Leaderboard::getScore()
{
return score_;
}
void Leaderboard::setScore(int score)
{
score_ = score;
}
string Leaderboard::getName()
{
return name_;
}
void Leaderboard::setName(string name) {
name_ = name;
}
int Leaderboard::calculateScore(int monsters_killed, int rooms_cleared, int sorcerer_anger) {
score_ = ((double) (monsters_killed + rooms_cleared) / sorcerer_anger) * 1000;
return score_;
}
void Leaderboard::writeLeaderboard(vector<Leaderboard> vec) {
ofstream fout;
fout.open("leaderboard.txt");
for (int i = 0; i < vec.size(); i++) {
fout << vec.at(i).getName() << "," << vec.at(i).getScore() << endl;
}
}
//swap for bubblesort with pass by reference
void swap(vector<Leaderboard>& vec, int startIndex, int endIndex) {
Leaderboard temp = vec.at(startIndex);
vec.at(startIndex) = vec.at(endIndex);
vec.at(endIndex) = temp;
}
//
void Leaderboard::bubbleSort(vector<Leaderboard>& final_score)
{
bool swapped = true;
do
{
swapped = false;
for(int i = 0; i < final_score.size()-1; i++)
{
//if left is bigger than right, swap, do this until there are no more swaps
if (final_score.at(i).getScore() < final_score.at(i+1).getScore())
{
swap(final_score, i, i+1);
swapped = true;
}
}
} while(swapped);
}
int splitScore(string input_string, char delimiter, string arr[], int arr_size)
{
int index = 0;
int length = input_string.length();
if(input_string == "")
{
return 0;
}
for(int i = 0; i < length; i++)
{
if(input_string[i] == delimiter)
{
index++;
// if(index +1 > arr_size)
// {
// return -1;
// }
}
else
{
arr[index] = arr[index] + input_string[i];
}
}
return index + 1;
}
//read in monster.txt
//for (count the number of lines) add to monster count
//initialize the array (num monsters)
//for (populate the monster array)
//make a new monster object for each monster listed
int Leaderboard::readLeaderboard(string file_name, vector<Leaderboard> & vec)
{
ifstream file;
string lines = "";
int num_scores = 0;
int arr_size = 0;
file.open(file_name);
//get number of lines
while (getline(file, lines))
arr_size++;
file.close();
file.open(file_name);
if(file.is_open())
{
while (!file.eof())
{
getline(file, lines);
if(lines.length() > 0)
{
string temp[arr_size];
for (int n = 0; n < arr_size; n++)
{
temp[n] = ""; //reset the temporary array
}
int split_value = splitScore(lines, ',', temp, arr_size);
Leaderboard t;
t.setName(temp[0]);
t.setScore(stoi(temp[1]));
vec.push_back(t);
num_scores++;
}
}
}
file.close();
return num_scores;
}