cpp
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
class Room {
private:
string name;
string description;
map<string, int> exits;
vector<string> items;
public:
Room(string n = "", string desc = "") : name(n), description(desc) {}
void setExit(string direction, int roomIndex) {
exits[direction] = roomIndex;
}
void addItem(string item) {
items.push_back(item);
}
bool hasItem(string item) {
for (size_t i = 0; i < items.size(); i++) {
if (items[i] == item) return true;
}
return false;
}
void removeItem(string item) {
for (size_t i = 0; i < items.size(); i++) {
if (items[i] == item) {
items.erase(items.begin() + i);
return;
}
}
}
string getName() { return name; }
string getDescription() { return description; }
int getExit(string direction) {
if (exits.find(direction) != exits.end()) {
return exits[direction];
}
return -1;
}
string getExits() {
string result = "Exits: ";
for (auto& exit : exits) {
result += exit.first + " ";
}
return result;
}
string getItems() {
if (items.empty()) return "No items here.";
string result = "Items: ";
for (auto& item : items) {
result += item + " ";
}
return result;
}
};
int main() {
// Create rooms
vector<Room> rooms;
rooms.push_back(Room("Entrance", "You stand at the entrance of a dark dungeon."));
rooms.push_back(Room("Hallway", "A long, dark hallway stretches before you."));
rooms.push_back(Room("Chamber", "A small chamber with ancient symbols on the walls."));
rooms.push_back(Room("Treasure Room", "A room filled with golden treasures!"));
// Set exits
rooms[0].setExit("north", 1);
rooms[1].setExit("south", 0);
rooms[1].setExit("east", 2);
rooms[2].setExit("west", 1);
rooms[2].setExit("north", 3);
rooms[3].setExit("south", 2);
// Add items
rooms[1].addItem("torch");
rooms[2].addItem("key");
rooms[3].addItem("treasure");
// Player inventory
vector<string> inventory;
// Current room
int currentRoom = 0;
bool hasWon = false;
cout << "Welcome to the Mini Text Dungeon!" << endl;
cout << "Commands: go [direction], look, inventory, take [item], quit" << endl << endl;
// Game loop
bool running = true;
while (running) {
// Display current room
cout << "Location: " << rooms[currentRoom].getName() << endl;
cout << rooms[currentRoom].getDescription() << endl;
cout << rooms[currentRoom].getExits() << endl;
cout << rooms[currentRoom].getItems() << endl;
// Check win condition
if (currentRoom == 3 && !hasWon) {
for (auto& item : inventory) {
if (item == "key") {
cout << "\nYou've unlocked the treasure chest with your key!" << endl;
cout << "Congratulations! You've won the game!" << endl;
hasWon = true;
break;
}
}
}
// Get player command
string command, argument;
cout << "\nWhat will you do? ";
cin >> command;
if (command == "go") {
cin >> argument;
int nextRoom = rooms[currentRoom].getExit(argument);
if (nextRoom == -1) {
cout << "You can't go that way." << endl;
} else {
currentRoom = nextRoom;
}
} else if (command == "look") {
// Room description already shown at the start of the loop
} else if (command == "take") {
cin >> argument;
if (rooms[currentRoom].hasItem(argument)) {
inventory.push_back(argument);
rooms[currentRoom].removeItem(argument);
cout << "You took the " << argument << "." << endl;
} else {
cout << "There is no " << argument << " here." << endl;
}
} else if (command == "inventory") {
if (inventory.empty()) {
cout << "Your inventory is empty." << endl;
} else {
cout << "Inventory: ";
for (auto& item : inventory) {
cout << item << " ";
}
cout << endl;
}
} else if (command == "quit") {
cout << "Thanks for playing!" << endl;
running = false;
} else {
cout << "Unknown command." << endl;
}
cout << endl;
}
return 0;
}