first game done - need correction

Started by
18 comments, last by Quak 19 years, 8 months ago
Well I would suggest not giving each Piece class their own display function. Instead have a separate class which deals with everything that has to do with displaying things on the screen. This would keep everything a bit more separate and make ur code more portable if at some point you want to change to say D3D.. I know that in this code it doesnt seem like much of problem to change those display things but its a suggestion of overall design. Like in a game you dont want the soldier class to handle anything that has to do with Display, Gameplay ect thats the job of the Graphics Engine and the Gameplay Engine...
Advertisement
Just a few quick thoughts:

- If all your "Chessman" objects have a position and a colour, you should probably add those members to the base class rather than each class. That way, you won't need to write a Get_color() function for each class. Something like:

class Chessman{    public:        virtual int Move(int, int)=0;	virtual void Display()=0;	virtual void Delete()=0;	virtual ~Chessman(){};        Chessman(int z, int s, color f)        {            p.row = z;            p.column = s;            clr= f;        }	color Get_color()        {            return (clr);        }    protected:    	Position p;	color clr;};




- You may also want to move some of the movement logic into separate class static functions. That way, you can "share" code between pieces. For example, a Queen (Lady) simply moves like a Rook (Tower) or a Bishop. Instead of having the same bit of code in both classes, they can both call a more generic function like MoveHorizontal(Position src, Position dest) or MoveDiagonal(Position src, Position dest).

- Rather than using int return types (where 0 means something, 1 means something else) you should either use bool (true if successful, false otherwise) or an enum so that your return errors are more descriptive than just numbers.

- If you've created a Position strucutre, why are you passing x,y (or s,z) values separately? You may want to change your functions to accept Position as an argument (rather than s and z).

- Regarding the Display() and Delete() functions... Another alternative is to have each class simply return the name of the object or the character-representation of the object. You can also have it return slightly different characters (lower/upper) based on the colour. For example:

char Pawn::GetCharRepresentation(){    if (clr == black)    {        return ('P');    }    else    {        return ('p');    }}const char* Pawn::GetName(){    return ("pawn");}


If you really wanted, you could simply have member variables and just put accessor functions in the base class rather than each class.

class Chessman{    public:        char GetCharRepresentation() { return (charRepresentation); }        const char* GetName() { return (name); }    protected:        static const int NAME_SIZE = 64;    	char charRepresentation;	char name[NAME_SIZE];};


Then, in your constructor you can just set the name and character. That way, you don't need to rewrite the same code for every single class. Also, now the only virtual functions you have left is the Move() function and the destructor.

But, anyway, it looks pretty good. My first Chess program looked much, much worse.

-HQ.



Big thanks for your replays guys. I'll try your suggestiones as soon as I get access to my one PC again :)
Nice game!

But, I typed "p" and it started a infinite loop repeating to enter a valid position, a bug.
Alfred Reinold Baudisch[Game Development Student] [MAC lover] [Ruby, Ruby on Rails and PHP developer] [Twitter]
First off, great work! This is pretty amazing for your first game! Great application of OOP methodolagy (sp) into a game setting!

The only suggestion I can make would be with the actual gameplay. You really need a better user interface ;) It seemed like no matter what I entered when it said "White's Turn", I got an infinate printf loop. Just some more detailed instructions would be amazing!

Once again, great job!

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
I simplified Pawn::Move as an example

bool Pawn::Move(int Row, int Col){	int UpOrDown;	bool NormalMove;	bool TakingMove;	ChessMan *DestPiece;			// cache values	UpOrDown = (m_Color == WHITE) ? 1 : - 1;	DestPiece = Board[Row][Col]->adress;	// why not only have a two-dim array with Chessman pointers? the Square class seems redundant		// valid row?	if(p.Row + UpOrDown == Row)	{		// moving up/down without taking a piece		NormalMove = (p.Col == Col && !DestPiece)				// moving diagonally and taking an opponent piece		TakingMove = ((p.Col - 1 == Col || p.Col + 1 == Col) && ValidateDestPiece(DestPiece));		if(NormalMove || TakingMove)		{			p.Row = Row;			p.Col = Col;						return true;		}	}	return false;}bool Chessman::ValidDestPiece(ChessPiece *Piece){	// validate pointer	if(Piece == NULL)	{		return false;	}	// validate color	if(m_Color == WHITE)	{		return (Piece->adress->get_color() == BLACK);	}	return (Piece->adress->get_color() == WHITE);}
First off, big thanks again! I am new to gamedev.net and my first impression is a really stunning one as I haven't expirienced a community more helpful and just that great :)

Good thoughts doho, I'll try it out and apply it to the other move() methods. The ValidDestPiece() method should work for the other chessman classes as virtual function in the ChessMan class as well ?!

Well the interface was really not the thing I spent the most thoughts on ;)
I thought about programming a graphic interface for the programm, I'm just making up my mind which library I want to use.
typ 1.1 and the program will crash :/

(or typ a letter like T)
Quote:Original post by Quak
The ValidDestPiece() method should work for the other Chessman classes as virtual function in the Chessman class as well ?!

It doesn't have to be virtual since no subclasses needs to change its behaviour. You can call this function from the other subclasses as well.
Year, sorry about the interface, I'll redesign it as I said.
You have to use only numbers. The very left, top square of the chessboard is 1 1, the very right, bottom square is 8 8. To move with a bishop from 1 1 to 8 8 you have to type 1 <ENTER>, 1 <ENTER>, 8 <ENTER>, 8 <ENTER>.

This topic is closed to new replies.

Advertisement