Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

text based game help

Started by emforce Jul 4, 2009 at 2:55 PM 4 replies 650+ views
Original Post
emforce
emforce
hi guys i am creating a gang based text game to work on until my new book comes. but i have come against a problem. 1) i can't seem to stop a player from purchasing items if the cost is greater than what his money is. here is the main file

#include <iostream>

class game
{
public:
	void MainMenu();
	void intro();
	void BuyEquipement();
	void Henchmen();
	void items();
	void Protection();

private:
	void SetStartEquip();
	void CheckMoney();
	int m_Money;
	int m_Dope, m_Heroin, m_Coke;
	int m_Henchmen;
	int m_Transport;
	int m_Weapons;
	int m_Business, m_Rate;
	int m_Transaction;
	int m_Cost;
};
void game::MainMenu()
{
	using std::cout;
	using std::cin;
	using std::endl;
	int MainChoice;
	SetStartEquip();


	cout << "Welcome to streets of NY! " << endl;
	cout << endl << " MAIN MENU ";
	cout << endl << " 1 - play game ";
	cout << endl << " 2 - about ";
	cout << endl << " 3 - quit ";
	cout << endl;
	cin >> MainChoice;

	switch (MainChoice)
	{
	case 1:
		intro();
		break;
	case 2:
		cout << "This text based game is a test of my coding ability" << endl;

		break;
	
	case 3:
		cin.get();
		break;
	default:
		cout << MainChoice << ": is an invalid option! ";
		break;
	}



}

void game::SetStartEquip()
{
	m_Money = 10000;
	m_Dope = 0;
	m_Heroin = 0;
	m_Coke = 0;
	m_Henchmen = 0;
	m_Transport = 0;
	m_Weapons = 0;
	m_Rate = 150;
	m_Business = 0;


}

void game::intro()
{
	using std::cout;
	using std::cin;
	using std::endl;
	
	int GameMenuChoice;


	cout << endl;
	cout << "Game Menu! " << endl;
	cout << " 1 - buy equimepent" << endl;
	cout << " 2 - henchmen " << endl;
	cout << " 3 - Protection money " << endl;
	cout << " 4 - show items " << endl;
	cin >> GameMenuChoice;

	switch (GameMenuChoice)
	{
	case 1:
		BuyEquipement();
		break;

	case 2:
		Henchmen();
		break;
	case 3:
		Protection();
		break;
	case 4:
		items();
		break;
	default:
		cout << GameMenuChoice << ": is not a valid option! ";
		break;
	}



}

void game::BuyEquipement()
{
	using std::cout;
	using std::cin;
	using std::endl;
	int DrugChoice;
	int DrugAmount;
	int GunAmount;
	int BuyMenuChoice;
	int NoOfPlanes;

	cout << "Buy Equipement menu! " << endl;
	cout << "1 - Buy Drugs" << endl;
	cout << "2 - Buy Weapons " << endl;
	cout << "3 - Buy Planes " << endl;
	cin >> BuyMenuChoice;

	switch(BuyMenuChoice)
	{
	case 1:

	
	cout << endl << "Please enter the type of drugs you wish to buy! " << endl;
	cout << "1 - Dope" << endl << "2 - Heroin " << endl << "3 - cocaine" << endl;
	cin >> DrugChoice;

	switch (DrugChoice)
	{
	case 1:
		cout << "How many units of Dope do you wish to buy? " << endl;
		cin >> DrugAmount;
		m_Cost = 100 * DrugAmount;
		CheckMoney();
		if(m_Transaction = 1)
		{
			m_Dope += DrugAmount;
			m_Money -= m_Cost;
		}
		else if(m_Transaction = 0)
		{
			cout << "Transaction Unsuccesful : Invalid funds";
		}

		
		break;
	case 2:
		cout << "How many units of Heroin do you wish to buy? " << endl;
		cin >> DrugAmount;
		
		m_Cost = 200 * DrugAmount;
		CheckMoney();
		if(m_Transaction = 1)
		{
			m_Heroin += DrugAmount;
			m_Money -= m_Cost;
		}
		else if(m_Transaction = 0)
		{
			cout << "Transaction Unsuccesful : Invalid funds";
		}
		
		
		break;
	case 3:
		cout << "How many units of Cocaine do you wish to buy? " << endl;
		cin >> DrugAmount;
		
		m_Cost = 150 * DrugAmount;
		CheckMoney();
		if(m_Transaction = 1)
		{
			m_Coke += DrugAmount;
			m_Money -= m_Cost;
		}
		else if(m_Transaction = 0)
		{
			cout << "Transaction Unsuccesful : Invalid funds";
		}

		break;
	default:
		cout << DrugChoice << ": is an invalid option!"<< endl;
		break;
	}
	break;
	case 2:

	cout << endl << "Enter how many weapons you wish to purchase: ";
	cin >> GunAmount;
	

	m_Cost = (100 * GunAmount);
	CheckMoney();
	if(m_Transaction = 1)
		{
			m_Weapons += GunAmount;
			m_Money -= m_Cost;
		}
		else if(m_Transaction = 0)
		{
			cout << "Transaction Unsuccesful : Invalid funds";
		}
	break;
	case 3:

	cout << "Enter how many planes you wish to purchase: ";
	cin >> NoOfPlanes;
	
	
	m_Cost = (5000 * NoOfPlanes);
	CheckMoney();
	if(m_Transaction = 1)
		{	
			m_Transport += NoOfPlanes;
			m_Money -= m_Cost;
		}
		else if(m_Transaction = 0)
		{
			cout << "Transaction Unsuccesful : Invalid funds";
		}
	break;
	default:
		cout << "Invalid selection";
		break;
	}
	intro();


}
void game::Henchmen()
{	
	using std::cout;
	using std::cin;
	using std::endl;
	int NoOfHench;
	int HenchmenMenu;
	
	cout << "Henchmen Menu" << endl;
	cout << "1 - hire henchmen" << endl;
	cout << "2 - collect from henchmen" << endl;
	cin >> HenchmenMenu;

	switch(HenchmenMenu)
	{
	case 1:
		cout << "Henchmen cost $30 to hire each" << endl;
	cout << "Enter how many henchmen you wish to hire: ";
	cin >> NoOfHench;
	
	
	m_Cost = (30 * NoOfHench);
	CheckMoney();
	if(m_Transaction = 1)
		{
			m_Henchmen += NoOfHench;
			m_Money -= m_Cost;
		}
		else if(m_Transaction = 0)
		{
			cout << "Transaction Unsuccesful : Invalid funds";
		}
	break;
	case 2:
		cout << "You collected: " << (m_Henchmen * 10);
		break;
	default:
		cout << "you entered an invalid option";
		break;


	}
	intro();

}
void game::items()
{
	using std::cout;
	using std::cin;
	using std::endl;
	cout << endl;
	cout << "Money: " << m_Money << endl;
	cout << "Cocaine units: " << m_Coke << endl;
	cout << "Heroin units: " << m_Heroin << endl;
	cout << "Dope units: " << m_Dope << endl;
	cout << "Number of Henchmen: " << m_Henchmen << endl;
	cout << "Number of Planes: " << m_Transport << endl;
	cout << "Number of Weapons: " << m_Weapons << endl;
	intro();

}
void game::Protection()
{
	using std::cout;
	using std::cin;
	using std::endl;	
	int ProtectionChoice;
	int NoOfBusinesses;
	int money;

	cout << "Protection Menu " << endl;
	cout << "1 - Increase amount of protected businesses" << endl;
	cout << "2 - Collect money from protected businesses" << endl;
	cout << "3 - change protection rates" << endl;
	cin >> ProtectionChoice;

	switch (ProtectionChoice)
	{
	case 1:
		cout << "Enter how many new businesses you wish to extort: ";
		cin >> NoOfBusinesses;
		m_Business += NoOfBusinesses;
		cout << "Businesses extorted: " << m_Business << endl;

		break;
	case 2:
		
		cout << "You collected: " << (m_Business * m_Rate) << endl;
		m_Money += (m_Business * m_Rate);
		break;
	case 3:
		cout << "Current rate: " << m_Rate << endl;
		cout << "Enter New Rate: ";
		cin >> m_Rate;
	break;
	default:
		cout << "Invalid option" << endl;
		break;
	}

	intro();
	



}
void game::CheckMoney()
{
	using std::cout;
	
	if (m_Money < m_Cost)
	{
		m_Transaction = 0;
	}
	else if (m_Money >= m_Cost)
	{
		
		m_Transaction = 1;
	}



}

could you please comment on my coding style as well. and tell me how i could improve :D thanks alot!
Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.
filipe
filipe
You're using the assignment operator in your conditional statements, and thus assigning m_Transaction to 1 every time.

Instead of
if(m_Transaction = 1)


Use this
if(m_Transaction == 1)


By the way, this is really a job for a bool instead of an int.
emforce
emforce
thanks so much! its now working :D
Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.
Luctus
Luctus
Quote:
Original post by emforce
could you please comment on my coding style as well. and tell me how i could improve :D thanks alot!
The most obvious problem with your code is that you've placed everything into a single monolothic class, which frankly isn't at all better than only using globals. For example m_Cost and m_Transaction really should be replaced by passing the cost as an argument to CheckMoney() which should then return a boolean representing wheter the transaction is possible or not.

The perhaps not so obvious, but very important other issue is that you never return from your functions, all of them ends with a call to another function. This problems this bring might not manifest themself immediately, but it's bad mainly for two reasons: 1) It's bad style and 2) You'll exhaust you stack space if you play for a long time.

Consider this: every time you make a function, in addition to a bunch of book keeping data such as where in the code to return after the function, the parameters to the function and the machine registers, your program will store all the local variables in what's called the stack memory. When a function returns, the space all those things took up will be avaliable again. But if you never let your functions return, the program will just keep piling on more things on the stack until your run out of memory.

Instead, you should have a data structure containing your game state, such as the amount of items owned, protection rate etc. which you then update in each iteration of your game loop. Something like
while(true){	// Display choices	// Perforform action and modify the current state	// Display the current state of the game}


The third thing that to consider is using a more data-driven approach. That is, instead of having a lot of code-branches with similar but specific functionality, you create a generalized function or class that takes different in-data that governs the specific behaviour. If you look at your code, the functions for buying stuff all follows a similar pattern: Present options->Retrieve choice->Add commodity/Reduce money. You could instead have a single function that took a data structure containing the choices, commodities avaliable and prices. That would make it much easier to extend your game with new features, load data from disk and make it easier to follow the logic when reading the code.

-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
daviangel
daviangel
Might want to spend some time looking into exceptions to make your program more robust. As it is right now it's too easy for someone to crash intentionally or inadvertantly.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
swiftcoder
swiftcoder
Quote:
Original post by Luctus
The perhaps not so obvious, but very important other issue is that you never return from your functions, all of them ends with a call to another function. This problems this bring might not manifest themself immediately, but it's bad mainly for two reasons: 1) It's bad style and 2) You'll exhaust you stack space if you play for a long time.

Consider this: every time you make a function, in addition to a bunch of book keeping data such as where in the code to return after the function, the parameters to the function and the machine registers, your program will store all the local variables in what's called the stack memory. When a function returns, the space all those things took up will be avaliable again. But if you never let your functions return, the program will just keep piling on more things on the stack until your run out of memory.
Note that you may encounter this style of programming very often in texts aimed at languages with a greater focus on recursion (such as Scheme or LISP), where this is the normal way of doing things. However, those languages have an optimisation called 'tail recursion', to prevent them running out of stack space.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.