-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xorti.cpp
85 lines (75 loc) · 3.69 KB
/
Xorti.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Xorti.h"
#include "SinExcepcionesDinero.h"
#include "Bancarota.h"
using namespace Exception;
using namespace std;
using namespace GameMechanics;
GameBoard::Xorti::Xorti(const string &name) : Tile(name) {}
void GameBoard::Xorti::action(Player::Participant * participant, GameMechanics::Game * game) {
int xorti = rand() % MAXIMUM_XORTI;
try {
if (xorti == 0) {
birthday(*participant, game);
} else if (xorti == 1) {
parkingFine(*participant, game);
} else if (xorti == 2) {
mepaFine(*participant, game);
} else {
moveToRandomPlace(*participant, game);
}
} catch (NoMoneyException & exception) {
cout << exception.message << endl;
bool isPlayerNotBankrupt = exception.payAmountDue(game, exception.amountDue, participant, nullptr);
if (!isPlayerNotBankrupt) {
Bankruptcy::transferProperties(game, participant, nullptr);
}
}
}
void GameBoard::Xorti::birthday(Player::Participant &participant, GameMechanics::Game * game) {
double birthdayMoney = rand() % (MAXIMUM_BIRTHDAY_PRICE - MINIMUM_BIRTHDAY_PRICE) + MINIMUM_BIRTHDAY_PRICE;
std::cout << "Es tu celebracion tome " << birthdayMoney << " un regalo de todos." << std::endl;
for (auto &player : game->getParticipantsPlaying()) {
if (player == &participant) {
continue;
}
try {
player->getMoney().subtractBalance(birthdayMoney);
participant.getMoney().addBalance(birthdayMoney);
} catch (NoMoneyException &noMoneyException) {
cout << noMoneyException.message << endl;
bool isPlayerNotBankrupt = noMoneyException.payAmountDue(game, noMoneyException.amountDue, player, &participant);
if (!isPlayerNotBankrupt) {
Bankruptcy::transferProperties(game, player, &participant);
}
}
}
}
void GameBoard::Xorti::parkingFine(Player::Participant &participant, GameMechanics::Game * game) {
double parkingFine = rand() % (MAXIMUM_PARKING_FINE - MINIMUM_PARKING_FINE) + MINIMUM_PARKING_FINE;
std::cout << "Te estacionaste en el lugar de un profesor! Fuiste multado " << parkingFine << "." << std::endl;
try {
participant.getMoney().subtractBalance(parkingFine);
game->setFreeParkingJackpot(game->getFreeParkingJackpot() + parkingFine);
} catch (NoMoneyException & noMoneyException) {
throw NoMoneyException(noMoneyException.amountDue);
}
}
void GameBoard::Xorti::mepaFine(Player::Participant &participant, GameMechanics::Game * game) {
double mepaFine = rand() % (MAXIMUM_MEPA_FINE - MINIMUM_MEPA_FINE) + MINIMUM_MEPA_FINE;
double totalFine = mepaFine * participant.getParticipantProperties().size();
std::cout << "MEPA te multó !!! Fuiste multado en total: " << totalFine << ". Tienes en total "
<< participant.getParticipantProperties().size() << " edificios... cada edificio fue multado "
<< mepaFine << "." << std::endl;
// If participant does not have enough money, handle the exception in action method
try {
participant.getMoney().subtractBalance(totalFine);
game->setFreeParkingJackpot(game->getFreeParkingJackpot() + totalFine);
} catch (NoMoneyException & noMoneyException) {
throw NoMoneyException(noMoneyException.amountDue);
}
}
void GameBoard::Xorti::moveToRandomPlace(Player::Participant &participant, GameMechanics::Game * game) {
participant.setCurrentPosition(rand() % GameMechanics::TOTAL_TILES);
std::cout << "¡Te movieron a una ubicación aleatoria! Nueva ubicación es: "
<< game->getGameBoard()[participant.getCurrentPosition()]->getName() << std::endl;
}