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: risiko #295

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

GianmarcoBasile
Copy link

No description provided.

int main(int argc, char *argv[])
{
srand(time(NULL));
Risiko *game = new Risiko();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Se decidi di utilizzare l'allocazione dinamica della memoria, ricordati effettuare una delete quando hai finito di utilizzare l'oggetto. Altrimenti, evita direttamente di utilizzare new.

Risiko *game = new Risiko();
game->Attack();
// ...
delete game;
Risiko game;
game.Attack();
// ...

Una maniera più avanzata per ottenere lo stesso risultato è utilizzare uno degli smart pointer offerti da c++:

#include <memory>
// ...
std::unique_ptr<Risiko> game{std::make_unique<Risiko>()};
game->Attack();
// Non c'è bisogno di chiamare delete: ci pensa il pointer appena esce fuori dallo scope
// ...

Comment on lines 72 to 74
vector<int> blue;
vector<int> red;
int attacker_points;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per quanto apprezzi l'utilizzo della OOP (Object Oriented Programming), temo che in questo caso sia un po' un'overkill: tutte le proprietà private della classe potrebbero tranquillamente essere spostate dentro il metodo Attack.

Copy link
Author

Choose a reason for hiding this comment

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

Nell'ultimo commit ho tolto la classe e utilizzo una semplice funzione

Comment on lines 86 to 87
this->blue.clear();
this->red.clear();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Non è necessario fare il clear dei vettori nel distruttore. Saranno comunque deallocati non appena l'oggetto viene distrutto.

Comment on lines 79 to 81
this->blue = {0, 0, 0};
this->red = {0, 0, 0};
attacker_points = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Come hai fatto anche per il campo attacker_points, non è necessario mettere this-> per indicare un membro della classe all'interno della stessa.
O almeno, scegli una metodologia e sii consistente.

Comment on lines 99 to 106
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants