From 13be537d1cc394b70ed5736db358053fb359edee Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 17:16:43 +0100 Subject: [PATCH 1/4] feat: git commit -m feat: I use maps to simplify use --- exercises/ex4.cpp | 95 +++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 9d93d8ac..95d3b672 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -1,56 +1,55 @@ /* Surprise me. */ -#include +#include +#include +#include -int main() -{ - int month; +const int ZERO = 48; +const int NINE = 57; +const int MAX_NUMBER = 10; +const int MINUS = 45; - /* Input month number from user */ - printf("Enter month number(1-12): "); - scanf("%d", &month); +const std::string ERROR_COLOR = "\033[31m"; +const std::string NORMAL_COLOR = "\033[0m"; - switch (month) - { - case 1: - printf("31 days"); - break; - case 2: - printf("28/29 days"); - break; - case 3: - printf("31 days"); - break; - case 4: - printf("30 days"); - break; - case 5: - printf("31 days"); - break; - case 6: - printf("30 days"); - break; - case 7: - printf("31 days"); - break; - case 8: - printf("31 days"); - break; - case 9: - printf("30 days"); - break; - case 10: - printf("31 days"); - break; - case 11: - printf("30 days"); - break; - case 12: - printf("31 days"); - break; - default: - printf("Invalid input! Please enter month number between 1-12"); - } +int insertNumber(); +bool checkStringIsNumber(std::string in); +int main(){ + int month = insertNumber(); + std::map calendary = { + {1, "31"}, + {2, "28/29"}, + {3, "31"}, + {4, "30"}, + {5, "31"}, + {6, "30"}, + {7, "31"}, + {8, "31"}, + {9, "30"}, + {10, "31"}, + {11, "30"}, + {12, "31"}, + }; + std::cout << calendary[month] << " days"; return 0; +} + +int insertNumber(){ + std::string insert; + bool isCorrect = true; + do { + if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + std::cout << "Insert Mounth: "; + getline(std::cin, insert); + } while(!(isCorrect = checkStringIsNumber(insert))); + return std::stoi(insert); +} + +bool checkStringIsNumber(std::string in){ + if(in.empty()) return false; + for(int i = 0; i < in.size(); i++) + if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false; + if(stoi(in) > MAX_NUMBER) return false; + return true; } \ No newline at end of file From 04e4e68a96ec4d6c5dac813dbfeb64f55d2db548 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 17:18:11 +0100 Subject: [PATCH 2/4] refactor: load old code --- exercises/ex4.cpp | 95 ++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 95d3b672..9d93d8ac 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -1,55 +1,56 @@ /* Surprise me. */ -#include -#include -#include +#include -const int ZERO = 48; -const int NINE = 57; -const int MAX_NUMBER = 10; -const int MINUS = 45; +int main() +{ + int month; -const std::string ERROR_COLOR = "\033[31m"; -const std::string NORMAL_COLOR = "\033[0m"; + /* Input month number from user */ + printf("Enter month number(1-12): "); + scanf("%d", &month); -int insertNumber(); -bool checkStringIsNumber(std::string in); + switch (month) + { + case 1: + printf("31 days"); + break; + case 2: + printf("28/29 days"); + break; + case 3: + printf("31 days"); + break; + case 4: + printf("30 days"); + break; + case 5: + printf("31 days"); + break; + case 6: + printf("30 days"); + break; + case 7: + printf("31 days"); + break; + case 8: + printf("31 days"); + break; + case 9: + printf("30 days"); + break; + case 10: + printf("31 days"); + break; + case 11: + printf("30 days"); + break; + case 12: + printf("31 days"); + break; + default: + printf("Invalid input! Please enter month number between 1-12"); + } -int main(){ - int month = insertNumber(); - std::map calendary = { - {1, "31"}, - {2, "28/29"}, - {3, "31"}, - {4, "30"}, - {5, "31"}, - {6, "30"}, - {7, "31"}, - {8, "31"}, - {9, "30"}, - {10, "31"}, - {11, "30"}, - {12, "31"}, - }; - std::cout << calendary[month] << " days"; return 0; -} - -int insertNumber(){ - std::string insert; - bool isCorrect = true; - do { - if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; - std::cout << "Insert Mounth: "; - getline(std::cin, insert); - } while(!(isCorrect = checkStringIsNumber(insert))); - return std::stoi(insert); -} - -bool checkStringIsNumber(std::string in){ - if(in.empty()) return false; - for(int i = 0; i < in.size(); i++) - if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false; - if(stoi(in) > MAX_NUMBER) return false; - return true; } \ No newline at end of file From 4bef88414535d588f4f2e2483cd56508dbd28afa Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 17:48:05 +0100 Subject: [PATCH 3/4] feat: implement risk risiko battle with dices --- exercises/risk-risiko.cpp | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/exercises/risk-risiko.cpp b/exercises/risk-risiko.cpp index 3166f058..28dc66f7 100644 --- a/exercises/risk-risiko.cpp +++ b/exercises/risk-risiko.cpp @@ -27,3 +27,62 @@ M 3 vs 3 => blue win O 2 vs 1 => red win */ + +#include +#include +#include +#include +#include + +using namespace std; + +const int NUM_DICES_FOR_USER = 3; +const short int RED_WIN = 1; +const short int BLUE_WIN = -1; +const unsigned short int FACES_DICES = 6; + +unsigned int * generateDicesNumber(); +void compareValue(unsigned int * dices); +void showDices(unsigned int * dices); + +int main() { + unsigned int seed = static_cast( + chrono::steady_clock::now().time_since_epoch().count() + ); + srand(seed); + + unsigned int * dices = generateDicesNumber(); + + sort(dices, dices+NUM_DICES_FOR_USER); + sort(dices+NUM_DICES_FOR_USER, dices+(NUM_DICES_FOR_USER*2)); + + showDices(dices); + compareValue(dices); + + free(dices); +} + +unsigned int * generateDicesNumber(){ + unsigned int * dices = new unsigned int; + for(int i = 0; i < NUM_DICES_FOR_USER*2; i++) dices[i] = rand() % FACES_DICES + 1; + return dices; +} + +void compareValue(unsigned int * dices) { + cout << endl << " R B"; + + for(int i = NUM_DICES_FOR_USER-1; i >= 0; i--) { + cout << endl << "N " << dices[i] << " vs " << dices[i+NUM_DICES_FOR_USER]; + if(dices[i] > dices[i+NUM_DICES_FOR_USER]) cout << " => RED WIN"; + else cout << " => BLUE WIN"; + } + cout << endl; +} + +void showDices(unsigned int * dices){ + cout << "\nBlue dices: " << endl; + for(int i = NUM_DICES_FOR_USER*2-1; i >= 0; i--) { + if(i == 2) cout << "\n\nRed dices: " << endl; + cout << dices[i] << endl; + } +} \ No newline at end of file From 14ed1a2aeb2510405de711a60657972422fac224 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 22:31:45 +0100 Subject: [PATCH 4/4] style(risk-risiko.cpp): improve readability and naming consistency --- exercises/risk-risiko.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/exercises/risk-risiko.cpp b/exercises/risk-risiko.cpp index 28dc66f7..5c58869f 100644 --- a/exercises/risk-risiko.cpp +++ b/exercises/risk-risiko.cpp @@ -37,6 +37,7 @@ using namespace std; const int NUM_DICES_FOR_USER = 3; +const int NUM_TOTAL_DICE = NUM_DICES_FOR_USER * 2; const short int RED_WIN = 1; const short int BLUE_WIN = -1; const unsigned short int FACES_DICES = 6; @@ -52,19 +53,23 @@ int main() { srand(seed); unsigned int * dices = generateDicesNumber(); - + sort(dices, dices+NUM_DICES_FOR_USER); sort(dices+NUM_DICES_FOR_USER, dices+(NUM_DICES_FOR_USER*2)); - + showDices(dices); compareValue(dices); free(dices); + return EXIT_SUCCESS; } unsigned int * generateDicesNumber(){ - unsigned int * dices = new unsigned int; - for(int i = 0; i < NUM_DICES_FOR_USER*2; i++) dices[i] = rand() % FACES_DICES + 1; + unsigned int* dices = new unsigned int[NUM_TOTAL_DICE]; + + for(int i = 0; i < NUM_TOTAL_DICE; i++) + dices[i] = rand() % FACES_DICES + 1; + return dices; } @@ -73,16 +78,22 @@ void compareValue(unsigned int * dices) { for(int i = NUM_DICES_FOR_USER-1; i >= 0; i--) { cout << endl << "N " << dices[i] << " vs " << dices[i+NUM_DICES_FOR_USER]; - if(dices[i] > dices[i+NUM_DICES_FOR_USER]) cout << " => RED WIN"; - else cout << " => BLUE WIN"; + + if(dices[i] > dices[i+NUM_DICES_FOR_USER]) + cout << " => RED WIN"; + else + cout << " => BLUE WIN"; } cout << endl; } void showDices(unsigned int * dices){ cout << "\nBlue dices: " << endl; - for(int i = NUM_DICES_FOR_USER*2-1; i >= 0; i--) { - if(i == 2) cout << "\n\nRed dices: " << endl; + + for(int i = NUM_TOTAL_DICE-1; i >= 0; i--) { + if(i == 2) + cout << "\n\nRed dices: " << endl; + cout << dices[i] << endl; } } \ No newline at end of file