Skip to content

Instantly share code, notes, and snippets.

@dmamills
Created July 9, 2023 17:06
Show Gist options
  • Save dmamills/83c2672152e9b7aea258f4687605b609 to your computer and use it in GitHub Desktop.
Save dmamills/83c2672152e9b7aea258f4687605b609 to your computer and use it in GitHub Desktop.
k-mart dopewars clone
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <string>
struct Drug {
std::string name;
int low_range;
int high_range;
int price;
int quantity;
};
struct City {
std::string name;
std::vector<Drug> drugs;
};
struct Player {
int day;
int money;
int debt;
bool has_gun;
bool in_custody;
int health;
std::map<std::string, Drug> drugs;
};
const int NUM_CITIES = 5;
const int NUM_DRUGS = 5;
const std::string CITY_NAMES[NUM_CITIES] = { "Toronto", "Vancouver", "Halifax", "Waterloo", "Niagra Falls" };
const std::string DRUG_NAMES[NUM_DRUGS] = { "Spice", "SoyKaf", "Black Meat", "Beta-phenethylamine", "Jenkem" };
const int DRUG_LOW_RANGE[NUM_DRUGS] = { 10, 50, 70, 80, 90 };
const int DRUG_HIGH_RANGE[NUM_DRUGS] = { 100, 200, 400, 500, 600 };
void print_intro() {
std::cout << "dope warz\n\n";
}
int get_valid_input() {
int choice;
int valid = 0;
while (!valid) {
std::cin >> choice;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else { valid = 1; }
}
return choice;
}
void print_menu(Player* player, std::string current_city) {
std::cout << "You are in: " << current_city << "\n";
std::cout << "It is day " << player->day << "\n";
bool owes_money = player->debt > 0;
if (owes_money) {
std::cout << "You have " << 30 - player->day << " days to pay off your debt of $" << player->debt << "\n";
}
else {
std::cout << "\n";
}
std::cout << "You have $" << player->money << "\n";
std::cout << "What would you like to do?\n";
std::cout << "1. Buy\n2. Sell\n3. Travel\n4. Pay Debt\n5. Inventory\n6. Quit\n";
}
City generate_city(std::string name) {
City city;
city.name = name;
for (int i = 0; i < NUM_DRUGS; i++) {
Drug drug;
drug.name = DRUG_NAMES[i];
drug.low_range = DRUG_LOW_RANGE[i];
drug.high_range = DRUG_HIGH_RANGE[i];
drug.price = rand() % (drug.high_range - drug.low_range) + drug.low_range;
drug.quantity = rand() % 100;
city.drugs.push_back(drug);
}
return city;
}
std::string travel_to_city() {
for (int i = 0; i < NUM_CITIES; i++) {
std::cout << "#" << i+1 << ": " << CITY_NAMES[i] << "\n";
}
int choice;
std::cin >> choice;
return CITY_NAMES[choice-1];
}
void print_city_prices(City city) {
for (int i = 0; i < NUM_DRUGS; i++) {
std::cout << "#" << i+1 << ": " << city.drugs[i].name << " Price: $" << city.drugs[i].price << " Quantity: " << city.drugs[i].quantity << "\n";
}
}
void print_inventory(Player* player) {
std::cout << "You have $" << player->money << "\n";
std::cout << "You owe $" << player->debt << "\n";
for (int i = 0; i < NUM_DRUGS; i++) {
std::cout << "#" << i+1 << ": " << player->drugs[DRUG_NAMES[i]].name << " Quantity: " << player->drugs[DRUG_NAMES[i]].quantity << "\n";
}
}
int get_drug_input() {
int choice = get_valid_input();
if (choice-1 > NUM_DRUGS) {
std::cout << "Invalid choice!\n";
return -1;
}
return choice-1;
}
void buy_drugs(City city, Player* player) {
print_city_prices(city);
std::cout << "You have $" << player->money << "\n";
std::cout << "Which drug would you like to buy?\n";
int choice = get_drug_input();
if (choice == -1) {
return;
}
std::cout << "How much " << city.drugs[choice].name << " would you like to buy?\n";
int quantity;
std::cin >> quantity;
int total_price = city.drugs[choice].price * quantity;
std::cout << "Total price: $" << total_price << "\n";
if (total_price > player->money) {
std::cout << "You don't have enough money!\n";
} else if (quantity > city.drugs[choice].quantity) {
std::cout << "There isn't enough drugs!\n";
} else {
player->money -= total_price;
player->drugs[city.drugs[choice].name].quantity += quantity;
std::cout << "You bought the drugs!\n";
}
}
void sell_drugs(City city, Player* player) {
print_city_prices(city);
int choice = get_drug_input();
if (choice == -1) {
return;
}
std::cout << "You have " << player->drugs[city.drugs[choice].name].quantity << " units of " << city.drugs[choice].name << "\nHow many would you like to sell?\n";
int quantity;
std::cin >> quantity;
if (quantity > player->drugs[city.drugs[choice].name].quantity) {
std::cout << "You don't have enough " << city.drugs[choice].name << "!\n";
}
else {
player->money += city.drugs[choice].price * quantity;
player->drugs[city.drugs[choice].name].quantity -= quantity;
std::cout << "You sold the drugs!\n";
}
}
int pay_debt(Player* player) {
std::cout << "You have $" << player->money << "\n";
std::cout << "You owe $" << player->debt << "\n";
std::cout << "How much would you like to pay?\n";
int amount = get_valid_input();
if (amount > player->money) {
std::cout << "You don't have enough money!\n";
return -1;
}
player->money -= amount;
player->debt -= amount;
if (player->debt == 0) {
std::cout << "Your loan shark smiles and says: 'Ya did good kid!'\n";
return 1;
}
return 0;
}
void create_player(Player* player) {
player->money = 2000;
player->debt = 5500;
player->day = 1;
player->has_gun = 0;
player->in_custody = 0;
player->health = 10;
for (int i = 0; i < NUM_DRUGS; i++)
{
Drug drug;
drug.name = DRUG_NAMES[i];
drug.quantity = 0;
player->drugs[drug.name] = drug;
}
}
int is_dead_beat(Player* player) {
if (player->health < 1) {
std::cout << "Just another dead gangbanger on the street. RIP homie.\n";
return -1;
}
int quantity = 0;
for (auto i = player->drugs.begin(); i != player->drugs.end(); i++) {
quantity += i->second.quantity;
}
if (player->money == 0 && quantity == 0) {
std::cout << "You're a dead beat!\n";
return -1;
}
bool owes_money = player->debt > 0;
if (player->day == 30 && owes_money) {
std::cout << "You didn't pay off your debt in time! You lose!\n";
return -1;
}
return 0;
}
void rob_player(Player* player) {
int player_money = player->money;
int amount = rand() % player->money + 1;
std::cout << "Oof! You were robbed! $" << amount << " was stolen!\n";
player->money -= amount;
}
void find_stash(Player* player) {
int drug_index = rand() % NUM_DRUGS;
int quantity = rand() % 10 + 1;
std::cout << "You stumbled upon a hidden stash! " << quantity << " units of " << player->drugs[DRUG_NAMES[drug_index]].name << "!\n";
player->drugs[player->drugs[DRUG_NAMES[drug_index]].name].quantity += quantity;
}
void market_change(City* city) {
int drug_index = rand() % NUM_DRUGS;
int up_or_down = rand() % 2;
std::cout << "The market for " << city->drugs[drug_index].name << " has " << (up_or_down == 0 ? "increased" : "decreased") << "!\n";
int current_price = city->drugs[drug_index].price;
int new_price = current_price + (rand() % 10 + 1) * (up_or_down == 0 ? 1 : -1);
city->drugs[drug_index].price = new_price;
}
void gun_sale(Player* player) {
std::cout << "Psst. The streets are dangerous, you want a piece? Only $500\n";
std::cout << "1. Yes\n";
std::cout << "2. No\n";
int choice;
std::cin >> choice;
if (choice != 1) { return; }
if (player->money < 500) {
std::cout << "You don't have enough money!\n";
return;
}
player->money -= 500;
player->has_gun = 1;
std::cout << "You bought a gun!\n";
}
void police_encounter(Player* player) {
std::cout << "You hear the voice of the long arm of the law: 'Freeze!'\n";
bool in_combat = true;
while (in_combat) {
int coin_flip = rand() % 2;
if (player->has_gun) {
std::cout << "1. Shoot\n2. Run\n3. Surrender\n";
int choice;
std::cin >> choice;
if (choice == 1) {
std::cout << "You yell 'Eat lead piggy!' and blast a bullet towards the cop, ";
if (coin_flip == 0) {
std::cout << "striking him down.\n";
in_combat = false;
} else {
std::cout << "but miss!\n";
}
} else if (choice == 2) {
if (coin_flip == 0) {
std::cout << "You ran away!\n";
in_combat = false;
}else {
std::cout << "You tried to run away, but the cop keeps pace and fires his gun, hitting you!\n";
player->health -= 1;
if (player->health < 1) {
in_combat = false;
}
}
} else if (choice == 3) {
std::cout << "You surrendered!\n";
player->in_custody = true;
in_combat = false;
}
} else {
std::cout << "1. Run\n2.Surrender\n";
int choice;
std::cin >> choice;
if (choice == 1) {
if (coin_flip == 0) {
std::cout << "You ran away!\n";
in_combat = false;
}
else {
std::cout << "You tried to run away, but the cop keeps pace and fires his gun, hitting you!\n";
player->health -= 1;
if (player->health < 1) {
in_combat = false;
}
}
}
else if (choice == 2) {
std::cout << "You surrendered!\n";
player->in_custody = true;
in_combat = false;
}
}
}
}
void random_event(Player* player, City* city) {
int chance = rand() % 5 + 1;
if (chance != 1) { return; }
int event_type = rand() % 5 + 1;
switch (event_type) {
case 1:
police_encounter(player);
break;
case 2: {
rob_player(player);
break;
}
case 3: {
find_stash(player);
break;
}
case 4: {
market_change(city);
break;
}
case 5: {
if (player->has_gun) return;
gun_sale(player);
break;
}
}
}
int handle_jail(Player* player) {
std::cout << "The cops haul you off to jail. They confiscate your stash. You are locked up for two days, before you have the chance to pay $200 for bail.\n";
player->day += 2;
if (player->money < 200) {
std::cout << "You don't have enough money to pay bail. You are gonna have do the time Sorry homie.\n";
return -1;
}
else {
player->money -= 200;
for (auto i = player->drugs.begin(); i != player->drugs.end(); i++) {
i->second.quantity = 0;
}
}
return 0;
}
int main() {
print_intro();
bool quit = false;
std::string current_city = CITY_NAMES[0];
Player* player = new Player;
create_player(player);
City city = generate_city(current_city);
while (!quit) {
int deadbeat = is_dead_beat(player);
if (deadbeat == -1) {
quit = true;
continue;
}
print_menu(player, current_city);
int choice = get_valid_input();
switch (choice) {
case 1:
buy_drugs(city, player);
break;
case 2:
sell_drugs(city, player);
break;
case 3:
current_city = travel_to_city();
city = generate_city(current_city);
random_event(player, &city);
if (player->in_custody) {
int res = handle_jail(player);
if(res == -1) { quit = true; }
break;
}
player->day += 1;
break;
case 4: {
int res = pay_debt(player);
if (res == 1) {
quit = true;
}
break;
}
case 5:
print_inventory(player);
break;
case 6:
std::cout << "You chose to quit. I don't blame ya this life aint for everyone.\n";
quit = true;
break;
default:
std::cout << "Invalid choice\n";
break;
}
}
delete player;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment