C Code Converted to FreeBASIC with C run time Library example

Started by stigma, Jan 18, 2023, 03:34 PM

Previous topic - Next topic

stigma

ok so first I'm learning C Programming Language and Coded or Ported Ron77 console game "Homeless - surviving the streets" to kinda simple C program (without string arrays yet)

here is the code in C :

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <String.h>
#include <time.h>


#define BEG1 "you have been begging all day for food and/or money but nothing"
#define BEG2 "you succeeded in getting some food and money from kind people"

#define WORK1 "you tried to find work but you where rejected everywhere you went"
#define WORK2 "you finded work and after 2 weeks got your frist sallery! you are off the streets! yay!"

#define PHONE1 "you phone family or friends you used to know but no one answers"
#define PHONE2 "you phone your parents - they come and get you and you are safe at home - you are off the streets - yay!"

#define DRUGS1 "you buy cheap alchohol and booze to try and forget your toubles pass out and wake up at hospital"
#define DRUGS2 "you get OD from bad alchohol and drugs and you die on the streets - RIP :( GAME OVER"

#define GAMEOVER "you die on the streets from hunger and desise and fetige - RIP"

int days = 1;
int health = 50;
int money = 55;
// bool isGameOver = false;

int message( char msg1[], char msg2[])
{
	time_t t;
	srand((unsigned) time(&t));
	int randonNumber = rand() % 2;
	int r = 0;
	if (randonNumber == 0)
	{
		printf("\n%s", msg1);
		r = 1;
	}
	else if (randonNumber == 1)
	{
		printf("\n%s", msg2);
		r = 2;
	}
	return r;
}

void opening(int health, int money, int days )
{
	printf("\nyou are on the streets for %i days\nyou have %i health: %i money", days, health, money);
}

int main()
{
	
	int input =0;
	int result = 0;
	do
	{
		system("cls");
		opening(health,money,days);
		
		printf("\nchoose what to do: \n1. beg for money or food\n2. try to find work or a job\n3. phone someone for help\n4. buy some street drugs/alcohol to forget your troubles\n");
		
		scanf("%d", &input);
		
		if (input == 1)
		{
			result = message(BEG1, BEG2);
		}
		else if (input == 2)
		{
			result = message(WORK1, WORK2);
		}
		else if (input == 3)
		{
			result = message(PHONE1, PHONE2);
		}
		else if (input == 4)
		{
			result = message(DRUGS1, DRUGS2);
		}
		
		if (input == 1 && result == 2)
		{
			health += 25;
			money += 30;
			days += 1;
		}
		else if (input == 2 && result == 2)
		{
			// isGameOver = true;
			break;
		}
		else if (input == 3 && result == 2)
		{
			// isGameOver = true;
			break;
		}
		else if (input == 4 && result == 2)
		{
			// isGameOver = true;
			break;
		}
		else
		{
			days += 1;
			health -= 5;
			money -= 10;
		}
		
		getchar();
		getchar();
	} while (health >= 0);
	getchar();
	getchar();
	getchar();
	return 0;
}

now I was wondering how or if I could convert this code into FreeBASIC using the C Run-Time Library

so here is the exact code ported to FreeBASIC using the C runtime Librarie (include "crt.bi"):

#include "crt.bi"

#define BEG1 "you have been begging all day for food and/or money but nothing"
#define BEG2 "you succeeded in getting some food and money from kind people"

#define WORK1 "you tried to find work but you where rejected everywhere you went"
#define WORK2 "you finded work and after 2 weeks got your frist sallery! you are off the streets! yay!"

#define PHONE1 "you phone family or friends you used to know but no one answers"
#define PHONE2 "you phone your parents - they come and get you and you are safe at home - you are off the streets - yay!"

#define DRUGS1 "you buy cheap alchohol and booze to try and forget your toubles pass out and wake up at hospital"
#define DRUGS2 "you get OD from bad alchohol and drugs and you die on the streets - RIP :( GAME OVER"

#define GAMEOVER "you die on the streets from hunger and desise and fetige - RIP"

dim shared as INTEGER days = 1
dim shared as INTeger health = 50
dim shared as integer money = 55

function message( msg1 as zstring, msg2 as zstring) as INTEGER
	randomize()
	dim randomNumber as integer = int(rnd*2)
	dim r as INTEGER
	
	if randomNumber = 0 then
		printf(!"%s", msg1)
		r = 1
	elseif randomNumber = 1 then
		printf(!"%s", msg2)
		r = 2
	EndIf
	return r
End Function

sub opening(health as integer, money as integer, days as integer)
	printf(!"\nyou are on the streets for %i days\nyou have %i health: %i money", days, health, money)
End Sub


function main_game() as integer
	dim as integer inpt, result
	
	WHILE health >= 0
		cls
		opening(health, money,days)
		
		printf(!"\nchoose what to do: \n1. beg for money or food\n2. try to find work or a job\n3. phone someone for help\n4. buy some street drugs/alcohol to forget your troubles\n")
		
		scanf("%d", @inpt)
		
		if inpt = 1 then
			result = message(BEG1,BEG2)
		elseif inpt = 2 then
			result = message(WORK1,WORK2)
		elseif inpt = 3 then
			result = message(PHONE1, PHONE2)
		elseif inpt = 4 then
			result = message(DRUGS1, DRUGS2)
		EndIf
		
		if (inpt = 1 and result = 2) then
			health += 25
			money += 30
			days +=1
		elseif (inpt = 2 and result = 2) then
			exit WHILE
		elseif (inpt = 3 and result = 2) then
			exit WHILE
		elseif (inpt = 4 and result = 2) then
			exit WHILE
		else
			days +=1
			health -= 5
			money -= 10
		EndIf
		
		SLEEP
		
	WEND
	
	return 0
End Function

main_game()

printf(!"\nTHAT'S ALL FOLKS!")
sleep

hope this helps someone...