-
Notifications
You must be signed in to change notification settings - Fork 117
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
base: main
Are you sure you want to change the base?
feat: risiko #295
Conversation
exercises/risk-risiko.cpp
Outdated
int main(int argc, char *argv[]) | ||
{ | ||
srand(time(NULL)); | ||
Risiko *game = new Risiko(); |
There was a problem hiding this comment.
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
// ...
exercises/risk-risiko.cpp
Outdated
vector<int> blue; | ||
vector<int> red; | ||
int attacker_points; |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
exercises/risk-risiko.cpp
Outdated
this->blue.clear(); | ||
this->red.clear(); |
There was a problem hiding this comment.
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.
exercises/risk-risiko.cpp
Outdated
this->blue = {0, 0, 0}; | ||
this->red = {0, 0, 0}; | ||
attacker_points = 0; |
There was a problem hiding this comment.
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.
exercises/risk-risiko.cpp
Outdated
if (attacker_points >= 2) | ||
{ | ||
cout << "Red wins!" << endl; | ||
} | ||
else | ||
{ | ||
cout << "Blue wins!" << endl; | ||
} |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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
No description provided.