#include <iostream>
#include <memory>
#include <cstdlib>
#include <ctime>
using namespace std;
class Dice {
private:
int sides_;
public:
Dice(int sides);
~Dice();
int roll() const;
};
class Player {
private:
shared_ptr<Dice> dice_;
public:
Player();
~Player();
Player& takeDice(shared_ptr<Dice>& dice);
Player& rollDice();
Player& putDice(shared_ptr<Dice>& place);
};
Dice::Dice(int sides) : sides_(sides) { cout << "Кубик " << this << " создан" << endl; }
Dice::~Dice() { cout << "Кубик " << this << " удален" << endl; }
int Dice::roll() const { return rand() % sides_ + 1; }
Player::Player() { cout << "Игрок " << this << " создан" << endl; }
Player::~Player() { cout << "Игрок " << this << " удален" << endl; }
Player& Player::takeDice(shared_ptr<Dice>& dice) {
if (dice) {
dice_ = dice;
dice.reset();
cout << "Игрок взял кубик" << endl;
}
return *this;
}
Player& Player::rollDice() {
if (dice_) {
cout << "Игрок бросил кубик. Выпало: " << dice_->roll() << endl;
}
return *this;
}
Player& Player::putDice(shared_ptr<Dice>& place) {
if (dice_) {
place = dice_;
dice_.reset();
cout << "Игрок положил кубик обратно" << endl;
}
return *this;
}
int main() {
srand(static_cast<unsigned int>(time(0)));
shared_ptr<Dice> dice1{ make_shared<Dice>(6) };
shared_ptr<Dice> dice2{ make_shared<Dice>(12) };
shared_ptr<Dice> dice3{ make_shared<Dice>(20) };
Player player{};
cout << "\nБроски кубиков на столе:" << endl;
cout << "D6: " << dice1->roll() << endl;
cout << "D12: " << dice2->roll() << endl;
cout << "D20: " << dice3->roll() << endl;
cout << "\nИгрок берет первый кубик" << endl;
player.takeDice(dice1);
player.rollDice();
player.putDice(dice1);
cout << "\nИгрок берет второй кубик" << endl;
player.takeDice(dice2);
player.rollDice();
player.putDice(dice2);
cout << "\nИгрок берет третий кубик" << endl;
player.takeDice(dice3);
player.rollDice();
player.putDice(dice3);
return 0;
}