Skip to content

feat: risiko #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions exercises/risk-risiko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,88 @@
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 <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

void attack()
{
vector<int> blue = {0, 0, 0};
vector<int> red = {0, 0, 0};
int attacker_points = 0;

for (int i = 0; i < 3; i++)
{
blue[i] = rand() % 6 + 1;
red[i] = rand() % 6 + 1;
}
sort(blue.begin(), blue.end(), greater<int>());
sort(red.begin(), red.end(), greater<int>());
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++)
{
if (blue[i] < red[i])
{
attacker_points++;
}
}
if (attacker_points >= 2)
{
cout << "Red wins!" << endl;
}
else
{
cout << "Blue wins!" << endl;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cosa cambia tra il contenuto dell'if e dell'else? solo "Blue" o "Red", quindi potresti spostare questa "condizione" lì e fare tutto in una riga

cout << (attacker_points >= 2 ? "Red" : "Blue") << " wins!" << endl;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grazie, non pensavo si potesse usare questa sintassi anche in questo caso

}

int main(int argc, char *argv[])
{
srand(time(NULL));
attack();
return EXIT_SUCCESS;
}