Jump to content

  • Log In with Google      Sign In   
  • Create Account

I need advices

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Kheyas   Members   -  Reputation: 179


Posted 11 March 2012 - 04:16 PM

Hey guys!
I started a simple text based game. Basically all I need is some advice and help. How can i make the save/load system work? How can I improve the code? Is there a way to use multiple files? Would using classes make it better?(I still don't know how to use classes -they seems so complicated Posted Image ).
The code is here: Code
The exe is here: Executable


#2 Vlad86   Members   -  Reputation: 130


Posted 12 March 2012 - 10:19 AM

case 1:{CreateCharacter();}
case 2:{LoadCharacter();CharacterMenu();}
case 3:{About();MainMenu();}
case 4:{Quit();}

should be something like:

case 1:{CreateCharacter();} break;
case 2:{LoadCharacter();CharacterMenu();} break;
case 3:{About();MainMenu();} break;
case 4:{Quit();} break;
default:{WrongValue();MainMenu();} break;

if you don't put break after a statement, other statements bellow will be also executed.
Break makes sure that it does not happens.

#3 Zael   Members   -  Reputation: 154


Posted 12 March 2012 - 10:39 AM

There is absolutely a way to use multiple files, but how you do that will depend a little bit on your compiler/IDE. For Code::Blocks and Visual Studio it should just be a matter of adding those files to your project. To use code from different files you have to declare that code somewhere first. Header files are good way of doing this. Then you can (for example) all your character creation code in a file called CharacterCreation.cpp. Put definitions of the functions in CharacterCreation.hpp and in your main file have an include of CharacterCreation.hpp.

Making classes would definitely make it better. Classes as a concept aren't too difficult to understand. Creating your own constructors, destructors, operators, and using inheritance can get tricky but just basic use of classes is fairly straight forward. Think of a class as an actual object. For example in your code you have a character. That character has a bunch of information that is all kept separately at the moment, but you can use a class to combine all that information into a single variable.

class Character
	 char[30] name;
	 int classType;
	 int level = 1;
	 int maxHealth;
	 int experience;
	 int experienceRequired = 100;
	 int health;
	 int damage;

Then you create a character variable based on the character class.
Character character;

And access the previous variables all from that class.
character.health = 90;
character.damage = 10;
character.experience = character.experience + 5;

This is a crude example that hopefully shows you how classes can be good just for bookkeeping, but the benefits don't stop there. Once you feel comfortable you can also add methods (functions that are specific to a class). In your current code CreateCharacter, SaveCharacter, and LoadCharacter would all be great methods to put into your class when you feel ready.

As you may imagine this makes it a lot easier to have more than one character as well. Before you would have had to create new (differently named) variables for each of your characters. With a class you can just do:
Character character1;
Character character2;
Character character3;
character1.name = "Conan";
character2.name = "Caesar";
character3.name = "Achilles";

Note: This example is really more similar to a struct because all the information is public (can be accessed from any class) and it has no methods; however, as a basic demonstration I think it suffices. As you learn more about methods and inheritance (especially) though, you will see that classes are actually far more powerful than this simple bookkeeping.

#4 Kheyas   Members   -  Reputation: 179


Posted 13 March 2012 - 07:25 AM

Thank you very much for the answers! :)

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.