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 1 commit
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
83 changes: 32 additions & 51 deletions exercises/risk-risiko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,68 +66,49 @@

using namespace std;

class Risiko
void attack()
{
private:
vector<int> blue;
vector<int> red;
int attacker_points;
vector<int> blue = {0, 0, 0};
vector<int> 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<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++)
{
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<int>());
sort(this->red.begin(), this->red.end(), greater<int>());
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;
}
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));
Risiko *game = new Risiko();
game->Attack();
attack();
return EXIT_SUCCESS;
}