Untitled
unknown
plain_text
3 years ago
2.4 kB
7
Indexable
#include <iostream>
#include <string>
// Define our player's basic stats
struct Player {
std::string name;
int health;
int attack;
int defense;
};
// Define the zombie's basic stats
struct Zombie {
std::string name;
int health;
int attack;
int defense;
};
// Function to calculate the damage dealt in an attack
int calculate_damage(int attack, int defense) {
return attack - defense;
}
int main() {
// Create our player and give them a name
Player player;
std::cout << "Enter your name: ";
std::cin >> player.name;
// Set the player's basic stats
player.health = 100;
player.attack = 10;
player.defense = 5;
// Create our first zombie and set its basic stats
Zombie zombie;
zombie.name = "Bruneian Zombie";
zombie.health = 50;
zombie.attack = 5;
zombie.defense = 2;
// Print the starting state of the game
std::cout << "A " << zombie.name << " has appeared!" << std::endl;
std::cout << player.name << " has " << player.health << " health." << std::endl;
std::cout << zombie.name << " has " << zombie.health << " health." << std::endl;
// Main game loop
while (player.health > 0 && zombie.health > 0) {
std::cout << "What would you like to do?" << std::endl;
std::cout << "1. Attack" << std::endl;
std::cout << "2. Defend" << std::endl;
std::cout << "3. Flee" << std::endl;
int choice;
std::cin >> choice;
if (choice == 1) {
// Player chooses to attack
int damage = calculate_damage(player.attack, zombie.defense);
std::cout << player.name << " attacks the " << zombie.name << " for " << damage << " damage!" << std::endl;
zombie.health -= damage;
if (zombie.health <= 0) {
std::cout << "The " << zombie.name << " has been defeated!" << std::endl;
break;
} else {
// Zombie counterattacks
damage = calculate_damage(zombie.attack, player.defense);
std::cout << "The " << zombie.name << " counterattacks for " << damage << " damage!" << std::endl;
player.health -= damage;
if (player.health <= 0) {
std::cout << player.name << " has been defeated!" << std::endl;
break;
}
}
} else if (choice == 2) {
// Player chooses to defend
std::cout << player.name << " defends against the " << zombie.name << "." << std::endl;
player.
Editor is loading...