Advertisement

Any other suggestions for my code?

Started by January 14, 2003 08:41 PM
10 comments, last by SSJCORY 21 years, 10 months ago
Hey you guys thanks for your suggestions the first time around. I have re-written and am re-posting. If anyone has any suggestions for the following code, like style, or some good ideas for coding future parts of the game Thanks
  
#include<iostream.h>
#include<stdlib.h>
class cplayer{
private:
	int health;
	int attackpower;
	int defensepower;
	int speed;
	int maxhealth;
	int special;
	int specialpower;
	int maxspecialpower;
public:
	void create(
		int h, int a, int d, int s, int mh,
		int spec, int maxspec
		){
		health = h;
		attackpower = a;
		defensepower = d;
		speed = s;
		maxhealth = mh;
		specialpower = spec;
		maxspecialpower = maxspec;
	}
	int GetHealth(){
		return (health);
	}
	int GetSpeed(){
		return (speed);
	}
	int GetSpecial(){
		return (special);
	}
	int GetSpecialPower(){
		return (specialpower);
	}
	void Attack();                  //Don''t forget

	void Defend();					//Don''t forget

}player, enemy;

class cmessages{
public:
	char initial1;
	char initial2;
	int rightinit;
	int playerclass;
	int rightclass;
	void BegginingMessage(){
		rightinit=2;
		while(rightinit!=1){
		cout<<"\n\n\n";
		cout<<"Type your first and last initials?";
		cin>>initial1>>initial2;
		cout<<initial1<<initial2<<" is that right(1 yes, 2 no)?";
		cin>>rightinit;
		system("cls");
		}
		rightclass=2;
		while(rightclass!=1){
		cout<<"\n\n\n";
		cout<<"Choose a class:\n";
		cout<<"1 Knight\n";
		cout<<"2 Archer\n";
		cout<<"3 Troll\n";
		cout<<"4 Goblin\n";
		cout<<"5 Wizard\n";
		cout<<"6 Wanderer\n";
		cout<<"Class:";
		cin>>playerclass;
		if(playerclass<1||playerclass>6){
			cout<<"\n\n\n";
			cout<<"Invalid Class\n";
			rightclass=2;
			system("cls");
		}
		if(playerclass>0&&playerclass<7){
		cout<<playerclass<<" is this right(1 yes, 2 no)?";
		cin>>rightclass;
		system("cls");
		}
		}
	player.create(100, 15, 15, 15, 100, 15, 15);
	}
	int menu;
	void Main(){
		cout<<"\n\n\n";
		cout<<"Press 1 to start game.\n";
		cout<<"Press 2 to see rules.\n";
		cout<<"Press 3 to quit.\n";
		cin>>menu;
		system("cls");
		if(menu==1){
			cout<<"\n\n\n";
			cout<<"IN THE BEGGINING\n";
		}
		if(menu==2){
			cout<<"Rules\n";
		}
		if(menu==3){
			cout<<"Cya\n";
		}
		cout<<"\n\n\n";
	}
}message;
main(){
	message.BegginingMessage();
	message.Main();
	return 0;
}
  
Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Man, you deleted your thread while I was trying to post! Shame.
quote: Original post by SSJCORY
Thanks guys you''ve all been so much help. Also is there a way to do color in console? Oh, and yeah you can use main as a function as long as it is a member of a class.

Cory Fisher
You can use color in console, but it''s usually operating system dependant on how to go about getting it to work. If you just want colored text and are using DOS, I''m pretty sure conio.h has what you are looking for. You might have to do some weird stuff to get it to work though.

If you want to get into graphics, you might try looking at the APIs of Alegro or SDL(I believe SDL is what it''s called). I hear they are pretty simple to start with, but you should probably know how to use arrays and pointers; most programming in general pretty much require that much. Hey, if you can''t understand how to use those APIs, just keep learning the language.

AS FOR YOUR CODE: Well, I don''t think you really change much of your code, just fixed the h = health stuff and capitalized your functions. Oh, you added the system("cls") call too. In the thread you deleted people posted some suggestions that I still think you should follow. If you forgot: do-whiles, spaces in your code to seperate the operators, and a constructor. I also posted a bit on a different way to structure your code, but you had your chance and deleted it and I''m not gonna go over it again.

Just keep coding and try formatting your code so it looks nice. Here''s an article to think about: webpages.smyrnacable.net/krum/1tbs.html

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
Just a few code-comments.
As nobodynews mentioned you ought to have a constructor.
The Get...() methods should be declared as const, like so:

  int GetSpeed() const {    return speed;}  

And the cout statements should have some endl''s here and there to make sure the output get printed when you actually want them to.

-Neophyte
int vs unsigned: if your values will never be negitive, then always use unsigned variables.

Add a default constructor to cplayer to initialize all the members to 0 or some other ''default'' value.

rename cplayer.create() to cplayer.initialize() - you are not creating the object at that time, you are initializing it.

A note on const functions that Neophyte mentioned: if your member function never edits any members of the class, then it should be marked as const.

Functions that dont take parameters mark them as void ie:
int GetHealth(void);
If you just specify GetHealth() then the argument defaults to an int.
quote: Original post by pawn69
Functions that dont take parameters mark them as void ie:
int GetHealth(void);
If you just specify GetHealth() then the argument defaults to an int.


Hi

if you are using C, thats ok, but (as seen here) if you use C++ its ok not writing void. IMO without void it looks better.

Bye
ScottManDeath

Thanks you guys, why would I need a constructor if I have my InitPlayer() function? Constructors confuse me so why use them if they''re not going to make a difference?

Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
quote:
#include<iostream.h>#include<stdlib.h>   


iostream.h is a non-standard header. You should be using iostream (no extension). The version with the extension is not guaranteed by the C++ standard to exist, and is usually only provided for backwards compatibility. Note that everything in the standard version of the header will be in the namespace 'std'.

quote:
void create(...)   


Rather than having a 'create' function, you may want to use a constructor to initialize your variables.

quote:
int GetHealth(){ ... }    


Functions that don't alter the values of any member variables in the class should probably be made const.

quote:
class cmessages{ ... }   


I don't see the purpose of this class. The name suggests that it just displays messages, but it seems like it runs the whole game.

quote:
	char initial1;	char initial2;	int rightinit;	int playerclass;	int rightclass;   


Shouldn't these variables be local to BegginingMessage? And I also think the name 'BegginingMessage' doesn't really represent what the function does too well.

quote:
void BegginingMessage(){ ... }   


You may want to split this function up into several smaller ones. Also, using whitespace around your operators might make things more readable.

quote:
int menu;   


This should probably be local to Main.

quote:
if(menu==1){...}if(menu==2){...}...   


It may be easier to use switch statements for this.

[edited by - SilentCoder on January 15, 2003 3:02:06 PM]
i dont know if you understand what he said about the std namespace but the first time I saw it a while ago I didnt ill show you what to do tho.


  #include <iostream> //Don''t put a .h on the endusing namspace std; //this will do the same as iostream.h will                    //the only difference is that iostrean.h is //non-standard. So dont use it. That was the first pointer //(command) I got form the poeple here.//other stuff here  


Homepage
Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
i dont know if you understand what he said about the std namespace but the first time I saw it a while ago I didnt ill show you what to do tho.


  #include <iostream> //Don''t put a .h on the endusing namspace std; //this will do the same as iostream.h will                    //the only difference is that iostrean.h is //non-standard. So dont use it. That was the first pointer //(command) I got form the poeple here.//other stuff here  


Homepage
Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
i dont know if you understand what he said about the std namespace but the first time I saw it a while ago I didnt ill show you what to do tho.


  #include <iostream> //Don''t put a .h on the endusing namspace std; //this will do the same as iostream.h will                    //the only difference is that iostrean.h is //non-standard. So dont use it. That was the first pointer //(command) I got form the poeple here.//other stuff here  


Homepage
Luck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.

This topic is closed to new replies.

Advertisement