Created
February 20, 2017 22:08
-
-
Save slice/5b040ba93fbd98a8cabceac3d738b49e to your computer and use it in GitHub Desktop.
really old code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
#include <vector> | |
#include <map> | |
enum Item { | |
/* Useless stuff */ | |
ITEM_SHOE, ITEM_SHIRT, ITEM_PANTS, | |
/* Medium valuable stuff */ | |
ITEM_GOLD_COIN, ITEM_SILVER_COIN, ITEM_BRONZE_COIN, | |
/* High valuable stuff */ | |
ITEM_GOLD_BAR, ITEM_SILVER_INGOT, ITEM_BRONZE_INGOT, | |
ITEM_DIAMOND_RING, ITEM_GOLD_RING, | |
/* This must be the last item! */ | |
ITEM_LAST | |
}; | |
class TreasureChest { | |
public: | |
TreasureChest(); | |
bool Exists; | |
bool IsLocked; | |
bool IsOpened; | |
bool IsTrapped; | |
}; | |
TreasureChest::TreasureChest() { | |
Exists = false; | |
IsLocked = false; IsOpened = false; | |
IsTrapped = false; | |
} | |
class Room { | |
public: | |
Room(); | |
~Room(); | |
int Index; | |
TreasureChest* TheTreasureChest; | |
}; | |
Room::Room() { TheTreasureChest = new TreasureChest(); } | |
Room::~Room() { delete TheTreasureChest; } | |
class Explore { | |
public: | |
Explore() { this->Health = 100; } | |
~Explore() {} | |
int Health; | |
std::vector<Item> Items; | |
int Money; | |
Room* TheRoom; | |
bool Ended; | |
int GetItemValue(Item it) { | |
if (it == ITEM_PANTS || it == ITEM_SHIRT || it == ITEM_SHOE) | |
return 1; | |
if (it == ITEM_GOLD_COIN) return 10; | |
if (it == ITEM_SILVER_COIN) return 5; | |
if (it == ITEM_BRONZE_COIN) return 3; | |
if (it == ITEM_GOLD_BAR) return 20; | |
if (it == ITEM_SILVER_INGOT) return 10; | |
if (it == ITEM_BRONZE_INGOT) return 6; | |
if (it == ITEM_DIAMOND_RING) return 50; | |
if (it == ITEM_GOLD_RING) return 40; | |
return -1; | |
} | |
Item GetRandomItem() { | |
return static_cast<Item>(rand() % ITEM_LAST); | |
} | |
void Look() { | |
std::cout << "You are now in room #" << TheRoom->Index << std::endl; | |
if (TheRoom->TheTreasureChest->Exists && !TheRoom->TheTreasureChest->IsOpened) { | |
std::cout << "There is a treasure chest in this room." << std::endl; | |
} else if (TheRoom->TheTreasureChest->Exists && TheRoom->TheTreasureChest->IsOpened) { | |
std::cout << "There is an opened treasure chest in this room." << std::endl; | |
} | |
} | |
std::string GetItemName(Item it) { | |
if (it == ITEM_LAST) return "You shouldn't have this!"; | |
if (it == ITEM_SHIRT) return "Tattered Shirt"; | |
if (it == ITEM_SHOE) return "Tattered Shoe"; | |
if (it == ITEM_PANTS) return "Tattered Pants"; | |
if (it == ITEM_DIAMOND_RING) return "Diamond Ring"; | |
if (it == ITEM_GOLD_RING) return "Golden Ring"; | |
if (it == ITEM_SILVER_INGOT) return "Silver Ingot"; | |
if (it == ITEM_GOLD_BAR) return "Gold Bar"; | |
if (it == ITEM_BRONZE_INGOT) return "Gold Ingot"; | |
if (it == ITEM_BRONZE_COIN) return "Bronze Coin"; | |
if (it == ITEM_SILVER_COIN) return "Silver Coin"; | |
if (it == ITEM_GOLD_COIN) return "Golden Coin"; | |
return "Unknown Item"; | |
} | |
void Proceed() { | |
int lastRoom = TheRoom->Index; | |
delete TheRoom; | |
TheRoom = new Room(); | |
int chestDetermineFactor = rand() % 4; | |
if (chestDetermineFactor == 1) TheRoom->TheTreasureChest->Exists = true; | |
TheRoom->Index = lastRoom + 1; | |
TheRoom->TheTreasureChest->IsOpened = false; | |
TheRoom->TheTreasureChest->IsLocked = false; | |
TheRoom->TheTreasureChest->IsTrapped = false; | |
std::cout << "Proceeded into the next room." << std::endl; | |
Look(); | |
} | |
std::map<Item, int> GetHistogramOfInventory() { | |
std::map<Item, int> histogram; | |
for (Item& i : Items) { ++histogram[i]; } | |
return histogram; | |
} | |
void TakeInventory() { | |
for (const auto& p : GetHistogramOfInventory()) { | |
std::cout << p.second << " of " << GetItemName(p.first) << std::endl; | |
} | |
} | |
void GiveRandomItems(int amount) { | |
for (int i = 0; i != amount; i++) { | |
Item it = GetRandomItem(); | |
Items.push_back(it); | |
} | |
} | |
void OpenChest() { | |
if (TheRoom->TheTreasureChest->IsOpened) { | |
std::cerr << "The open treasure chest has nothing inside." << std::endl; | |
return; | |
} | |
if (TheRoom->TheTreasureChest->Exists) { | |
std::cout << "Opened the treasure chest." << std::endl; | |
int determineEventFactor = rand() % 3; | |
if (determineEventFactor == 0 /* nothing */) { | |
std::cout << "Inside was nothing." << std::endl; | |
} else if (determineEventFactor == 1/* random items */) { | |
int items = 1 + (rand() % 8); | |
std::cout << "Got " << items << " items!" << std::endl; | |
GiveRandomItems(items); | |
} else if (determineEventFactor == 2 /* random money */) { | |
int money = 5 + (rand() % 20); | |
std::cout << "Got $" << money << "!" << std::endl; | |
Money += money; | |
} | |
TheRoom->TheTreasureChest->IsOpened = true; | |
} else { std::cerr << "What treasure chest?" << std::endl; } | |
} | |
void Start() { | |
Money = 0; | |
Ended = false; | |
srand(time(NULL)); // seed based on time. | |
std::cout << "You find yourself in a treasure dungeon." << std::endl; | |
TheRoom = new Room(); | |
TheRoom->Index = 0; | |
while (!Ended) { | |
std::string line; | |
std::cout << "[R:" << TheRoom->Index << "|HP:" << Health << "|$:" << Money << "]> "; | |
std::getline(std::cin, line); | |
if (line == "look" || line == "l") | |
Look(); | |
if (line == "proceed" || line == "p") | |
Proceed(); | |
if (line == "open chest" || line == "oc") | |
OpenChest(); | |
if (line == "exit" || line == "quit") | |
Ended = true; | |
if (line == "i" || line == "inventory") | |
TakeInventory(); | |
if (line == "_loot") | |
GiveRandomItems(35); | |
if (line == "_cache") | |
GiveRandomItems(50); | |
if (line == "_treasure") | |
GiveRandomItems(10); | |
if (line == "_rich") | |
GiveRandomItems(100); | |
if (line == "_megarich") | |
GiveRandomItems(250); | |
} | |
} | |
void End() { | |
if (TheRoom) delete TheRoom; | |
std::cout << "You black out, and you find yourself out of the treasure dungeon." << std::endl; | |
std::cout << "You escaped!" << std::endl; | |
std::map<Item, int> h = GetHistogramOfInventory(); | |
int earnings = 0; | |
for (const auto& p : h) { | |
std::cout << p.second << " of " << GetItemName(p.first) << " = $" << GetItemValue(p.first) * p.second << std::endl; | |
earnings += GetItemValue(p.first) * p.second; | |
} | |
std::cout << "Money earned: $" << Money << std::endl; | |
earnings += Money; | |
std::cout << "Health leftover: " << Health << " * 2 = " << "$" << Health * 2 << std::endl; | |
earnings += Health; | |
std::cout << "Total earnings: $" << earnings << std::endl; | |
} | |
}; | |
int main(int argc, char* argv[]) { | |
Explore explore; | |
explore.Start(); | |
explore.End(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment