Compare commits

...

No commits in common. "81d3dd970aa26740bf9722a5e594c7a9d2e27c4e" and "6a83e9472d50e7687005920caf8bff22b399cb4b" have entirely different histories.

14 changed files with 470 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.vscode/

249
Dungeon.cpp Normal file
View File

@ -0,0 +1,249 @@
#include <iostream>
#include "Dungeon.h"
using namespace std;
Dungeon::Dungeon(Player p) {
player = p;
}
void Dungeon::printActions(int numActions, string actions[]) {
cout << "\nChoose an action:\n";
for (int i = 0; i < numActions; i++) {
cout << actions[i] << "\n";
}
}
void Dungeon::handleLootActions(Room *room) {
vector<Item> items = room->items;
int size = room->items.size();
player.lootRoom(room);
room->clearLoot();
for (int i = 0; i < size; i++) {
cout << "You open the chest and find a " << items[i].name << "\n";
cout << "Your health is now " << player.currentHealth << ", your attack is now " << player.attack << ", your defense is now " << player.defense << "\n";
}
}
void Dungeon::handleFightActions(GameCharacter *enemy) {
string actions[] = {"a. Attack", "b. Retreat"};
while (true) {
printActions(2, actions);
string input;
cin >> input;
if (input == "a") {
int damage = enemy->takeDamage(player.attack);
cout << "Your attack does " << damage << " damage.\n";
} else if (input == "b") {
player.changeRooms(player.previousRoom);
enterRoom(player.currentRoom);
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
if (enemy->isDead()) {
cout << "You win! You have defeated the enemy " << enemy->name << ".\n";
player.increaseStats(10, 5, 5);
player.currentRoom->clearEnemies();
return;
}
int damage = player.takeDamage(enemy->attack);
cout << "The " << enemy->name << " attacked and you took " << damage << " damage.\n";
cout << "You now have " << player.currentHealth << " health.\n";
if (player.isDead()) {
cout << "You have been defeated...";
return;
}
}
}
void Dungeon::handleEmptyRoom(Room *room) {
cout << "You enter the room but it is empty.\n";
string actions[] = {"a. Move to another room"};
while (true) {
printActions(1, actions);
string input;
cin >> input;
if (input == "a") {
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
}
void Dungeon::handleRoomWithChest(Room *room) {
cout << "You enter the room and see a large chest in the middle.\n";
string actions[] = {"a. Loot the chest", "b. Move to another room"};
while (true) {
printActions(2, actions);
string input;
cin >> input;
if (input == "a") {
handleLootActions(room);
return;
} else if (input == "b") {
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
}
void Dungeon::handleRoomWithEnemy(Room *room) {
GameCharacter enemy = room->enemies.front();
cout << "You enter the room and see a " << enemy.name << " in the middle.\n";
string actions[] = {"a. Fight the " + enemy.name, "b. Go back to previous room"};
while (true) {
printActions(2, actions);
string input;
cin >> input;
if (input == "a") {
handleFightActions(&enemy);
return;
} else if (input == "b") {
player.changeRooms(player.previousRoom);
enterRoom(player.currentRoom);
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
}
void Dungeon::enterRoom(Room *room) {
if (room->enemies.size() > 0) {
handleRoomWithEnemy(room);
} else if (room->items.size() > 0) {
handleRoomWithChest(room);
} else {
handleEmptyRoom(room);
}
}
void Dungeon::handleMovementActions(Room *room) {
while (true){
if (room->position == 0) {
string actions[] = {"a. Move right", "b. Move down"};
printActions(2, actions);
string input;
cin >> input;
if (input == "a") {
player.changeRooms(&rooms[1]);
return;
} else if (input == "b") {
player.changeRooms(&rooms[2]);
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
else if (room->position == 1) {
string actions[] = {"a. Move left"};
printActions(1, actions);
string input;
cin >> input;
if (input == "a") {
player.changeRooms(&rooms[0]);
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
else if (room->position == 2) {
string actions[] = {"a. Move up", "b. Move right"};
printActions(2, actions);
string input;
cin >> input;
if (input == "a") {
player.changeRooms(&rooms[0]);
return;
} else if (input == "b") {
player.changeRooms(&rooms[3]);
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
else {
string actions[] = {"a. Move left"};
printActions(1, actions);
string input;
cin >> input;
if (input == "a") {
player.changeRooms(&rooms[2]);
return;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
}
}
int Dungeon::performEndGameLogic() {
while (true) {
string actions[] = {"a. Yes", "b. No"};
printActions(2, actions);
string input;
cin >> input;
if (input == "a") {
return 1;
} else if (input == "b") {
return 0;
} else {
cout << "Incorrect choice. Please try again.\n";
}
}
}
int Dungeon::runDungeon() {
cout << "Welcome to the dungeon! Inside you will find treasure but also enemies. Enter at your peril!\n";
player.currentRoom = &rooms[0];
player.previousRoom = &rooms[0];
while (true) {
enterRoom(player.currentRoom);
if (player.isDead()) {
cout << "Game over! Try again?\n";
return performEndGameLogic();
} else {
if (player.currentRoom->isExit) {
if (player.currentRoom->enemies.size() <= 0) {
cout << "You win! Play again?\n";
return performEndGameLogic();
}
}
}
handleMovementActions(player.currentRoom);
}
}

26
Dungeon.h Normal file
View File

@ -0,0 +1,26 @@
#include "Player.h"
#ifndef DUNGEON_H
#define DUNGEON_H
class Dungeon{
public:
Player player;
Room rooms[4];
Dungeon(Player);
int runDungeon();
void enterRoom(Room *);
void handleEmptyRoom(Room *);
void handleRoomWithChest(Room *);
void handleRoomWithEnemy(Room *);
void handleLootActions(Room *);
void handleFightActions(GameCharacter *);
void handleMovementActions(Room *);
void printActions(int, string[]);
int performEndGameLogic();
};
#endif

24
GameCharacter.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "GameCharacter.h"
GameCharacter::GameCharacter(string n, int h, int a, int d) {
name = n;
maxHealth = h;
currentHealth = h;
attack = a;
defense = d;
}
int GameCharacter::takeDamage(int amount) {
int damage = amount - defense;
if (damage < 0) {
damage = 0;
}
currentHealth -= damage;
return damage;
}
bool GameCharacter::isDead() {
return currentHealth <= 0;
}

18
GameCharacter.h Normal file
View File

@ -0,0 +1,18 @@
#include <string>
#ifndef GAMECHARACTER_H
#define GAMECHARACTER_H
using namespace std;
class GameCharacter{
public:
string name;
int maxHealth, currentHealth, attack, defense;
GameCharacter(string, int, int, int);
int takeDamage(int);
bool isDead();
};
#endif

8
Item.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "Item.h"
Item::Item(string n, int h, int a, int d) {
name = n;
health = h;
attack = a;
defense = d;
}

15
Item.h Normal file
View File

@ -0,0 +1,15 @@
#include <string>
#ifndef ITEM_H
#define ITEM_H
using namespace std;
class Item {
public:
string name;
int health, attack, defense;
Item(string, int, int, int);
};
#endif

31
Player.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "Player.h"
Player::Player(string n, int h, int a, int d): GameCharacter(n, h, a, d) {
Item dagger = Item("Dagger", 0, 5, 0);
addItem(dagger);
}
void Player::addItem(Item item) {
inventory.push_back(item);
increaseStats(item.health, item.attack, item.defense);
}
void Player::increaseStats(int h, int a, int d) {
currentHealth += h;
maxHealth += h;
attack += a;
defense += d;
}
void Player::lootRoom(Room * room) {
vector<Item> items = room->items;
for (int i = 0; i < items.size(); i++) {
addItem(items[i]);
}
}
void Player::changeRooms(Room * newRoom) {
previousRoom = currentRoom;
currentRoom = newRoom;
}

19
Player.h Normal file
View File

@ -0,0 +1,19 @@
#include "Room.h"
#ifndef PLAYER_H
#define PLAYER_H
class Player: public GameCharacter {
public:
Room *currentRoom, *previousRoom;
vector<Item> inventory;
Player(string = "", int = 0, int = 0, int = 0);
void addItem(Item);
void increaseStats(int, int, int);
void lootRoom(Room*);
void changeRooms(Room*);
};
#endif

View File

@ -1,2 +0,0 @@
# text-dungeon-crawler

16
Room.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "Room.h"
Room::Room(int p, bool ie, vector<Item> is, vector<GameCharacter> gcs) {
position = p;
isExit = ie;
items = is;
enemies = gcs;
}
void Room::clearLoot() {
items.clear();
}
void Room::clearEnemies() {
enemies.clear();
}

21
Room.h Normal file
View File

@ -0,0 +1,21 @@
#include <vector>
#include "Item.h"
#include "GameCharacter.h"
#ifndef ROOM_H
#define ROOM_H
class Room {
public:
int position;
bool isExit;
vector<Item> items;
vector<GameCharacter> enemies;
Room(int = 0, bool = false, vector<Item> = vector<Item>(), vector<GameCharacter> = vector<GameCharacter>());
void clearLoot();
void clearEnemies();
};
#endif

BIN
main Executable file

Binary file not shown.

42
main.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <iostream>
#include "Dungeon.cpp"
#include "GameCharacter.cpp"
#include "Player.cpp"
#include "Item.cpp"
#include "Room.cpp"
using namespace std;
int main() {
cout << "Welcome to our dungeon crawler! What is your name?\n";
string name;
cin >> name;
Player player = Player(name, 100, 30, 5);
Item sword = Item("Sword", 0, 50, 0);
GameCharacter smallEnemy = GameCharacter("Pilon", 30, 10, 5);
GameCharacter bigEnemy = GameCharacter("Grand Pilon", 75, 60, 25);
Room rooms[] = {
Room(0, false, vector<Item>(), vector<GameCharacter>()),
Room(1, false, vector<Item>({ sword }), vector<GameCharacter>()),
Room(2, false, vector<Item>(), vector<GameCharacter>({ smallEnemy })),
Room(3, true, vector<Item>(), vector<GameCharacter>({ bigEnemy})),
};
Dungeon dungeon = Dungeon(player);
dungeon.rooms[0] = rooms[0];
dungeon.rooms[1] = rooms[1];
dungeon.rooms[2] = rooms[2];
dungeon.rooms[3] = rooms[3];
while (true) {
int result = dungeon.runDungeon();
if (result == 0) {
cout << "Goodbye!";
return 0;
}
}
}