-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
72 lines (62 loc) · 1.87 KB
/
game.cpp
File metadata and controls
72 lines (62 loc) · 1.87 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
#include <iostream>
#include <ctime>
#include <fstream>
#include <map>
using std::cout;
using std::cin;
using std::string;
int getRandomNumber(int maxValue) {
std::srand(std::time(nullptr));
return std::rand() % maxValue;
}
int startGame(int hiddenNumber) {
int gamerNumber;
int attempt = 1;
while (true) {
cout << "Enter your guess:";
cin >> gamerNumber;
if (gamerNumber > hiddenNumber) {
cout << "Less than " << gamerNumber << std::endl;
} else if (gamerNumber < hiddenNumber) {
cout << "Greater than " << gamerNumber << std::endl;
} else {
cout << "You win! Attempts = " << attempt << std::endl;
break;
}
attempt++;
}
return attempt;
}
std::map<string, int> readScoreTable(string &fileName) {
string username;
string line;
int score;
std::ifstream scoreFile;
scoreFile.open(fileName);
std::map<string, int> arrScore;
while (getline(scoreFile, line)) {
size_t pos = line.find("\t");
score = std::stoi(line.substr(pos));
username = line.substr(0, pos);
if (arrScore[username] == 0 || score < arrScore[username]) {
arrScore[username] = score;
}
}
scoreFile.close();
return arrScore;
}
void showScoreTable(std::map<string, int> &arrScore) {
std::map<string, int>::iterator arr = arrScore.begin();
for (int i = 0; arr != arrScore.end(); arr++, i++) {
cout << arr->first << "\t" << arr->second << std::endl;
}
}
void writeSoreTable(string &fileName, std::map<string, int> &arrScore) {
std::ofstream scoreFile;
scoreFile.open(fileName);
std::map<string, int>::iterator arr = arrScore.begin();
for (int i = 0; arr != arrScore.end(); arr++, i++) {
scoreFile << arr->first << "\t" << arr->second << std::endl;
}
scoreFile.close();
}