News:

Welcome to RetroCoders Community

Main Menu

homeless game first version in C

Started by ron77, Jun 02, 2023, 01:59 PM

Previous topic - Next topic

ron77

okay so I kinda ported my freebasic old console game "Homeless - surviving the Streets" to C just as an exercise 8)

I don't think I could survive on the streets as a homeless person, so I hope I'll never be in that situation.

Anyhow, it still has some work to be done on it (more info in the terminal?), but the main thing is the engine works - by the way, this version is for Windows only (not portable) for Linux. It needs some adjustments.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
#include <windows.h>

int health = 75, money =50, turns = 1;

char* call[] = {
	"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",
	"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",
	"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",
	"you call a relative but no one answers... you are left alone on the streets",
	"you call your family and they pick you up and let you stay for as long as you need... you are off the streets",
	"you call but there is no answer"

}; // 0 - 5 (4 game over you won)

char* shelther[] = {
	"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",
	"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",
	"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",
	"you sleep at the park on a bench where you freeze to death cause of the cold weather... you are now in heaven RIP",
	"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'",
	"you are arrested by the police and now instead surviving on the street you have to survive being in jail",

}; // 0 -5 (3 game over you die)

char* beg[] = {
	"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'",
	"you sit at a corner and people give you coins and dollars you get enough money to go by for a few days",
	"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",
	"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",
	"you sit all day for nothing nobody gives you anything"

}; // 0 - 4 (3 game over you die)

char* work[] = {
	"you try to find work but nobody is interested",
	"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!",
	"you find work but you are fired really quick",

}; // 0 - 2 (1 game over you won)

char* drugs[] = {
	"you get drunk and stone from cheep alcohol and for a few hours you forget about all your problems",
	"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",
	"you get stones and passed out when you wake in the morning everything you had is gone - you gut mugged",
	"you do drugs and over dose and die on the streets RIP"
	
}; //0 - 3 ( 3 game over you die)

bool isOver = false;

char* callSomeone() {
	srand(time(0));
	int result = rand()%6;
	if (result == 4) {
		isOver = true;
		return call[result];
	}
	return call[result];
}

char* seekShelther() {
	srand(time(0));
	int result = rand()%6;
	if (result == 3) {
		isOver = true;
		return shelther[result];
	}
	return shelther[result];
}

char* begFood() {
	srand(time(0));
	int result = rand()%5;
	if (result == 3) {
		isOver = true;
		return beg[result];
	}
	return beg[result];
}

char* findWork() {
	srand(time(0));
	int result = rand()%3;
	if (result == 1) {
		isOver = true;
		return work[result];
	}
	return work[result];
}

char* useDrugs() {
	srand(time(0));
	int result = rand()%4;
	if (result == 3) {
		isOver = true;
		return drugs[result];
	}
	return drugs[result];
}

int main() {
	
	int input = 0;
	
	while (health > 0 && isOver == false) {
		
		system("cls");
		
		printf("days on the street: %i health: %i money: %i\n\n\n", turns, health, money);
		printf("press 1. for begging for food or money on the streets\n\n");
		printf("press 2. for calling someone for help\n\n");
		printf("press 3. for finding shelter for the day or night\n\n");
		printf("press 4. for trying to find work or a job\n\n");
		printf("press 5. for buying and using drugs or alcohol to forget your troubles\n\n");
		
		scanf("%i", &input);
		
		if (input == 1) {
			printf("%s", begFood());
		}else if (input == 2) {
			printf("%s", callSomeone());
		}else if (input == 3) {
			printf("%s", seekShelther());
		}else if (input == 4) {
			printf("%s", findWork()); 
		}else if (input == 5) {
			printf("%s", useDrugs());
		}else {
			printf("invalid input only 1 to 5 keys allows! try again!");
		}
		
		turns++;
		health -= 5;
		money -= 5;
		
		Sleep(5000);
		
	 
	
	}
	
	printf("\n\nGAME OVER!!!...");
	
	return 0;
}

ron77

okay here is a much better version that can even run on visual studio msvc

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
#include <windows.h>

int health = 75, money =50, turns = 1;

char* call[] = {
	"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",
	"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",
	"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",
	"you call a relative but no one answers... you are left alone on the streets",
	"you call your family and they pick you up and let you stay for as long as you need... you are off the streets",
	"you call but there is no answer"

}; // 0 - 5 (4 game over you won)

char* shelther[] = {
	"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",
	"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",
	"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",
	"you sleep at the park on a bench where you freeze to death cause of the cold weather... you are now in heaven RIP",
	"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'",
	"you are arrested by the police and now instead surviving on the street you have to survive being in jail",

}; // 0 -5 (3 game over you die)

char* beg[] = {
	"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'",
	"you sit at a corner and people give you coins and dollars you get enough money to go by for a few days",
	"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",
	"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",
	"you sit all day for nothing nobody gives you anything"

}; // 0 - 4 (3 game over you die)

char* work[] = {
	"you try to find work but nobody is interested",
	"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!",
	"you find work but you are fired really quick",

}; // 0 - 2 (1 game over you won)

char* drugs[] = {
	"you get drunk and stone from cheep alcohol and for a few hours you forget about all your problems",
	"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",
	"you get stones and passed out when you wake in the morning everything you had is gone - you gut mugged",
	"you do drugs and over dose and die on the streets RIP"
	
}; //0 - 3 ( 3 game over you die)

bool isOver = false;

char* callSomeone() {
	srand ( (unsigned int)time(NULL) );
	int result = rand()%6;
	if (result == 4) {
		isOver = true;
		return call[result];
	}
	return call[result];
}

char* seekShelther() {
	srand ( (unsigned int)time(NULL) );
	int result = rand()%6;
	if (result == 3) {
		isOver = true;
		return shelther[result];
	}
	return shelther[result];
}

char* begFood() {
	srand ( (unsigned int)time(NULL) );
	int result = rand()%5;
	if (result == 3) {
		isOver = true;
		return beg[result];
	}
	return beg[result];
}

char* findWork() {
	srand ( (unsigned int)time(NULL) );
	int result = rand()%3;
	if (result == 1) {
		isOver = true;
		return work[result];
	}
	return work[result];
}

char* useDrugs() {
	srand ( (unsigned int)time(NULL) );
	int result = rand()%4;
	if (result == 3) {
		isOver = true;
		return drugs[result];
	}
	return drugs[result];
}

int main() {
	
	int input = 0;
	
	while (health > 0 && isOver == false) {
		
		system("cls");
		
		printf("days on the street: %i health: %i money: %i\n\n\n", turns, health, money);
		printf("press 1. for begging for food or money on the streets\n\n");
		printf("press 2. for calling someone for help\n\n");
		printf("press 3. for finding shelter for the day or night\n\n");
		printf("press 4. for trying to find work or a job\n\n");
		printf("press 5. for buying and using drugs or alcohol to forget your troubles\n\n");
		
		scanf("%i", &input);
		char *pR = NULL;
		if (input == 1) {
			pR =  begFood();
			// printf("%s", begFood());
		}else if (input == 2) {
			pR = callSomeone();
			// printf("%s", callSomeone());
		}else if (input == 3) {
			pR = seekShelther();
			// printf("%s", seekShelther());
		}else if (input == 4) {
			pR = findWork();
			// printf("%s", findWork()); 
		}else if (input == 5) {
			pR = useDrugs();
			// printf("%s", useDrugs());
		}else {
			printf("invalid input only 1 to 5 keys allows! try again!");
		}
		
		printf("%s", pR);
		
		turns++;
		health -= 5;
		money -= 5;
		
		Sleep(5000);
		
	 
	
	}
	
	printf("\n\nGAME OVER!!!...");
	
	return 0;
}