commit 73b746ba8f0fadc1e0bf0008242a5ef4f005a778 Author: Kishan Takoordyal Date: Sat Nov 14 18:28:26 2020 +0400 init diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..0cba2e6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/Dungeon.cpp b/Dungeon.cpp new file mode 100644 index 0000000..11f9477 --- /dev/null +++ b/Dungeon.cpp @@ -0,0 +1,249 @@ +#include + +#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 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); + } +} diff --git a/Dungeon.h b/Dungeon.h new file mode 100644 index 0000000..d2d150a --- /dev/null +++ b/Dungeon.h @@ -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 diff --git a/GameCharacter.cpp b/GameCharacter.cpp new file mode 100644 index 0000000..4007604 --- /dev/null +++ b/GameCharacter.cpp @@ -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; +} diff --git a/GameCharacter.h b/GameCharacter.h new file mode 100644 index 0000000..6b0f9a5 --- /dev/null +++ b/GameCharacter.h @@ -0,0 +1,18 @@ +#include + +#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 diff --git a/Item.cpp b/Item.cpp new file mode 100644 index 0000000..3ec3f1b --- /dev/null +++ b/Item.cpp @@ -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; +} diff --git a/Item.h b/Item.h new file mode 100644 index 0000000..1ecf988 --- /dev/null +++ b/Item.h @@ -0,0 +1,15 @@ +#include + +#ifndef ITEM_H +#define ITEM_H + +using namespace std; + +class Item { + public: + string name; + int health, attack, defense; + Item(string, int, int, int); +}; + +#endif diff --git a/Player.cpp b/Player.cpp new file mode 100644 index 0000000..5372bba --- /dev/null +++ b/Player.cpp @@ -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 items = room->items; + + for (int i = 0; i < items.size(); i++) { + addItem(items[i]); + } +} + +void Player::changeRooms(Room * newRoom) { + previousRoom = currentRoom; + currentRoom = newRoom; +} diff --git a/Player.h b/Player.h new file mode 100644 index 0000000..5ca1d2f --- /dev/null +++ b/Player.h @@ -0,0 +1,19 @@ +#include "Room.h" + +#ifndef PLAYER_H +#define PLAYER_H + +class Player: public GameCharacter { + public: + Room *currentRoom, *previousRoom; + vector inventory; + Player(string = "", int = 0, int = 0, int = 0); + + void addItem(Item); + void increaseStats(int, int, int); + void lootRoom(Room*); + void changeRooms(Room*); +}; + +#endif + diff --git a/Room.cpp b/Room.cpp new file mode 100644 index 0000000..27d7bc0 --- /dev/null +++ b/Room.cpp @@ -0,0 +1,16 @@ +#include "Room.h" + +Room::Room(int p, bool ie, vector is, vector gcs) { + position = p; + isExit = ie; + items = is; + enemies = gcs; +} + +void Room::clearLoot() { + items.clear(); +} + +void Room::clearEnemies() { + enemies.clear(); +} diff --git a/Room.h b/Room.h new file mode 100644 index 0000000..02259d8 --- /dev/null +++ b/Room.h @@ -0,0 +1,21 @@ +#include + +#include "Item.h" +#include "GameCharacter.h" + +#ifndef ROOM_H +#define ROOM_H + +class Room { + public: + int position; + bool isExit; + vector items; + vector enemies; + Room(int = 0, bool = false, vector = vector(), vector = vector()); + + void clearLoot(); + void clearEnemies(); +}; + +#endif diff --git a/main b/main new file mode 100755 index 0000000..3f7e577 Binary files /dev/null and b/main differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5dac653 --- /dev/null +++ b/main.cpp @@ -0,0 +1,42 @@ +#include + +#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(), vector()), + Room(1, false, vector({ sword }), vector()), + Room(2, false, vector(), vector({ smallEnemy })), + Room(3, true, vector(), vector({ 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; + } + } +}