Skip to content

Commit 14ed1a2

Browse files
committed
style(risk-risiko.cpp): improve readability and naming consistency
1 parent 4bef884 commit 14ed1a2

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

exercises/risk-risiko.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
using namespace std;
3838

3939
const int NUM_DICES_FOR_USER = 3;
40+
const int NUM_TOTAL_DICE = NUM_DICES_FOR_USER * 2;
4041
const short int RED_WIN = 1;
4142
const short int BLUE_WIN = -1;
4243
const unsigned short int FACES_DICES = 6;
@@ -52,19 +53,23 @@ int main() {
5253
srand(seed);
5354

5455
unsigned int * dices = generateDicesNumber();
55-
56+
5657
sort(dices, dices+NUM_DICES_FOR_USER);
5758
sort(dices+NUM_DICES_FOR_USER, dices+(NUM_DICES_FOR_USER*2));
58-
59+
5960
showDices(dices);
6061
compareValue(dices);
6162

6263
free(dices);
64+
return EXIT_SUCCESS;
6365
}
6466

6567
unsigned int * generateDicesNumber(){
66-
unsigned int * dices = new unsigned int;
67-
for(int i = 0; i < NUM_DICES_FOR_USER*2; i++) dices[i] = rand() % FACES_DICES + 1;
68+
unsigned int* dices = new unsigned int[NUM_TOTAL_DICE];
69+
70+
for(int i = 0; i < NUM_TOTAL_DICE; i++)
71+
dices[i] = rand() % FACES_DICES + 1;
72+
6873
return dices;
6974
}
7075

@@ -73,16 +78,22 @@ void compareValue(unsigned int * dices) {
7378

7479
for(int i = NUM_DICES_FOR_USER-1; i >= 0; i--) {
7580
cout << endl << "N " << dices[i] << " vs " << dices[i+NUM_DICES_FOR_USER];
76-
if(dices[i] > dices[i+NUM_DICES_FOR_USER]) cout << " => RED WIN";
77-
else cout << " => BLUE WIN";
81+
82+
if(dices[i] > dices[i+NUM_DICES_FOR_USER])
83+
cout << " => RED WIN";
84+
else
85+
cout << " => BLUE WIN";
7886
}
7987
cout << endl;
8088
}
8189

8290
void showDices(unsigned int * dices){
8391
cout << "\nBlue dices: " << endl;
84-
for(int i = NUM_DICES_FOR_USER*2-1; i >= 0; i--) {
85-
if(i == 2) cout << "\n\nRed dices: " << endl;
92+
93+
for(int i = NUM_TOTAL_DICE-1; i >= 0; i--) {
94+
if(i == 2)
95+
cout << "\n\nRed dices: " << endl;
96+
8697
cout << dices[i] << endl;
8798
}
8899
}

0 commit comments

Comments
 (0)