Path: blob/main/extensions/copilot/src/extension/prompts/node/test/fixtures/cppNoExtraSemicolons.cpp
13406 views
#include <iostream>1#include <string>2#include <vector>3#include <memory>4#include <random>5#include <algorithm>67/**8* Base Item class for game objects9*/10class Item {11private:12std::string name;13std::string description;14int value;1516public:17Item(const std::string& itemName, const std::string& itemDesc, int itemValue)18: name(itemName), description(itemDesc), value(itemValue) {}1920virtual ~Item() = default;2122std::string getName() const { return name; }23std::string getDescription() const { return description; }24int getValue() const { return value; }2526virtual void use() {27std::cout << "Using " << name << ": " << description << std::endl;28}29};3031/**32* Weapon class derived from Item33*/34class Weapon : public Item {35private:36int damage;3738public:39Weapon(const std::string& name, const std::string& desc, int value, int weaponDamage)40: Item(name, desc, value), damage(weaponDamage) {}4142int getDamage() const { return damage; }4344void use() override {45std::cout << "Wielding " << getName() << " that deals " << damage << " damage!" << std::endl;46}47};4849/**50* Character class for player and NPCs51*/52class Character {53private:54std::string name;55int health;56int maxHealth;57std::vector<std::shared_ptr<Item>> inventory;58std::shared_ptr<Weapon> equippedWeapon;5960public:61Character(const std::string& charName, int charHealth)62: name(charName), health(charHealth), maxHealth(charHealth), equippedWeapon(nullptr) {}6364virtual ~Character() = default;6566std::string getName() const { return name; }67int getHealth() const { return health; }68int getMaxHealth() const { return maxHealth; }6970void takeDamage(int amount) {71health = std::max(0, health - amount);72std::cout << name << " takes " << amount << " damage. Health: " << health << "/" << maxHealth << std::endl;73}7475void heal(int amount) {76health = std::min(maxHealth, health + amount);77std::cout << name << " heals " << amount << " points. Health: " << health << "/" << maxHealth << std::endl;78}7980void addItem(std::shared_ptr<Item> item) {81inventory.push_back(item);82std::cout << name << " received " << item->getName() << std::endl;83}8485void equipWeapon(const std::string& weaponName) {86for (const auto& item : inventory) {87auto weapon = std::dynamic_pointer_cast<Weapon>(item);88if (weapon && weapon->getName() == weaponName) {89equippedWeapon = weapon;90std::cout << name << " equipped " << weaponName << std::endl;91return;92}93}94std::cout << "No weapon named " << weaponName << " in inventory!" << std::endl;95}9697void attack(Character& target) {98if (!equippedWeapon) {99std::cout << name << " has no weapon equipped!" << std::endl;100return;101}102103std::cout << name << " attacks " << target.getName() << " with " << equippedWeapon->getName() << std::endl;104target.takeDamage(equippedWeapon->getDamage());105}106107void showInventory() {108std::cout << name << "'s inventory:" << std::endl;109if (inventory.empty()) {110std::cout << " Empty" << std::endl;111return;112}113114for (const auto& item : inventory) {115std::cout << " " << item->getName() << " - " << item->getDescription() << std::endl;116}117}118};119120/**121* Game class to manage game state122*/123class Game {124private:125std::shared_ptr<Character> player;126std::vector<std::shared_ptr<Character>> enemies;127bool gameRunning;128129public:130Game() : gameRunning(false) {}131132void initialize() {133// Create player134player = std::make_shared<Character>("Hero", 100);135136// Add some starter items137player->addItem(std::make_shared<Weapon>("Rusty Sword", "An old but reliable blade", 5, 10));138player->addItem(std::make_shared<Item>("Health Potion", "Restores 20 health", 15));139140// Create an enemy141enemies.push_back(std::make_shared<Character>("Goblin", 30));142143gameRunning = true;144std::cout << "Game initialized! Welcome, " << player->getName() << "!" << std::endl;145}146147void run() {148if (!gameRunning) {149initialize();150}151152// Simple gameplay demonstration153player->showInventory();154player->equipWeapon("Rusty Sword");155156std::cout << "\nA " << enemies[0]->getName() << " appears!" << std::endl;157player->attack(*enemies[0]);158159if (enemies[0]->getHealth() > 0) {160std::cout << enemies[0]->getName() << " attacks back!" << std::endl;161// Simulating enemy attack with fixed damage162player->takeDamage(5);163} else {164std::cout << enemies[0]->getName() << " was defeated!" << std::endl;165enemies.erase(enemies.begin());166}167168std::cout << "Game demonstration complete." << std::endl;169}170};171172/**173* Main function174*/175int main() {176std::cout << "=== Text Adventure Game ===" << std::endl;177178Game game;179game.run();180181return 0;182}183184185