Skip to content
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

feat: Implementation Risiko #300

Open
wants to merge 1 commit 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
55 changes: 55 additions & 0 deletions exercises/risk-risiko.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,58 @@
M 3 vs 3 => blue win
O 2 vs 1 => red win
*/

#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;


int attacker_Dices[3];
int defender_Dices[3];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Evita di usare variabili globali perchè sono "rischiose" e ci si può accedere da qualsiasi punto del codice. Piuttosto potresti creare questi array nel main e passarli alla funzione compare, tipo:

// Definisco la costante della dimensione degli array
#define DIM 3
void compare (int* attacker_Dices, int* defender_Dices){
    //...
}

//...

int main(){
    int* attacker_Dices = new int[DIM];
    int* defender_Dices = new int[DIM];
    //...
    compare(attacker_Dices, defender_Dices);
}

E ovviamente si dovrebbe adattare il codice con la costante definita DIM che incrementa la leggibilità del codice.


void compare(){
sort(attacker_Dices, attacker_Dices+ 3, greater<int>());
sort(defender_Dices, defender_Dices + 3, greater<int>());
for(int i=0; i<3; i++){
if(defender_Dices[i]>=attacker_Dices[i])
{
cout<<"N"<<attacker_Dices[i]<<" vs "<<defender_Dices[i]<<
" ==> Blue Win"<<endl;
}
else
{
cout<<"N"<<attacker_Dices[i]<<" vs "<<defender_Dices[i]<<
" ==> Red Win"<<endl;
}
}

}

int main(){
srand(rand()%556745+1);


cout<<"Red Dices:\n";
for(int i=0; i<3; i++){
attacker_Dices[i]= rand() % 5+1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Potresti mettere attacker_Dices[i] = rand() % 6; piuttosto che lasciare esplicitamente 5+1

cout<<attacker_Dices[i]<<endl;


}


cout<<"Blue Dices:\n";
for(int i=0; i<3; i++){
Copy link
Collaborator

Choose a reason for hiding this comment

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

Potresti eliminare questo for e utilizzare il precedente per fare le operazioni sul secondo array visto che esse sono indipendenti fra loro, quindi evitare una serie di cicli.

defender_Dices[i]= rand() % 5+1;
cout<<defender_Dices[i]<<endl;

}

compare();





Copy link
Collaborator

Choose a reason for hiding this comment

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

Consiglio di mettere l'istruzione di return del main e di rimuovere eventuali spazi bianchi (o ritorni a capo) che allungano il codice, quindi migliorare l'indentazione per migliorare la leggibilità del codice.

}