-
Notifications
You must be signed in to change notification settings - Fork 114
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
base: main
Are you sure you want to change the base?
feat: risiko #295
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,107 @@ | |
M 3 vs 3 => blue win | ||
O 2 vs 1 => red win | ||
*/ | ||
|
||
/* | ||
Write a program that simulates a risk/risiko fight using 6 dices. | ||
|
||
How does it work? | ||
When a player attacks another player he uses 3 dices, the red is always the attacker and the blue is the defender. | ||
|
||
You have to compare the dice with the highest number to simulate the fight. | ||
N = first highest number | ||
M = second highest number | ||
O = third highest number | ||
|
||
If the numbers are equal, the defensor (blue) wins. | ||
|
||
Output: | ||
Red dices: | ||
6 (N) | ||
3 (M) | ||
2 (O) | ||
|
||
Blue dices: | ||
5 (N) | ||
3 (M) | ||
1 (O) | ||
|
||
R B | ||
N 6 vs 5 => red win | ||
M 3 vs 3 => blue win | ||
O 2 vs 1 => red win | ||
*/ | ||
|
||
#define EXIT_SUCCESS 0 | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
class Risiko | ||
{ | ||
private: | ||
vector<int> blue; | ||
vector<int> red; | ||
int attacker_points; | ||
|
||
public: | ||
Risiko() | ||
{ | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Come hai fatto anche per il campo attacker_points, non è necessario mettere |
||
} | ||
|
||
~Risiko() | ||
{ | ||
this->blue.clear(); | ||
this->red.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
void Attack() | ||
{ | ||
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) | ||
{ | ||
cout << "Red wins!" << endl; | ||
} | ||
else | ||
{ | ||
cout << "Blue wins!" << endl; | ||
} | ||
} | ||
}; | ||
|
||
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 commentThe 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 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
// ... |
||
game->Attack(); | ||
return EXIT_SUCCESS; | ||
} |
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