-
Notifications
You must be signed in to change notification settings - Fork 0
/
fightMech.cpp
140 lines (119 loc) · 4.34 KB
/
fightMech.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
134
135
136
137
138
139
140
#include "fightMech.h"
#include "characters.h"
#include <iostream>
using namespace std;
void displayFightStats(const Character& character, const Mob& mob) {
cout << character.type << " Stats: " << "\t";
cout << mob.name << " Stats: " << endl;
cout << "HP: " << character.hp << "\t";
cout << "HP: " << mob.hp << endl;
cout << "Attack Power: " << character.attackPower << "\t";
cout << "Attack Power: " << mob.attackPower << endl;
cout << "Defense: " << character.defense << "\t";
cout << "Defense: " << mob.defense << endl;
cout << "Agility: " << character.agility << endl;
cout << "Magic: " << character.magic << "\t";
cout << "Magic: " << mob.magic << endl;
cout << "-----------------------------" << endl;
}
int calculateDamage(int attackPower, int defense) {
int damage = attackPower - defense;
return (damage > 0) ? damage : 1; // Ensure minimum damage is 1
}
void playerAttack(Mob &mob, Character &player) {
int damage = calculateDamage(player.attackPower, mob.defense);
mob.hp -= damage;
cout << "You attacked " << mob.name << " for " << damage << " damage!" << endl;
if (mob.hp <= 0) {
cout << mob.name << " has been defeated!" << endl;
player.purse += mob.purse;
}
}
void mobAttack(Mob &mob, Character &player) {
if(mob.magic != 0){
int random = rand() % 100;
if(random < 75){
int damage = calculateDamage(mob.attackPower, player.defense);
player.hp -= damage;
cout << mob.name << " attacked you for " << damage << " damage!" << endl;
if (player.hp <= 0) {
cout << "You have been defeated!" << endl;
}
}
else{
int damage = (mob.attackPower * mob.magic / player.agility) - player.defense;
player.hp -= damage;
cout << mob.name << " attacked you for " << damage << " damage!" << endl;
if (player.hp <= 0) {
cout << "You have been defeated!" << endl;
}
}
}else {
int damage = calculateDamage(mob.attackPower, player.defense);
player.hp -= damage;
cout << mob.name << " attacked you for " << damage << " damage!" << endl;
if (player.hp <= 0) {
cout << "You have been defeated!" << endl;
}
}
}
void magicAttack ( Mob &mob, Character &player){
if(player.magic == 0){
cout << player.type << " doesnt have any magic" << endl;
}
else {
int random = rand() % 100;
if(random < 50){
int damage = (player.attackPower * player.magic) - mob.defense;
mob.hp -= damage;
cout << "You attacked " << mob.name << " for " << damage << " damage!" << endl;
if (mob.hp <= 0) {
cout << mob.name << " has been defeated!" << endl;
player.purse += mob.purse;
}
}else{
cout << "Attack Missed!" << endl;
}
}
}
void fight(Mob &mob, Character &player) {
while (mob.hp > 0 && player.hp > 0) {
cout << "Press 1 to Attack" << endl << "Press 2 to use magic" << endl << "Press 3 to run away " << endl;
int choice;
cin >> choice;
if (choice == 1) {
// Player attacks first
playerAttack(mob, player);
// Check if mob is dead
if (mob.hp > 0) {
// If mob is still alive, it attacks back
mobAttack(mob, player);
}
displayFightStats(player,mob);
}
else if(choice == 2 ) {
magicAttack(mob,player);
if (mob.hp > 0) {
// If mob is still alive, it attacks back
mobAttack(mob, player);
}
displayFightStats(player,mob);
}
else if (choice == 3) {
cout << "You ran away from the fight!" << endl;
break;
}
else {
cout << "Invalid choice! Please enter 1, 2 or 3." << endl;
}
// Check if the player is dead after the mob's attack
if (player.hp <= 0) {
cout << "Game over! You have been defeated by the " << mob.name << "." << endl;
break;
}
}
if (mob.hp <= 0) {
cout << "You have successfully defeated the " << mob.name << "!" << endl;
displayStats(player);
}
}