advice on program?

Started by
1 comment, last by pulpfist 14 years, 10 months ago
first off guys i would just like to say thanks for all the help you gave me in order to learn functions :D now i would like to show you my text based game -- with! hit rates incorporated :D this is going in my portfolio which will hopefully help me get into uni in about a year and a half so please feel free to tell me what i should add to make it better :D only downside is there is a bug i am resolving when i get home tonight which stops player from playing again. By the way not yet implemented the player_health variable but will do that tonight :D anyways here it is please feel free to comment

#include <iostream>
#include <stdlib.h>
#include <time.h>

// different functions
void intro();
void combat();
void end();

// global variables
int monster_health = 500;
int player_health = 100;
char choice;
int hit_rate;
bool hit;



int main()
{
	using namespace std;
	cout << " Welcome to the text based game Warriors of Troy! \n";
	intro();
	combat();
	end();

	cin.get();
	return 0;
}
void intro()
{	
	
	char name[20];
	using std::cin;
	using std::cout;

	cout << " to begin please enter your name! \n";
	cin.getline(name , 20);
	cout << " hello and welcome : " << name << "\n";
	cout << " you are walking to the city when you are attack by a monster! ";
}
void combat()
{
	using namespace std;

	while (monster_health > 1){

	cout << endl;
	cout << " what do you do? " << endl
		<< "1) crushing blow " << endl
		<< "2) accurate lunge " << endl
		<< "3) quick lunge " << endl;
	cout << " please enter your choice \n";

	cin >> choice;
	switch(choice)
	{
		case '1' :
			cout << " Crushing blow !";
			if(rand() % 100 < 75 == true)
			{
			monster_health = monster_health - 20;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			}
			else
			{
				cout << " you missed... ";
			}
			break;
		case '2' :
			cout << " Accurate lunge! ";
			if(rand() % 100 < 75 == true)
			{
			monster_health = monster_health - 15;
			cout << " the monster now has :" << monster_health
				<< " health! ";
			}
			else
			{
				cout << " you missed...";
			}
			break;
		case '3' :
			cout << " quick lunge! ";
			if(rand() % 100 < 75 == true)
			{

			monster_health = monster_health - 10;
			cout << " the monster now has :" << monster_health
				<< " health! " << endl;
			}
			else
			{
				cout << " you missed ";
			} 
			break;
		default :
			cout << " you have entered an unavailable option! \n";
			break;
	}
	
	}	
	
}
void end()
{
	using namespace std;
	char quit_choice;
	cout << " congratulations you have killed the monster! ";
	cout << " would you like to play again? ";
	cin >> quit_choice;
	if (quit_choice == 'y' || quit_choice == 'Y')
	{
		
		intro();
		combat();
	}
	else if (quit_choice == 'n' || quit_choice == 'N')
	{

		cin.get();
	}
	else
	{
		cout << " you entered an incorrect option program will now terminate!";
		cin.get();

}
}



Game Development Tutorials - My new site that tries to teach LWJGL 3.0 and OpenGL to anyone willing to learn a little.

Advertisement
Nice and simple.

Notice how all three attack options almost do the same code? That code snippet is a perfect candidate for a function.

Use functions to make the code easier for you. If you copy/paste a piece of code more than two times consider putting it into a function.

In your case the attack switch could be simplified to this, which IMHO makes it way more readable. I'll leave the function coding to you.

switch ( choice ){  case '1' :    AttackMonster( " Crushing blow !", 75, 20 );    break;  case '2' :    AttackMonster( " Accurate lunge! ", 75, 15 );    break;  case '3' :    AttackMonster( " quick lunge! ", 75, 10 );    break;  default :    cout << " you have entered an unavailable option! \n";    break;}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

You can do like Endurion suggested, or, if you don't want to go with functions you can still simplify the switch like this
string callout = "";int damage = 0;switch(choice){	case '1' :		callout = " Crushing blow !";		damage = 20;		break;	case '2' :		callout = " Accurate lunge! ";		damage = 15;		break;	case '3' :		callout = " quick lunge! ";		damage = 10; 		break;	default :		cout << " you have entered an unavailable option! \n";		return;			}if(rand() % 100 < 75 == true){	monster_health = monster_health - damage;	cout << callout << " the monster now has :" << monster_health << " health! ";}else{	cout << " you missed... ";}


Another small thing I noticed was that you are using a raw character array to store the name of the player. Thats fair enough, but I would personally prefer to use a string.
std::string name;...std::getline(std::cin, name)

This topic is closed to new replies.

Advertisement