-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardNine.cpp
131 lines (116 loc) · 3.06 KB
/
CardNine.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "CardNine.h"
int CardNine::numberofCards = 0;
int CardNine::price = -1;
int CardNine::fees = -1;
Player* CardNine::Owner = NULL;
bool CardNine::firstSave = true;
CardNine::CardNine(const CellPosition& pos):Card(pos) {
cardNumber = 9;
numberofCards++;
}
void CardNine::SetFees(int x)
{
fees = x;
}
void CardNine::SetPrice(int x)
{
price = x;
}
// A Constructor takes card position
Card* CardNine::GetCopy() {
return new CardNine(position);
}
void CardNine::ReadCardParameters(Grid* pGrid) {
//check if its the first time defining
if (numberofCards > 1)
return;
//read price
//read fees
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
pOut->PrintMessage("Enter card(9) price: ");
int p=pIn->GetInteger(pOut);
pOut->PrintMessage("Enter card(9) fees: ");
int f = pIn->GetInteger(pOut);
//validations for price and fees (no negative price or fees)
while (p < 0) {
pGrid->PrintErrorMessage("ERROR: entered price(int) is negative or invalid. Click to continue");
pOut->PrintMessage("Enter card(9) price: ");
p= pIn->GetInteger(pOut);
}
price = p;
while (f < 0) {
pGrid->PrintErrorMessage("ERROR: entered fees(int) is negative or invalid. Click to continue");
pOut->PrintMessage("Enter card(9) fees: ");
f = pIn->GetInteger(pOut);
}
fees = f;
pOut->ClearStatusBar();
}
void CardNine::Apply(Grid* pGrid, Player* pPlayer) {
//call apply of base class
Card::Apply(pGrid, pPlayer);
//if Owner nothing happens
if (Owner == pPlayer)
return;
//input & output pointers
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
//check if first player
if (Owner == NULL) {
pOut->PrintMessage("Buy Stations(9) (price= " + to_string(price) + " fees= " + to_string(fees) + " ) ?(1 to buy)");
int ans = pIn->GetInteger(pOut);
if (ans == 1) {
if (pPlayer->GetWallet() >= price) {
pPlayer->DecrementWallet(price);
Owner = pPlayer;
}
else
pGrid->PrintErrorMessage("Not enough Coins in wallet. Click anywhere to continue.");
}
}
else {
int c=pPlayer->TransferCoins(Owner, fees);
if (c == -1)
pGrid->PrintErrorMessage("Transferred fees to station owner. Click to continue...");
else
pGrid->PrintErrorMessage("Player did not have enough money. Tranferred player's wallet. Click to continue...");
}
pGrid->GetOutput()->ClearStatusBar();
}
void CardNine::Save(ofstream& OutFile, const CellPosition& startCellPos)
{
Card::Save(OutFile, startCellPos);
if (firstSave) {
OutFile << " " << price << " " << fees << endl;
firstSave = false;
}
else
OutFile << endl;
}
void CardNine::Load(ifstream& Infile)
{
if(price==-1)
Infile >> price >> fees;
}
CardNine::~CardNine() {
numberofCards--;
if (numberofCards == 0) {
price = -1;
fees = -1;
Owner = NULL;
firstSave = true;
}
}
int CardNine::getPrice()const {
return price;
}
bool CardNine::CheckOwner(Player* p)const {
if (Owner == p)
return true;
else
false;
}
void CardNine::SetOwner(Player* p) {
Owner = p;
}