-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCardThirteen.cpp
More file actions
151 lines (132 loc) · 3.93 KB
/
CardThirteen.cpp
File metadata and controls
151 lines (132 loc) · 3.93 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "CardThirteen.h"
CardThirteen::CardThirteen(const CellPosition& pos) : Card(pos) // set the cell position of the card
{
cardNumber = 13; // set the inherited cardNumber data member with the card number (10 here)
NumberOf13Cards++;
}
Player* CardThirteen::Owner = nullptr;
int CardThirteen::NumberOf13Cards = 0;
int CardThirteen::SavedCards = 0;
int CardThirteen::Price = 0;
int CardThirteen::Fees = 0;
void CardThirteen::ReadCardParameters(Grid* pGrid)
{
if (NumberOf13Cards == 1)
{
// Get a Pointer to the Input / Output Interfaces
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
// Read the Price parameter
pOut->PrintMessage("Enter Price of The Card ..");
Price = pIn->GetInteger(pOut);
if (Price < 0)
{
pGrid->PrintErrorMessage("You entered an invalid price, click to continue...");
return;
}
// Read the Fees parameter
pOut->PrintMessage("Enter Fees to Pay by Passing Players ..");
Fees = pIn->GetInteger(pOut);
if (Fees < 0)
pGrid->PrintErrorMessage("You entered an invalid Fees, click to continue...");
///Make the needed validations on the read parameters
// Clear messages
//pOut->ClearStatusBar();
}
}
void CardThirteen::Apply(Grid* pGrid, Player* pPlayer)
{
Card::Apply(pGrid, pPlayer);
if (Owner == nullptr)
{
// Get a Pointer to the Input / Output Interfaces
Input* pIn = pGrid->GetInput();
Output* pOut = pGrid->GetOutput();
pOut->PrintMessage("Do you want to buy all cells with cardnum " + to_string(cardNumber) + " ? Price: " + to_string(Price) + " ,Fees: " + to_string(Fees) + " (Y/N)");
string ans;
do
{
ans = pIn->GetSrting(pOut);
pOut->ClearStatusBar();
if (ans == "y" || ans == "Y" || ans == "yes" || ans == "YES" || ans == "1") ans = "yes";
else if (ans == "n" || ans == "N" || ans == "no" || ans == "NO" || ans == "0") ans = "no";
else pGrid->PrintErrorMessage("Error: Wrong Input ! Try again ...");
} while (!(ans == "yes") && !(ans == "no"));
if (ans == "yes")
{
if (Price <= pPlayer->GetWallet())
{
pPlayer->SetWallet(pPlayer->GetWallet() - Price);
Owner = pPlayer;
pGrid->PrintErrorMessage("You bought the cards successfully...");
}
else pGrid->PrintErrorMessage("Error: You don't have enough money...");
}
}
else
{
if (pPlayer != Owner)
{
pPlayer->SetWallet(pPlayer->GetWallet() - Fees);
Owner->SetWallet(Owner->GetWallet() + Fees);
}
}
}
bool CardThirteen::IsValidCard() const
{
if (Price > 0 && Fees > 0) return true;
return false;
}
void CardThirteen::Save(ofstream& save, ObjectType T)
{
if (T != Cards) return;
save << cardNumber << " " << position.GetCellNum();
if (++SavedCards == 1)
save << " " << Price << " " << Fees;
save << "\n";
if (SavedCards == NumberOf13Cards) SavedCards = 0;
}
void CardThirteen::Read(ifstream& load)
{
int cellpos, p, f;
load >> cellpos;
position = CellPosition::GetCellPositionFromNum(cellpos);
if (NumberOf13Cards == 1)
{
load >> p >> f;
Price = p;
Fees = f;
}
}
void CardThirteen::RemoveOwnership()
{
Owner = NULL;
}
void CardThirteen::EditParameters(Grid* pGrid)
{
int prevPrice = Price, prevFees = Fees;
// Get a Pointer to the Input / Output Interfaces
Output* pOut = pGrid->GetOutput();
Input* pIn = pGrid->GetInput();
// Read the startPos parameter
pOut->PrintMessage("Enter Price of The Card ..");
Price = pIn->GetInteger(pOut);
// Read the endPos parameter
pOut->PrintMessage("Enter Fees to Pay by Passing Players ..");
Fees = pIn->GetInteger(pOut);
///Make the needed validations on the read parameters
if (!IsValidCard())
{
pGrid->PrintErrorMessage("Error: Invalid Values ...");
Price = prevPrice;
Fees = prevFees;
return;
}
pGrid->PrintErrorMessage("Card Edited Successfully ...");
// Clear messages
pOut->ClearStatusBar();
}
CardThirteen::~CardThirteen()
{
NumberOf13Cards--;
}