tips for a newb [c++]

Started by
5 comments, last by emforce 14 years, 10 months ago
hi guys this is my simple game that i have been working on and was wondering what you guys thought i could do to make it better. its not quite finished i am just going to bed and wanted to find out how i am doing so far :D


// simple text role playing games

#include <iostream>
using namespace std;


int main()
{	
char quit_choice;			// outside game loop or else quit_choice not recognised
int menu_choice;
 

cout << " hello and welcome to warriors vs monsters rpg game created by me Elliot Forbes";
cout << " 1 - start new game \n";
cout << " 2 - about game \n";
cout << " 3 - updates \n";
cout << " 4 - quit \n";
cin >> menu_choice;

switch (menu_choice)
{
case 1 :

	
	do {					// game loop hopefully
	int choice;
	int player_health = 100;
 int player_damage_accurate = 15;
 int player_damage_crushing = 20;
 int player_damage_quick = 10;
 int monster_health = 50;
 int monster_damage = 10;
	
	char name[20];
	cout << " to start this adventure please type in your name ";
	cin.getline(name, 20);
	cout << "\n";
	cout << " hello " << name;		// add a bit of depth to game by including personal names
	cout << " you are a warrior and a monster attacks you ";
	cout << endl << " what type of attack do you want to use? \n "
		<< " you have " << player_health << " to begin with \n"
		<< " 1) accurate lunge \n"  
		<< " 2) crushing blow \n"  
		<< " 3) quick blow  \n"  ;
	
	cin >> choice;				
	if (choice == 1)
	{	
		monster_health = monster_health - player_damage_accurate;
		cout << " the boar is hurt and now has " <<
		monster_health << "\n";
		cout << " the boar manages to damage you!\n ";
		player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";

		do
		{
			cout << " the monster is still not dead ";
		cout << " what type of attack do you want to use? ";
		cout <<  " 1) accurate lunge \n"  
		<< " 2) crushing blow \n"  
		<< " 3) quick blow  \n"  ;
		cin >> choice;
		if (choice == 1)
		{
	cout << " you choose accurate lunge \n";
	monster_health = monster_health - player_damage_accurate;
	cout << " the monster now only has " << monster_health
		<< " health \n";

		}
		else if (choice == 2)
		{	
			cout << " you choose crushing blow \n ";
			monster_health = monster_health - player_damage_crushing;
			cout << " the monster now has " << monster_health << " health! \n";
			player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";
		}
		else 
		{	
			cout << " you have choosen quick blow! ";
			monster_health = monster_health - player_damage_quick;
			cout << " the monster now has " << monster_health << " health! \n";
			player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";

		}
		}while (monster_health > 0);
	} 
	else if (choice == 2)
	{	
		cout << " you go for the crushing blow and miss! ";
		cout << "\n";
		cout << " the monster kills you! ";
		cout << " better luck next time " << name;
		do
		{
			cout << " the monster is still not dead ";
		cout << " what type of attack do you want to use? ";
		cout <<  " 1) accurate lunge \n"  
		<< " 2) crushing blow \n"  
		<< " 3) quick blow  \n"  ;
		cin >> choice;
		if (choice == 1)
		{
	cout << " you choose accurate lunge \n";
	monster_health = monster_health - player_damage_accurate;
	cout << " the monster now only has " << monster_health
		<< " health \n";
player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";
		}
		else if (choice == 2)
		{	
			cout << " you choose crushing blow \n ";
			monster_health = monster_health - player_damage_crushing;
			cout << " the monster now has " << monster_health << " health! \n";
player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";
		}
		else 
		{	
			cout << " you have choosen quick blow! ";
			monster_health = monster_health - player_damage_quick;
			cout << " the monster now has " << monster_health << " health! \n";

player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";
		}
		}while (monster_health > 0);
	}
	else
	{	
	
		do
		{
			cout << " the monster is still not dead ";
		cout << " what type of attack do you want to use? ";
		cout <<  " 1) accurate lunge \n"  
		<< " 2) crushing blow \n"  
		<< " 3) quick blow  \n"  ;
		cin >> choice;
		if (choice == 1)
		{
	cout << " you choose accurate lunge \n";
	monster_health = monster_health - player_damage_accurate;
	cout << " the monster now only has " << monster_health
		<< " health \n";
player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";
		}
		else if (choice == 2)
		{	
			cout << " you choose crushing blow \n ";
			monster_health = monster_health - player_damage_crushing;
			cout << " the monster now has " << monster_health << " health! \n";
player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";
		}
		else 
		{	
			cout << " you have choosen quick blow! ";
			monster_health = monster_health - player_damage_quick;
			cout << " the monster now has " << monster_health << " health! \n";
player_health = player_health - monster_damage;
		cout << " you now have " << player_health << " health! ";

		}
		}while (monster_health > 0);
	}
if (monster_health < 1)
	cout << " the monster is now dead! ";
cout << " congratulations!" ;

	cout << "\n";
cout << " would you like to play again ? <Y/N> ";

cin >> quit_choice;			// simpe do you want to quit with play again option


	}while (quit_choice == 'y' || quit_choice == 'Y');
					// end of loop

		break;
	case 2 :
		cout << " this is my first text based game ";
		cin.get();
		break;
	case 3 :
		cout << " updates for the future include \n";
		cout << " hitpoints ";
		cout << " different classes ";
		cout << " and others :D ";
		cin.get();
		break;
	case 4 :
		cin.get();
		break;
}

	cin.get();		// added to keep program running at finish
	return 0;

}


please post what you think :D

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

Advertisement
My God 0.o

...

I suggest to learn functions to make things cleaner.
Holy crap, you can read!
Since you are using C++, classes could come in handy here. Such as making a class for the player character.

Here is an example of how it might come in handy.
/// Player.hclass Player {public:        Player(const std::string& name, int health);    void takeDamage(int damage);    bool isDead() const;private:    int health;    std::string name;};/// Player.cppPlayer::Player(const std::string& name, int health) : name(name), health(health) {}void Player::takeDamage(int damage) {    health -= damage;}bool Player::isDead() const {    if (health <= 0)        return true;    else        return false;}


Then you can use it like:
// Create player object with 50 health.Player player("Frank", 50);// Make the player take some damage.player.takeDamage(someDamage);if (player.isDead())    cout << "Player died" << endl;


This is just a quick example, but I'm sure you can see how this will make the code much more readable, and have little to no redundancy. I suggest you read up about classes and Object Oriented Programming.
I second c_olin's suggestion. And if you have any questions or need help you can PM me. :]

You're on the right track and congratulations on your game. :]
Holy crap, you can read!
Since he did not use class, I would guess that he hasn't learned it yet. But
so far, its a step further. Try to organize you code into little task by
using functions, as suggested earlier. So for example, you could have
a void Intro Func().
Our whole life is a opengl application.
Good work! Consistent style and one can see from the code that in the subset of C++ you use, you at least know what you're doing. You're definitely on the right track here :)

The nested ifs and loop are a bit funny, though. You have a lot of duplicate code there. You have written down the first iteration in code and use a do { } while(); loop for the remaining iterations. Probably to somehow incorporate the miss-then-hit for the crushing blow attack into your code.

You could try to combine that into a single loop like this:

cout << "You are a warrior and a monster attacks you!" << endl;do {  cout << "What type of attack do you want to use?" << endl;  cout << "1) Accurate lunge  2) Crushing blow  3) Quick blow" << endl;  cin >> choice;    switch(choice) {    case 1: {      if(random() % 100 < 75) { // 75% hit chance        cout << "You hit the monster with accurate lunge" << endl;        // ...      } else {        cout << "You miss" << endl;        // ...      }      break;    }    case 2: {      // ...    }  }} while(monster_health > 0);


You can nest multiple switch..cases into one another. The break; statement will only break out of the innermost one.

Of course, next you will have very similar code in the cases for the different attack types. Solving that requires the use of some more variables of type string. You'll find out ;)
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
ok thanks alot! guys and about functions i have just started learning the chapter on them. so i will go and try to make it with functions tonight now :D. but for now i have school and double higher computing! :D but thanks for the snippets of code to help me to incorporate functions into my program :D talk to you guys later :D

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

This topic is closed to new replies.

Advertisement