-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
449 lines (416 loc) · 16.7 KB
/
main.cpp
File metadata and controls
449 lines (416 loc) · 16.7 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//
// main.cpp
//
// Project: UALR - Programming 2 - Spring 22 - Power Play 5
// Created by: Mark McCorkle on 20220218
// Based on: Code Provided by Sean Orme
// IDE: CLion 2021.2.3 - VERIFIED WORKING
// IDE: XCode - VERIFIED/UNVERIFIED
// IDE: Visual Studio 2022 - VERIFIED/UNVERIFIED
// IDE: Linux via g++ - VERIFIED WORKING (g++ -Wall -std=c++17 main.cpp -o main)
//
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <map>
struct Item
{
enum class Type { sword, armor, shield, numTypes };
Type clasification;
int bonusValue;
};
struct Object
{
enum class Type { player, slime, orc, sprite, dragon, numTypes };
Type name;
int strength{ 0 };
int health{ 0 };
int level{ 0 };
std::map<Item::Type, Item> inventory;
};
std::vector<Object> createMonsters(const Object& player);
void monsterAttack(Object& player, const std::vector<Object>& monsters);
void bringOutYourDead(std::vector<Object>& monsters);
void playerAttack(const Object& player, std::vector<Object>& monsters);
void levelUp(Object& player);
int calculateAC(const Object& object);
void heal(Object& object);
void displayBattle(const Object& player, const std::vector<Object>& monsters);
void printName(const Object& object);
void printItem(const Item & item);
int attack(const Object& object);
void defend(Object& object, int damage);
std::random_device seed;
std::default_random_engine engine(seed());
/* Instructions:
There are 5 for loops in my solution, we are going to switch the ones we can to STL algorithms. For my solution:
✓) one used std::generate
✓) note::you have to create a vector with the right number of elements.
✓) std::vector<int> v(4); would create a vector with 4 elements.
✓) one used std::remove_if
3) three used std::for_each NOTE::this is for practice. Most of these would be just fine as a ranged based for loop
NOTE:: for the lambda parameter for player.inventory, you will want to use: std::pair<Item::Type, Item>
*/
int main()
{
Object player{ Object::Type::player, 0,1,0, {} };
std::vector<Object> monsters;
while (player.health > 0)
{
levelUp(player);
monsters = createMonsters(player);
std::cout << monsters.size() << " monster(s) approaches!!" << std::endl;
system("pause");
system("cls");
while (player.health > 0 && monsters.size() > 0)
{
displayBattle(player, monsters);
std::cout << "What do you do? (a)ttack (h)eal ";
char command{ 'x' };
std::cin >> command;
switch (command)
{
case 'a':
{
playerAttack(player, monsters);
break;
}
case 'h' :
heal(player);
break;
default:
std::cout << "please enter a or h" << std::endl;
break;
}
bringOutYourDead(monsters);
monsterAttack(player, monsters);
system("PAUSE");
system("CLS");
}
}
if (player.health <= 0)
{
std::cout << "You Have Died" << std::endl;
}
if (player.health <= 0 && monsters.size() == 0)
{
std::cout << "BUT" << std::endl;
}
if (monsters.size() == 0)
{
std::cout << "You have killed the monsters!!!" << std::endl;
}
system("PAUSE");
return 0;
}
void displayBattle(const Object& player, const std::vector<Object>& monsters)
{
printName(player);
std::cout << " h:" << player.health << std::endl;
// Capture Clause: There is nothing to reference other than the inventory item pair being passed in. Therefore,
// the capture clause will remain empty.
// Parameters: As we iterate over the map of inventory items, we pass in each "item" as a reference with the
// variable name of "item". It is also marked as const as the pair is passed into displayBattle as a
// const. This ensures that the item pair never gets modified within the scope of this function. For
// each "item", we print the inventory item as was set up before.
// Return-Type: With the for_each, void is the return type we use. Since, all changes are captured within the lambda
auto inventorySweepLambda = []( const std::pair<Item::Type, Item>& item )->void
{
std::cout << " ";
printItem(item.second);
std::cout << std::endl;
};
std::for_each( player.inventory.begin(), player.inventory.end(), inventorySweepLambda );
std::cout << std::endl << " Monsters: " << std::endl;
//// OLD WAY:
// for (const auto & item : player.inventory)
// {
// std::cout << " ";
// printItem(item.second);
// std::cout << std::endl;
// }
// std::cout << std::endl << " Monsters: " << std::endl;
int i{ 0 };
// Capture Clause: There is one outside reference and that is "i". Which, is the index value of the current monster.
// Parameters: As we iterate over the vector of monster Objects, we pass in each Object as a reference with the
// variable name of "monster". It is also marked as const as the monsters vector is passed into
// monsterAttack as a const. This ensures that the object never gets modified within the scope of this
// function. For each "monster", we apply the existing code as setup before.
// Return-Type: With the for_each, void is the return type we use. Since, all changes are captured within the lambda
auto monsterNamePrinterLambda = [&i]( const Object& monster )->void
{
std::cout << " " << i + 1 << ". ";
printName(monster);
std::cout << " h:" << monster.health << std::endl;
i++;
};
std::for_each( monsters.begin(), monsters.end(), monsterNamePrinterLambda );
//// OLD WAY:
// for (int i{ 0 }; i < monsters.size(); i++)
// {
// std::cout << " " << i + 1 << ". ";
// printName(monsters[i]);
// std::cout << " h:" << monsters[i].health << std::endl;
// }
}
std::vector<Object> createMonsters(const Object& player)
{
std::normal_distribution<double> randomNumMonsters((double)player.level, player.level / 2.0);
int numMonsters{ std::max(1, (int)randomNumMonsters(engine)) };
std::vector<Object> monsters( numMonsters );
// Capture Clause: Kinda like global variables are to the main function. So, we want to reference them by using a
// "&" symbol. I want to limit the scope of what I reference, so I just reference the player object
// only. There's not any other variables that I will need to reference, and I don't want to
// reference EVERYTHING, so I limit it the player object.
// Parameters: There will be nothing to iterator over here as the Objects have not been generated yet. Normally,
// they are the objects we are iterating over, in this case it is the reference to the "Objects" in the
// vector.
// Return-Type: Since we are iterating over a vector of objects, we want to return an Object that is placed at that
// vectors element/location/index
auto monsterGeneratorLambda = [&player]( )-> Object
{
std::normal_distribution<double> monsterLevel((float)player.level, player.level / 4.0);
int level{ std::max(1, (int)monsterLevel(engine)) };
std::uniform_int_distribution<int> monsterType(1, (int)Object::Type::numTypes - 1);
Object::Type name{ (Object::Type)monsterType(engine) };
double strengthVariance{ 0.0 };
double healthVariance{ 0.0 };
switch (name)
{
case Object::Type::slime:
strengthVariance = level * 1.5;
healthVariance = level * 1.25;
break;
case Object::Type::orc:
strengthVariance = level * 2;
healthVariance = level * level * 1.25;
break;
case Object::Type::sprite:
strengthVariance = level * 1.75;
healthVariance = level;
break;
case Object::Type::dragon:
strengthVariance = level * 6;
healthVariance = level * level * 3;
break;
case Object::Type::player:
break;
case Object::Type::numTypes:
break;
}
std::normal_distribution<double> randomStrength(strengthVariance, level / 4.0);
std::normal_distribution<double> randomHealth(healthVariance * 5, level / 2.0);
Object brandSpankingNewMonsterFreshOffThePress = { name,
std::max(1, (int)randomStrength(engine)),
std::max(1, (int)randomHealth(engine)),
level,
{} };
return brandSpankingNewMonsterFreshOffThePress;
};
std::generate( monsters.begin(), monsters.end(), monsterGeneratorLambda );
return monsters;
}
void monsterAttack(Object& player, const std::vector<Object>& monsters)
{
std::bernoulli_distribution willAttack(.75);
std::cout << std::endl;
// Capture Clause: There are two variables that will need to be captured in this case; willAttack and player. Both
// will be passed as reference since they are user-defined types. We could also use a "&" to catch
// all references within, but the specificity narrows the potential for errors to occur.
// Parameters: As we iterate over the vector of monster Objects, we pass in each Object as a reference with the
// variable name of "monster". It is also marked as const as the monsters vector is passed into
// monsterAttack as a const. This ensures that the object never gets modified within the scope of this
// function. For each "monster", we apply the existing code as setup before.
// Return-Type: With the for_each, void is the return type we use. Since, all changes are captured within the lambda
auto monsterAttackLambda = [&willAttack, &player]( const Object& monster )->void
{
if (willAttack(engine))
{
printName(monster);
std::cout << " attacks!" << std::endl;
defend(player, attack(monster));
}
else
{
printName(monster);
std::cout << " twiddles its thumbs" << std::endl;
}
};
std::for_each( monsters.begin(), monsters.end(), monsterAttackLambda );
//// OLD WAY:
// for (const auto & monster : monsters)
// {
// if (willAttack(engine))
// {
// printName(monster);
// std::cout << " attacks!" << std::endl;
// defend(player, attack(monster));
// }
// else
// {
// printName(monster);
// std::cout << " twiddles its thumbs" << std::endl;
// }
// }
}
void playerAttack(const Object& player, std::vector<Object>& monsters)
{
std::cout << "Which Monster: ";
int monsterNum{ 0 };
std::cin >> monsterNum;
if (monsterNum > 0 && monsterNum <= (int)monsters.size())
{
defend(monsters[monsterNum - 1], attack(player));
}
}
void levelUp(Object& player)
{
player.level++;
std::normal_distribution<double> randomHealth(20.0 + player.level * 5, 5.0);
player.health += std::max(1, (int)randomHealth(engine));
std::normal_distribution<double> randomStrength(3.0 + player.level, 1.0);
player.strength += std::max(1, (int)randomStrength(engine));
//grab new item.
std::uniform_int_distribution<int> randomItem(0, (int)Item::Type::numTypes - 1);
std::normal_distribution<double> randomBonus((double)player.level, (double)player.level / 2);
Item found{ (Item::Type)randomItem(engine), std::max(1, (int)randomBonus(engine)) };
std::cout << "You found a ";
printItem(found);
std::cout << "!!!!" << std::endl;
if (
auto haveOne{ player.inventory.find(found.clasification) };
haveOne == player.inventory.end()
|| player.inventory[found.clasification].bonusValue < found.bonusValue
)
{
std::cout << "You keep the shiny new toy!" << std::endl;
player.inventory[found.clasification] = found;
}
else
{
std::cout << "You toss aside the ugly old thing!" << std::endl;
}
}
int calculateAC(const Object& object)
{
int AC{ 0 };
if (auto armor{ object.inventory.find(Item::Type::armor) };
armor != object.inventory.end())
{
AC += armor->second.bonusValue;
}
if (auto shield{ object.inventory.find(Item::Type::shield) };
shield != object.inventory.end())
{
AC += shield->second.bonusValue;
}
return AC;
}
void printName(const Object& object)
{
std::cout << "L:" << object.level << " ";
switch (object.name)
{
case Object::Type::player:
std::cout << "Player";
break;
case Object::Type::slime:
std::cout << "Slime";
break;
case Object::Type::orc:
std::cout << "Orc";
break;
case Object::Type::sprite:
std::cout << "Sprite";
break;
case Object::Type::dragon:
std::cout << "Dragon";
break;
case Object::Type::numTypes:
break;
}
}
void printItem(const Item& item)
{
switch (item.clasification)
{
case Item::Type::armor:
std::cout << "Armor";
break;
case Item::Type::shield:
std::cout << "Shield";
break;
case Item::Type::sword:
std::cout << "Sword";
break;
case Item::Type::numTypes:
break;
}
std::cout << "+" << item.bonusValue;
}
int attack(const Object& object)
{
int potentialDamage{ object.strength };
if (auto sword{ object.inventory.find(Item::Type::sword) };
sword != object.inventory.end())
{
potentialDamage += sword->second.bonusValue;
}
std::normal_distribution<double> damageDealt(potentialDamage, 2.0);
printName(object);
std::cout << " deals ";
return std::max(1, (int)damageDealt(engine));
}
void defend(Object& object, int damage)
{
std::normal_distribution<double> defense(calculateAC(object), 1.0 / object.level);
damage = std::max(0, damage - (int)defense(engine));
std::cout << damage << " damage to ";
printName(object);
std::cout << "!!!" << std::endl;
object.health -= damage;
}
void heal(Object& object)
{
std::normal_distribution<double> randomHeal(object.strength, 3.0);
int amountHealed{ std::max(1, (int)randomHeal(engine)) };
printName(object);
std::cout << " is healed by " << amountHealed << "hp!" << std::endl;
object.health += amountHealed;
}
void bringOutYourDead(std::vector<Object>& monsters)
{
// Capture Clause: There is nothing to reference other than the Object struct instance being passed in. Therefore,
// the capture clause will remain empty.
// Parameters: As we iterate over the vector of Objects, we pass in each Object as a reference with the variable
// name of "object". For each "object", if it matches our conditions, then we remove it. That is,
// remove it from the list of iterators.
// Return-Type: With the remove_if, if it matches the conditions we set, it will return true. Otherwise, it will
// return false. So, we will want a bool as our return type.
auto monsterRemoverLambda = []( Object& object )->bool
{
if (object.health <= 0)
{
printName(object);
std::cout << " has died!!!" << std::endl << std::endl;
return true;
}
return false;
};
monsters.erase( std::remove_if( monsters.begin(), monsters.end(), monsterRemoverLambda ), monsters.end() );
//// OLD WAY:
// for (auto monsterIter{ monsters.begin() }; monsterIter != monsters.end(); )
// {
// if (monsterIter->health <= 0)
// {
// printName(*monsterIter);
// std::cout << " has died!!!" << std::endl << std::endl;
// monsterIter = monsters.erase(monsterIter);
// }
// else
// {
// monsterIter++;
// }
// }
}