-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
358 lines (333 loc) · 7.59 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#pragma once
#include "utils.h"
std::ostream& operator<<(std::ostream& out, const Card& c) {
return out << c.rank << c.suits;
}
std::ostream& operator<<(std::ostream& out, const vector<Card>& v) {
for (size_t i = 0; i < v.size(); i++) {
out << v.at(i) << " ; ";
}
return out << "\n";
}
vector<Card> createDeck() {
vector <string> suits = {"Heart" ,"Diamond" ,"Club" ,"Spade"};
string ranks = "A234567890JQK";
Card newCard;
vector <Card> deck;
for (unsigned int i = 0; i < ranks.length(); i++)
{
for (size_t j = 0; j < suits.size(); j++)
{
if (i == 0)
{
newCard.suits = suits.at(j);
newCard.rank = ranks.at(i);
newCard.score = 11;
}
else if (i < 9)
{
newCard.suits = suits.at(j);
newCard.rank = ranks.at(i);
newCard.score = i + 1;
}
else if (i == 9)
{
newCard.suits = suits.at(j);
newCard.rank = "10";
newCard.score = i + 1;
}
else if (i >= 1)
{
newCard.suits = suits.at(j);
newCard.rank = ranks.at(i);
newCard.score = 10;
}
deck.push_back(newCard);
}
}
return deck;
}
string getHumanPlay()
{
string option;
getline(cin, option);
while (option != "hit" && option != "stand" && option != "HIT" && option != "STAND" && option != "Hit" && option != "Stand" && option != "EXIT" && option != "Exit" && option != "exit") {
cout << "Please insert a valid entry: ";
getline(cin, option);
}
if (option == "HIT" || option == "Hit") {
option = "hit";
}
else if (option == "Stand" || option == "STAND") {
option = "stand";
}
else if (option == "Exit" || option == "EXIT")
{
option = "exit";
}
return option;
}
unsigned short int readUnsignedIntBetween(unsigned int minValue, unsigned int maxValue){
unsigned int short newInt;
cout << "Insert Value ( " << minValue << " - " << maxValue << " ) : ";
while (!(cin >> newInt) || newInt > maxValue || newInt < minValue) {
cout << endl;
cin.clear();
cin.ignore();
cout << "Invalid Value: Insert a new one: ";
}
cin.ignore();
cout << endl;
return newInt;
}
int readIntBetween(int min, int max){
int newInt;
cout << "Insert a value: ";
while(!(cin >> newInt) || newInt > max || newInt < min) {
cout << endl;
cin.clear();
cin.ignore();
cout << "Wrong value, value between " << min << " - " << max;
}
cin.ignore();
cout << endl;
return newInt;
}
unsigned int readUnsignedInt() {
unsigned int newUnsignedInt;
cout << "Insira um valor: ";
while (!(cin >> newUnsignedInt)) {
cout << endl;
cin.clear();
cin.ignore();
cout << "Valor inválido, insira um novo valor unsigned int: ";
}
cin.ignore();
cout << endl;
return newUnsignedInt;
}
int readInt() {
int newInt;
cout << "Insira um valor: ";
while (!(cin >> newInt)) {
cout << endl;
cin.clear();
cin.ignore();
cout << "Valor inválido, insira um novo valor unsigned int: ";
}
cin.ignore();
cout << endl;
return newInt;
}
int readBinary(){
int value;
cin >> value;
while (value != 0 || value != 1){
cout << endl;
cin.clear();
cin.ignore();
cout << "Valor inválido, insira 0 ou 1: ";
}
cin.ignore();
cout << endl;
return value;
}
float readFloat() {
float newFloat;
cout << "Insira um novo valor: ";
while (!(cin >> newFloat)) {
cout << endl;
cin.clear();
cin.ignore();
cout << "Valor invalido, insira um novo valor: ";
}
cin.ignore();
cout << endl;
return newFloat;
}
char readCharYorN() {
char newChar;
cout << "Insira o caracter: ";
while (!(cin >> newChar) || (newChar != 'n' && newChar != 'N' && newChar != 'Y' && newChar != 'y')) {
cout << endl;
cin.clear();
cin.ignore();
cout << "Caracter invalido, insira um novo caracter: ";
}
cin.ignore();
cout << endl;
return newChar;
}
//Paralel function
void Users(vector <int> &usersVEC, int &user) {
string line;
ifstream UserFileI("users_temp.txt");
int find = 0;
if (UserFileI.is_open())
{
usersVEC.clear();
while (getline(UserFileI, line)) //enquanto houver linhas no ficheiro
{
usersVEC.push_back(stoi(line, nullptr, 10));
}
UserFileI.close();
for (unsigned int i = 0; i < usersVEC.size(); i++)
{
if (user == usersVEC.at(i))
{
find = 1;
}
}
if (find == 0)
{
if (usersVEC.size() != 0)
{
usersVEC.push_back(usersVEC.back() + 1);
user = usersVEC.back();
}
else
{
usersVEC.push_back(1);
user = usersVEC.back();
}
}
}
else
{
usersVEC.push_back(1);
user = usersVEC.back();
}
ofstream UserFileO("users_temp.txt");
if (UserFileO.is_open())
{
for (unsigned int i = 0; i < usersVEC.size(); i++)
{
UserFileO << usersVEC.at(i) << endl;
}
UserFileO.close();
}
}
void FileCopy(string filetxt, string filetxt_temp) {
ofstream File(filetxt);
ifstream File_temp(filetxt_temp);
string line;
if (File.is_open() && File_temp.is_open())
{
while (getline(File_temp, line))
{
File << line << endl;
}
File.close();
File_temp.close();
}
}
bool FileExist(string filetxt_temp) {
ifstream file(filetxt_temp);
if (file.is_open())
{
file.close();
return true;
}
else
{
return false;
}
}
int BinaryInt(int id, vector <int> VEC) {
int lowindex = 0;
int highindex = VEC.size() - 1;
int i = -1;
while (lowindex <= highindex)
{
i = (highindex + lowindex) / 2;
if (id == VEC.at(i))
{
return i;
}
else if (id < VEC.at(i))
{
highindex = i - 1;
}
else if (id > VEC.at(i))
{
lowindex = i + 1;
}
}
return -1;
}
int saveChanges(vector <int> &usersVEC, int &user, pair <int, int> xy, int &save) {
char decision;
Users(usersVEC, user);
if (usersVEC.size() == 1)
{
system("cls");
cout << setw((xy.first - 50) / 2) << (char)201;
for (int i = 0; i < 50; i++)
{
cout << (char)205;
}
cout << (char)187 << endl;
cout << setw((xy.first - 50) / 2) << (char)186 << setw(51) << (char)186 << endl;
string text = "Do you want save all changes?";
cout << setw((xy.first - 50) / 2) << (char)186 << setw((50 + text.length()) / 2) << text << setw(51 - (50 + text.length()) / 2) << (char)186 << endl;
cout << setw((xy.first - 50) / 2) << (char)186 << setw(51) << (char)186 << endl;
cout << setw((xy.first - 50) / 2) << (char)200;
for (int i = 0; i < 50; i++)
{
cout << (char)205;
}
cout << (char)188 << endl;
cout << endl << "Yes 'Y' or No 'N': ";
cin >> decision;
while (!(decision == 'Y' || decision == 'y' || decision == 'N' || decision == 'n'))
{
cout << endl << "Yes 'Y' or No 'N': ";
cin >> decision;
}
//alteracao
if (decision == 'Y' || decision == 'y')
{
remove("users_temp.txt");
save = 1;
}
else if (decision == 'N' || decision == 'n')
{
remove("users_temp.txt");//delete file temp
save = 0;
}
}
else
{
//elimina usuario no vetor
usersVEC.erase(usersVEC.begin() + BinaryInt(user, usersVEC));
ofstream UserFileO("users_temp.txt");
if (UserFileO.is_open())
{
for (unsigned int i = 0; i < usersVEC.size(); i++)
{
UserFileO << usersVEC.at(i) << endl;
}
UserFileO.close();
}
}
return 0;
}
int readNameOfFile(string & fileName) {
cout << "Name of File?" << endl;
cin >> fileName;
while (fileName.find(".txt") != fileName.length() - 4)
{
cout << "Write name of file again" << endl;
cin >> fileName;
}
return 0;
}
void waitXTime(unsigned int time) {
clock_t t1 = clock();
float seconds_past = 0.0;
while (seconds_past < time)
{
seconds_past = (clock() - t1) / CLOCKS_PER_SEC;
}
return;
}