Welcome to RetroCoders Community

Main Menu

homeless game in C++

Started by ron77, Jun 03, 2023, 01:57 PM

Previous topic - Next topic


Hello - someone on Discord ported my game to C++ (with msvc++ visual studio 2022). I did not write this code as I don't know it well C++, but I'm posting the C++ code here for preservation - the guy really knows well C++, but as you can see, he uses OOP and classes, so not sure if mysoft will like it or not but anyway it's my C game converted to C++ so I'm giving the code here...

in case you wish to join my private discord server "just freeBASIC and C" (where the guy tested my game and converted it to C++) here is an invite link : Just FreeBASIC and C Discord Server Invite Link

here is the code in C++ (warning!!! heavy OOP ahead! :D )

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <random>
enum action { callSomeone, seekShelter, begFood, findWork, useDrug };
class game {
	bool isOver;
	int health = 75;
	int money = 50;
	int turns = 1;
	std::string *call;
	std::string *shelter;
	std::string *beg;
	std::string *work;
	std::string *drugs;
	game() noexcept : isOver(false) { //string[0] == death :)
		call = new std::string[6];
		call[0] = "you call your family and they pick you up and let you stay for as long as you need... you are off the streets";
		call[1] = "you call your grandma and you both cry... she promises to try to get you some help... she sends you her love... you promise to keep in touch with her";
		call[2] = "you call a friend and she gives you some money and lets you stay at her place for the night... you get to have a good shower some clean cloths and eat a good meal and your back on the streets again";
		call[3] = "you call a number of a hot-line for aid to homeless persons... the come and pick you up give some food and shelter for the night you talk to a social worker and he gives you his number just in case you need it";
		call[4] = "you call a relative but no one answers... you are left alone on the streets";
		call[5] = "you call but there is no answer";

		shelter = new std::string[6];
		shelter[0] = "you sleep at the park on a bench where you freeze to death cause of the cold weather... you are now in heaven RIP";
		shelter[1] = "you go to a shelter for homeless people... you are robed from you money by violent addicts... you succeed to run away from there and spend the rest of the night at a park crying";
		shelter[2] = "you try to sleep at the park but you get arrested by a policeman... you spend few hours in jail where you are beaten by inmates... by morning you are on the street again";
		shelter[3] = "you have enough money to rent a motel room for the night... you shower and wash you cloths and sleep in a warm bed and dream you have a home";
		shelter[4] = "you sleep on the street on a corner when you wake up you find a 20 dollar bill and a note that says 'may god help you this is the little i can do'";
		shelter[5] = "you call but there is no answer";

		beg = new std::string[5];
		beg[0] = "no one gives you nothing! you're hungry and you have no choice but to look for food in the garbage... you get sick... very sick and you die on the streets from food poison... RIP";
		beg[1] = "you sit for hours and people pass you by as if you are invisible then some kind lady comes and invite you to eat at a dinner on her expense... you talk with her for two hours and tell her your story... then she says 'bye'";
		beg[2] = "you sit at a corner and people give you coins and dollars you get enough money to go by for a few days";
		beg[3] = "you sit and some men offers you work for some money and a meal you accept... you go to his house and help him paint his house then he gives you 10 dollars and a home cocked meal";
		beg[4] = "you sit all day for nothing nobody gives you anything";

		work = new std::string[3];
		work[0] = "you find work as a dishwasher at a dinner after a week you can afford a place to call home. you are off the street!";
		work[1] = "you try to find work but nobody is interested";
		work[2] = "you find work but you are fired really quick";

		drugs = new std::string[4];
		drugs[0] = "you do drugs and over dose and die on the streets RIP";
		drugs[1] = "you get drunk and stone from cheep alcohol and for a few hours you forget about all your problems";
		drugs[2] = "you do cheep street drugs and you get sick and collapse on the street... you wake up at the hospital and when you are better you are out on the street again";
		drugs[3] = "you get stones and passed out when you wake in the morning everything you had is gone - you gut mugged";
	game(std::initializer_list<int> a) = delete; //Possible to be exist after an update :)
	~game() { //destructor -> clean up memory
		delete[] call;
		delete[] shelter;
		delete[] beg;
		delete[] work;
		delete[] drugs;
	inline int Health() { return health; }
	inline int Money() { return money; }
	inline int Turns() { return turns; }
	inline bool gameOver() { return isOver; }
	std::string action(int act) {
		health -= 5;
		money -= 5;
		std::random_device rd;
		std::mt19937 gen(rd());
		int result;
		switch (act) {
		case action::callSomeone:{
			std::uniform_int_distribution<int> dis(0, 5);
			result = dis(gen);
			if (!result) isOver = true;
			return call[result];
		} break;
		case action::seekShelter:{
			std::uniform_int_distribution<int> dis(0, 5);
			result = dis(gen);
			if (!result) isOver = true;
			return shelter[result];
		} break;
		case action::begFood:{
			std::uniform_int_distribution<int> dis(0, 4);
			result = dis(gen);
			if (!result) isOver = true;
			return beg[result];
		} break;
		case action::findWork:{
			std::uniform_int_distribution<int> dis(0, 2);
			result = dis(gen);
			if (!result) isOver = true;
			return work[result];
		} break;
		case action::useDrug:{
			std::uniform_int_distribution<int> dis(0, 3);
			result = dis(gen);
			if (!result) isOver = true;
			return drugs[result];
		} break;
			std::cerr << "invalid input only 1 to 5 keys allows! try again!";
			health+= 5;
			money += 5;
			return 0;
int main() {
	class game F;
	int input = 0;
	do {
		std::cout << "days on the street: " << F.Turns() << " health: " << F.Health() << " money: " << F.Money() << "\n\n\n";
		std::cout << "press 1. for calling someone for help\n" << std::endl;
		std::cout << "press 2. for finding shelter for the day or night\n" << std::endl;
		std::cout << "press 3. for begging for food or money on the streets\n" << std::endl;
		std::cout << "press 4. for trying to find work or a job\n" << std::endl;
		std::cout << "press 5. for buying and using drugs or alcohol to forget your troubles\n" << std::endl;

		std::cin >> input;
		if ( {
			std::cin.ignore(100, '\n');
		} //if not number -> failbit -> delete 100 chars in buffer
		std::cout << F.action(input - 1);
	} while (F.Health() > 0 && !F.gameOver());
	std::cout << std::endl<<std::endl<< "GAME OVER!!!...";
	return 0;