-
Notifications
You must be signed in to change notification settings - Fork 0
/
CardTen.cpp
133 lines (116 loc) · 3.05 KB
/
CardTen.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
132
133
#include "CardTen.h"
int CardTen::numberofCards = 0;
int CardTen::price = -1;
int CardTen::fees = -1;
Player* CardTen::Owner = NULL;
bool CardTen::firstSave = true;
CardTen::CardTen(const CellPosition& pos) :Card(pos) {
cardNumber = 10;
numberofCards++;
}
void CardTen::SetFees(int x)
{
fees = x;
}
void CardTen::SetPrice(int x)
{
price = x;
}
// A Constructor takes card position
Card* CardTen::GetCopy() {
return new CardTen(position);
}
void CardTen::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(10) price: ");
int p = pIn->GetInteger(pOut);
pOut->PrintMessage("Enter card(10) 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(10) 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(10) fees: ");
f = pIn->GetInteger(pOut);
}
fees = f;
pOut->ClearStatusBar();
}
void CardTen::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(10) (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 CardTen::Save(ofstream& OutFile, const CellPosition& startCellPos)
{
Card::Save(OutFile, startCellPos);
if (firstSave) {
OutFile << " " << price << " " << fees << endl;
firstSave = false;
}
else
OutFile << endl;
}
CardTen::~CardTen() {
numberofCards--;
if (numberofCards == 0) {
price = -1;
fees = -1;
Owner = NULL;
firstSave = true;
}
}
void CardTen::Load(ifstream& Infile)
{
if(price==-1)
Infile >> price >> fees;
}
int CardTen::getPrice()const {
return price;
}
bool CardTen::CheckOwner(Player* p)const {
if (Owner == p)
return true;
else
false;
}
void CardTen::SetOwner(Player* p) {
Owner = p;
}