Skip to content

Implementazione risiko #303

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 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
83 changes: 83 additions & 0 deletions exercises/risk-risiko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,86 @@
M 3 vs 3 => blue win
O 2 vs 1 => red win
*/
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;



void compare()
{
int dim=3;
int attack_dice[dim];
int defend_dice[dim];
srand(static_cast<unsigned int>(time(nullptr)));

cout<<"Dadi di attacco:"<<endl;

for(int i=0;i<dim;i++)
{
attack_dice[i] = rand() % 6+1; // Genera numeri casuali tra 1 e 6.
cout <<attack_dice[i]<< endl;

}

cout<<"Dadi di difesa:"<<endl;

for(int i=0;i<dim;i++)
{
defend_dice[i]=rand()%6+1;
cout<< defend_dice[i]<<endl;
}

sort(attack_dice, attack_dice + dim, greater<int>());
sort(defend_dice, defend_dice + dim, greater<int>());
cout<<endl;
for(int i=0;i<dim;i++){
if(attack_dice[i]<=defend_dice[i])
{
cout<<attack_dice[i]<<"vs"<<defend_dice[i]<<" Win Blue " <<endl;
cout<<endl;
}
else
{
cout<<attack_dice[i]<<"vs"<<defend_dice[i]<<" Win Red "<<endl;
cout<<endl;

}

}

}
Copy link
Member

Choose a reason for hiding this comment

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

questa funzione è un po' grande potresti provare a spezzarla in più funzioni


void gioca_ancora()
{
bool gioca_ancora=true;
int x;

while(gioca_ancora==true)
{
cout<<"Vuoi giocare ancora? (se si premi 1 se no premi 0)"<<endl;
cin>>x;
if(x==1)
{
compare();
}
else if(x==0)
{
gioca_ancora=false;
Copy link
Member

Choose a reason for hiding this comment

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

puoi usare "break" in questo caso e non dover utiizzare la variabile gioca_ancora

while (true) {
....
  else if (x == 0) {
    cout << "Esci!" << endl;
    break;
  }
}

cout<<"Esci!"<<endl;

}
}

}

int main()
{
compare();
gioca_ancora();

return 0;
}