-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.cpp
242 lines (214 loc) · 5.09 KB
/
utils.cpp
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
#include "player.h"
#include "utils.h"
#include <algorithm>
/* Program utils functions */
string itos(int32_t integer)
{
string ret;
while (integer > 0){
ret.push_back((integer % 10) + 48); // ASCII -> Inteiro
integer /= 10;
}
reverse(ret.begin(), ret.end());
return ret;
}
bool fileExists(string filename)
{
ifstream fin(filename.c_str());
if (fin.is_open()){
fin.close();
return true;
}
else {
return false;
}
}
void showLogicBoard(Board b)
{
vector<vector<int> > board = b.get_board();
//vector<vector<int> > arr = { { 1,-2 },{ 3,4,-9,0 },{6,-7,8} };
/*
for_each(begin(arr), end(arr), [&](const std::vector<int>& col)
{
for_each(begin(col), end(col), [&](int x)
{
cout << setw(2) << x << " ";
});
cout << endl;
});
*/
for (uint16_t i = 0; i < board.size(); i++) {
for (uint16_t j = 0; j < board.at(0).size(); j++) {
cout << setw(2) << board.at(i).at(j) << " ";
}
cout << endl;
}
}
//cleans map
Scores::Scores()
{
scores.clear();
}
//Loads scores from file
Scores::Scores(string filename): fileScores(filename)
{
if (fileExists(fileScores)) {
loadScores(fileScores);
}
else {
scores.clear();
}
}
//Saves scores into file scores.txt
Scores::~Scores()
{
saveScores(fileScores);
}
//Add scores to map
void Scores::addScores(unsigned score, std::string name)
{
scores[score] = name;
}
//Shows map scores in ascendent order
void Scores::printScores(ostream &out) const
{
const unsigned adjust = 5;
MapIterator current = scores.begin();
MapIterator stop = scores.end();
cout << endl;
out << "\nGame scores: " << endl;
while (current != stop){
out.width(adjust);
out << right << current->first << " - " << current->second << '\n';
++current;
}
}
/*Overload << operator for class Scores*/
ostream & operator<< (ostream &out, Scores &score)
{
const unsigned adjust = 5;
map<unsigned, string>::const_iterator current = score.scores.begin();
map<unsigned, string>::const_iterator stop = score.scores.end();
out << "\nGame scores: " << endl;
while (current != stop){
out.width(adjust);
out << right << current->first << " - " << current->second << '\n';
++current;
}
return out;
}
//Save scores and player name into file
void Scores::saveScores(string fileScores)
{
ofstream fout;
MapIterator current = scores.begin();
MapIterator stop = scores.end();
fout.open(fileScores.c_str());
if (fout.fail()) {
cout << "Output file opening failed.\n";
exit(1);
}
while (current != stop) {
fout << current->first << " - " << current->second << '\n';
++current;
}
fout.close();
}
//Loads scores and player name from file
void Scores::loadScores(string fileScores)
{
ifstream fin;
unsigned score;
string name;
char dash;
scores.clear();
fin.open(fileScores.c_str());
if (fin.fail()){
cout << "Input file fail to open.\n";
cin.get();
exit(1);
}
while (!fin.eof()){
fin >> score >> dash >> name;
addScores(score, name);
}
fin.close();
}
uint8_t colorInterpreter(const string color)
{
if (color == "BLACK")
return BLACK;
else if (color == "BLUE")
return BLUE;
else if (color == "GREEN")
return GREEN;
else if (color == "CYAN")
return CYAN;
else if (color == "RED")
return RED;
else if (color == "MAGENTA")
return MAGENTA;
else if (color == "BROWN")
return BROWN;
else if (color == "LIGHTGRAY")
return LIGHTGRAY;
else if (color == "DARKGRAY")
return DARKGRAY;
else if (color == "LIGHTBLUE")
return LIGHTBLUE;
else if (color == "LIGHTGREEN")
return LIGHTGREEN;
else if (color == "LIGHTCYAN")
return LIGHTCYAN;
else if (color == "LIGHTRED")
return LIGHTRED;
else if (color == "LIGHTMAGENTA")
return LIGHTMAGENTA;
else if (color == "YELLOW")
return YELLOW;
else if (color == "WHITE")
return WHITE;
return -1;
}
void clrscr(void)
{
// upper left corner
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
DWORD dwConSize;
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hCon, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// fill with spaces
FillConsoleOutputCharacter(hCon, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hCon, &csbi);
FillConsoleOutputAttribute(hCon, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
// cursor to upper left corner
SetConsoleCursorPosition(hCon, coordScreen);
}
//==========================================================================================
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//==========================================================================================
void setcolor(unsigned int color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon, color);
}
//==========================================================================================
void setcolor(unsigned int color, unsigned int background_color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
if (background_color == BLACK) {
SetConsoleTextAttribute(hCon, color);
}
else {
SetConsoleTextAttribute(hCon, color | background_color * 16 + color);
}
}