From 71d95448783610e93969b1c90c55d200094319ae Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Wed, 18 Oct 2023 19:05:57 +0200 Subject: [PATCH 1/3] feat: risiko --- exercises/risk-risiko.cpp | 104 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/exercises/risk-risiko.cpp b/exercises/risk-risiko.cpp index 3166f05..dc9a0b9 100644 --- a/exercises/risk-risiko.cpp +++ b/exercises/risk-risiko.cpp @@ -27,3 +27,107 @@ M 3 vs 3 => blue win O 2 vs 1 => red win */ + +/* + Write a program that simulates a risk/risiko fight using 6 dices. + + How does it work? + When a player attacks another player he uses 3 dices, the red is always the attacker and the blue is the defender. + + You have to compare the dice with the highest number to simulate the fight. + N = first highest number + M = second highest number + O = third highest number + + If the numbers are equal, the defensor (blue) wins. + + Output: + Red dices: + 6 (N) + 3 (M) + 2 (O) + + Blue dices: + 5 (N) + 3 (M) + 1 (O) + + R B + N 6 vs 5 => red win + M 3 vs 3 => blue win + O 2 vs 1 => red win +*/ + +#define EXIT_SUCCESS 0 +#include +#include +#include +#include + +using namespace std; + +class Risiko +{ +private: + vector blue; + vector red; + int attacker_points; + +public: + Risiko() + { + this->blue = {0, 0, 0}; + this->red = {0, 0, 0}; + attacker_points = 0; + } + + ~Risiko() + { + this->blue.clear(); + this->red.clear(); + } + + void Attack() + { + for (int i = 0; i < 3; i++) + { + this->blue[i] = rand() % 6 + 1; + this->red[i] = rand() % 6 + 1; + } + sort(this->blue.begin(), this->blue.end(), greater()); + sort(this->red.begin(), this->red.end(), greater()); + cout << "Red dices:" << endl; + cout << this->red[0] << " (N)" << endl; + cout << this->red[1] << " (M)" << endl; + cout << this->red[2] << " (O)" << endl; + cout << endl; + cout << "Blue dices:" << endl; + cout << this->blue[0] << " (N)" << endl; + cout << this->blue[1] << " (M)" << endl; + cout << this->blue[2] << " (O)" << endl; + cout << endl; + for (int i = 0; i < 3; i++) + { + if (this->blue[i] < this->red[i]) + { + this->attacker_points++; + } + } + if (attacker_points >= 2) + { + cout << "Red wins!" << endl; + } + else + { + cout << "Blue wins!" << endl; + } + } +}; + +int main(int argc, char *argv[]) +{ + srand(time(NULL)); + Risiko *game = new Risiko(); + game->Attack(); + return EXIT_SUCCESS; +} From 189cc298aaab685420bcefcdaff7306a11c680ce Mon Sep 17 00:00:00 2001 From: GianmarcoBasile Date: Fri, 20 Oct 2023 17:29:29 +0200 Subject: [PATCH 2/3] refactor: remove the class risiko and implement attack function --- exercises/risk-risiko.cpp | 83 +++++++++++++++------------------------ 1 file changed, 32 insertions(+), 51 deletions(-) diff --git a/exercises/risk-risiko.cpp b/exercises/risk-risiko.cpp index dc9a0b9..d92b668 100644 --- a/exercises/risk-risiko.cpp +++ b/exercises/risk-risiko.cpp @@ -66,68 +66,49 @@ using namespace std; -class Risiko +void attack() { -private: - vector blue; - vector red; - int attacker_points; + vector blue = {0, 0, 0}; + vector red = {0, 0, 0}; + int attacker_points = 0; -public: - Risiko() + for (int i = 0; i < 3; i++) { - this->blue = {0, 0, 0}; - this->red = {0, 0, 0}; - attacker_points = 0; + blue[i] = rand() % 6 + 1; + red[i] = rand() % 6 + 1; } - - ~Risiko() - { - this->blue.clear(); - this->red.clear(); - } - - void Attack() + sort(blue.begin(), blue.end(), greater()); + sort(red.begin(), red.end(), greater()); + cout << "Red dices:" << endl; + cout << red[0] << " (N)" << endl; + cout << red[1] << " (M)" << endl; + cout << red[2] << " (O)" << endl; + cout << endl; + cout << "Blue dices:" << endl; + cout << blue[0] << " (N)" << endl; + cout << blue[1] << " (M)" << endl; + cout << blue[2] << " (O)" << endl; + cout << endl; + for (int i = 0; i < 3; i++) { - for (int i = 0; i < 3; i++) - { - this->blue[i] = rand() % 6 + 1; - this->red[i] = rand() % 6 + 1; - } - sort(this->blue.begin(), this->blue.end(), greater()); - sort(this->red.begin(), this->red.end(), greater()); - cout << "Red dices:" << endl; - cout << this->red[0] << " (N)" << endl; - cout << this->red[1] << " (M)" << endl; - cout << this->red[2] << " (O)" << endl; - cout << endl; - cout << "Blue dices:" << endl; - cout << this->blue[0] << " (N)" << endl; - cout << this->blue[1] << " (M)" << endl; - cout << this->blue[2] << " (O)" << endl; - cout << endl; - for (int i = 0; i < 3; i++) - { - if (this->blue[i] < this->red[i]) - { - this->attacker_points++; - } - } - if (attacker_points >= 2) + if (blue[i] < red[i]) { - cout << "Red wins!" << endl; - } - else - { - cout << "Blue wins!" << endl; + attacker_points++; } } -}; + if (attacker_points >= 2) + { + cout << "Red wins!" << endl; + } + else + { + cout << "Blue wins!" << endl; + } +} int main(int argc, char *argv[]) { srand(time(NULL)); - Risiko *game = new Risiko(); - game->Attack(); + attack(); return EXIT_SUCCESS; } From bce132f7cdbb65610218a50430ba4e2ac393fde1 Mon Sep 17 00:00:00 2001 From: Gianmarco Basile <67815194+GianmarcoBasile@users.noreply.github.com> Date: Sat, 21 Oct 2023 13:42:29 +0200 Subject: [PATCH 3/3] refactor: remove an if --- exercises/risk-risiko.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/exercises/risk-risiko.cpp b/exercises/risk-risiko.cpp index d92b668..1f317e9 100644 --- a/exercises/risk-risiko.cpp +++ b/exercises/risk-risiko.cpp @@ -96,14 +96,7 @@ void attack() attacker_points++; } } - if (attacker_points >= 2) - { - cout << "Red wins!" << endl; - } - else - { - cout << "Blue wins!" << endl; - } + cout << (attacker_points >= 2 ? "Red" : "Blue") << " wins!" << endl; } int main(int argc, char *argv[])