Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/test/fixtures/cppNoExtraSemicolons.cpp
13406 views
1
#include <iostream>
2
#include <string>
3
#include <vector>
4
#include <memory>
5
#include <random>
6
#include <algorithm>
7
8
/**
9
* Base Item class for game objects
10
*/
11
class Item {
12
private:
13
std::string name;
14
std::string description;
15
int value;
16
17
public:
18
Item(const std::string& itemName, const std::string& itemDesc, int itemValue)
19
: name(itemName), description(itemDesc), value(itemValue) {}
20
21
virtual ~Item() = default;
22
23
std::string getName() const { return name; }
24
std::string getDescription() const { return description; }
25
int getValue() const { return value; }
26
27
virtual void use() {
28
std::cout << "Using " << name << ": " << description << std::endl;
29
}
30
};
31
32
/**
33
* Weapon class derived from Item
34
*/
35
class Weapon : public Item {
36
private:
37
int damage;
38
39
public:
40
Weapon(const std::string& name, const std::string& desc, int value, int weaponDamage)
41
: Item(name, desc, value), damage(weaponDamage) {}
42
43
int getDamage() const { return damage; }
44
45
void use() override {
46
std::cout << "Wielding " << getName() << " that deals " << damage << " damage!" << std::endl;
47
}
48
};
49
50
/**
51
* Character class for player and NPCs
52
*/
53
class Character {
54
private:
55
std::string name;
56
int health;
57
int maxHealth;
58
std::vector<std::shared_ptr<Item>> inventory;
59
std::shared_ptr<Weapon> equippedWeapon;
60
61
public:
62
Character(const std::string& charName, int charHealth)
63
: name(charName), health(charHealth), maxHealth(charHealth), equippedWeapon(nullptr) {}
64
65
virtual ~Character() = default;
66
67
std::string getName() const { return name; }
68
int getHealth() const { return health; }
69
int getMaxHealth() const { return maxHealth; }
70
71
void takeDamage(int amount) {
72
health = std::max(0, health - amount);
73
std::cout << name << " takes " << amount << " damage. Health: " << health << "/" << maxHealth << std::endl;
74
}
75
76
void heal(int amount) {
77
health = std::min(maxHealth, health + amount);
78
std::cout << name << " heals " << amount << " points. Health: " << health << "/" << maxHealth << std::endl;
79
}
80
81
void addItem(std::shared_ptr<Item> item) {
82
inventory.push_back(item);
83
std::cout << name << " received " << item->getName() << std::endl;
84
}
85
86
void equipWeapon(const std::string& weaponName) {
87
for (const auto& item : inventory) {
88
auto weapon = std::dynamic_pointer_cast<Weapon>(item);
89
if (weapon && weapon->getName() == weaponName) {
90
equippedWeapon = weapon;
91
std::cout << name << " equipped " << weaponName << std::endl;
92
return;
93
}
94
}
95
std::cout << "No weapon named " << weaponName << " in inventory!" << std::endl;
96
}
97
98
void attack(Character& target) {
99
if (!equippedWeapon) {
100
std::cout << name << " has no weapon equipped!" << std::endl;
101
return;
102
}
103
104
std::cout << name << " attacks " << target.getName() << " with " << equippedWeapon->getName() << std::endl;
105
target.takeDamage(equippedWeapon->getDamage());
106
}
107
108
void showInventory() {
109
std::cout << name << "'s inventory:" << std::endl;
110
if (inventory.empty()) {
111
std::cout << " Empty" << std::endl;
112
return;
113
}
114
115
for (const auto& item : inventory) {
116
std::cout << " " << item->getName() << " - " << item->getDescription() << std::endl;
117
}
118
}
119
};
120
121
/**
122
* Game class to manage game state
123
*/
124
class Game {
125
private:
126
std::shared_ptr<Character> player;
127
std::vector<std::shared_ptr<Character>> enemies;
128
bool gameRunning;
129
130
public:
131
Game() : gameRunning(false) {}
132
133
void initialize() {
134
// Create player
135
player = std::make_shared<Character>("Hero", 100);
136
137
// Add some starter items
138
player->addItem(std::make_shared<Weapon>("Rusty Sword", "An old but reliable blade", 5, 10));
139
player->addItem(std::make_shared<Item>("Health Potion", "Restores 20 health", 15));
140
141
// Create an enemy
142
enemies.push_back(std::make_shared<Character>("Goblin", 30));
143
144
gameRunning = true;
145
std::cout << "Game initialized! Welcome, " << player->getName() << "!" << std::endl;
146
}
147
148
void run() {
149
if (!gameRunning) {
150
initialize();
151
}
152
153
// Simple gameplay demonstration
154
player->showInventory();
155
player->equipWeapon("Rusty Sword");
156
157
std::cout << "\nA " << enemies[0]->getName() << " appears!" << std::endl;
158
player->attack(*enemies[0]);
159
160
if (enemies[0]->getHealth() > 0) {
161
std::cout << enemies[0]->getName() << " attacks back!" << std::endl;
162
// Simulating enemy attack with fixed damage
163
player->takeDamage(5);
164
} else {
165
std::cout << enemies[0]->getName() << " was defeated!" << std::endl;
166
enemies.erase(enemies.begin());
167
}
168
169
std::cout << "Game demonstration complete." << std::endl;
170
}
171
};
172
173
/**
174
* Main function
175
*/
176
int main() {
177
std::cout << "=== Text Adventure Game ===" << std::endl;
178
179
Game game;
180
game.run();
181
182
return 0;
183
}
184
185