-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBattle.cpp
More file actions
302 lines (256 loc) · 6.02 KB
/
Copy pathBattle.cpp
File metadata and controls
302 lines (256 loc) · 6.02 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
February 1, 2015
Nicholas Sabelli
COMP 345 Section SI
Assignement #1
This file implements the Battle class along with the Die structure. It is used to define the battle phase of risk.
*/
#include <iostream>
#include <ctime>
#include "Battle.h"
#include "Country.h"
using namespace std;
Battle::Battle(Country* attack, Country* defend)
{
allIn = false;
cout << "Battle!!!" << endl << endl;
cout << attack->name << " Vs. " << defend->name << endl << endl;
cout << "Player " << attack->occupiedBy->name << " Vs. Player " << defend->occupiedBy->name << endl << endl;
cout << attack->numberOfPieces << " Armies Vs. " << defend->numberOfPieces << " Armies" << endl << endl;
if (CanBattle(attack, defend))
{
if (attack->occupiedBy->isComputer)
allIn = true;
else
allIn = AllIn(); //Are you all in?
}
while (CanBattle(attack, defend)) //Verifies if both have enough armies to continue battling.
{
Roll(attack, defend);
if (!allIn)
{
if (attack->occupiedBy->isComputer || WillContinue()) //Continue fight?
{
if (attack->occupiedBy->isComputer)
allIn = true;
else
allIn = AllIn(); //Are you all in?
}
else
{
break;
}
}
}
if (IsVictory(attack, defend)) //Did attacker win?
{
Conquer(attack, defend);
}
else
{
cout << "Attacking Player Was Defeated!" << endl << endl;
}
}
bool Battle::AllIn()
{
char ans;
do
{
cout << "Are You All In? (Y/N): ";
cin >> ans;
cout << endl;
if (toupper(ans) == 'Y')
{
return true;
}
else if (toupper(ans) == 'N')
{
return false;
}
} while (true);
}
bool Battle::CanBattle(Country* attack, Country* defend)
{
if (attack->numberOfPieces > 1 && defend->numberOfPieces > 0)
{
return true;
}
else
{
return false;
}
}
bool Battle::WillContinue()
{
char ans;
do
{
cout << "Would You Like To Continue? (Y/N): ";
cin >> ans;
cout << endl;
if (toupper(ans) == 'Y')
{
return true;
}
else if (toupper(ans) == 'N')
{
return false;
}
} while (true);
}
bool Battle::IsVictory(Country* attack, Country* defend)
{
if (defend->numberOfPieces == 0)
{
return true;
}
else
{
return false;
}
}
void Battle::Roll(Country* attack, Country* defend)
{
int attackRoll = 0;
int defendRoll = 0;
int tempRoll = 0;
switch (attack->numberOfPieces) //Depending on the attackers number of pieces he/she get a max amount of die.
{
case 2:
attackRoll = 1;
break;
case 3:
attackRoll = 2;
break;
default:
attackRoll = 3;
}
switch (defend->numberOfPieces) //Depending on the defends number of pieces he/she get a max amount of die.
{
case 1:
defendRoll = 1;
break;
default:
defendRoll = 2;
}
if (!allIn) //Not all in? How many die do I roll then?
{
do
{
cout << attack->occupiedBy->name << " Player How Many Die Would You Like To Roll? (1";
for (int i = 1; i < attackRoll; i++)
{
cout << ", " << i + 1;
}
cout << "): ";
cin >> tempRoll;
cout << endl;
} while (tempRoll < 1 || tempRoll > attackRoll);
attackRoll = tempRoll;
do
{
cout << defend->occupiedBy->name << " Player How Many Die Would You Like To Roll? (1";
for (int i = 1; i < defendRoll; i++)
{
cout << ", " << i + 1;
}
cout << "): ";
cin >> tempRoll;
cout << endl;
} while (tempRoll < 1 || tempRoll > defendRoll);
defendRoll = tempRoll;
}
srand(static_cast<int>(time(0)));
Die att;
Die def;
for (int i = 0; i < attackRoll; i++) //Rolls attackers die.
{
att.roles[i] = rand() % 6 + 1;
}
for (int i = 0; i < defendRoll; i++) //Rolls defenders die.
{
def.roles[i] = rand() % 6 + 1;
}
attack->diesRolled = attackRoll;
defend->diesRolled = defendRoll;
int attWin = 0;
int attLoss = 0;
Compare(att, def, attackRoll, defendRoll, attWin, attLoss);
Casualties(attack, defend, attWin, attLoss);
Report(attack, defend, attWin, attLoss);
}
void Battle::Compare(Die& att, Die& def, int attackRoll, int defendRoll, int& attWin, int& attLoss)
{
for (int i = 0; i < attackRoll; i++) //Sort attackers die, highest to lowest.
{
for (int j = 1; j < attackRoll; j++)
{
if (att.roles[i] < att.roles[j])
{
swap(att.roles[i], att.roles[j]);
}
}
}
for (int i = 0; i < defendRoll; i++) //Sort defenders die, highest to lowest.
{
for (int j = 1; j < defendRoll; j++)
{
if (def.roles[i] < def.roles[j])
swap(def.roles[i], def.roles[j]);
}
}
for (int i = 0; i < defendRoll; i++) //Compare pairs of die.
{
if (att.roles[i] > def.roles[i])
{
attWin++; //Attack die higher.
}
else
{
attLoss++; //Defend die higher or equal.
}
}
}
void Battle::Report(Country* attack, Country* defend, int attWin, int attLoss)
{
cout << "Attacking Player Destroyed " << attWin << " Of The Defending Players Armies And Lost " << attLoss << " Armies!" << endl << endl;
}
void Battle::Casualties(Country* attack, Country* defend, int attWin, int attLoss)
{
//Losses from the attack are subtracted.
defend->numberOfPieces -= attWin;
attack->numberOfPieces -= attLoss;
}
void Battle::Conquer(Country* attack, Country* defend)
{
int ans;
cout << "Congratulations Attacking Player You Have Defeated Your Opponent!" << endl << endl;
if (attack->numberOfPieces > 2)
{
if (attack->occupiedBy->isComputer)
ans = attack->numberOfPieces - 1;
else{
do
{
cout << "How many armies would you like to move? (min " << attack->diesRolled << ", max " << attack->numberOfPieces - 1 << "): ";
cin >> ans;
cout << endl;
} while (ans < attack->diesRolled || ans > attack->numberOfPieces - 1); //Range min and max of pieces to move.
}
//Transfer of ownership.
attack->numberOfPieces -= ans;
defend->numberOfPieces += ans;
defend->occupiedBy->RemoveCountry(defend);
attack->occupiedBy->AddCountry(defend);
defend->occupiedBy = attack->occupiedBy;
}
else //Attacker only has 2 pieces. One stays the other captures.
{
//Transfer of ownership.
attack->numberOfPieces -= 1;
defend->numberOfPieces += 1;
attack->occupiedBy->AddCountry(defend);
defend->occupiedBy->RemoveCountry(defend);
defend->occupiedBy = attack->occupiedBy;
}
}