123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|657|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> C/C++/C#/Other -> Console Chess

Thu, 07 Jun 2007, 16:59
mike_g
I have been trying to practice C++ a bit. I have to say I don't like it very much so far. With all that extra code its like a big turn that wont flush. Then again its probably just that I suck at OOP. Anyway heres my latest creation, its a console chess game. At the moment it just checks moves. I nearly have a version which tests it the king is in check or check mate and saves and loads games, but its not all up and running yet so heres an early version of it:

Oh yeah and it only runs on windows systems. I might change that for DOS tho
Thu, 07 Jun 2007, 22:11
JL235
I've not really used C++, and so what I'm gonna say is based on programming and learning with Java. One of the most important aspects of Object-Oriented programming is about abstraction, to help 'decouple' your code and so make it far simpler to use.

One idea after just looking at your code, is that could make a Piece class with a 'checkmove' method (which in Java I would make abstract so the Piece class could not be used). Then subclasses the Piece with Bishop/King/Queen/etc, classes and simply override the 'checkMove' for each (this would be forced in Java if the method in Piece was abstract) with the correct move checking method for that class.

When you create the pieces you can then store them as Piece, even though they are actually of the Queen/King/Rook/etc class. This is called 'polymorphism'.

This means when you select a piece (or move it or whatever happens) it will simply run it's 'checkMove' function, rather then having to find and run it's associated 'KightMove', 'QueenMove', etc. This is a far better way to design the program. You then shouldn't need to set it's type either as each piece can set it's own type upon creation.
Fri, 08 Jun 2007, 03:16
mike_g
That sounds like a good idea, I'll see if I can set it up like that. I just had to re-write a lot of the program because I had the players lurking in between the board and pieces, which was causing all kinds of problems. I guess once I get the hang of how to use objects a bit better C++ will become more fun to code in.
Fri, 08 Jun 2007, 03:57
JL235
Object-oriented programming is much more then just defining classes and methods, it's also about how to design and structure a program in the object-oriented sense (which isn't just 'making everything and object'). That's far harder then people think.

You ought check out your local library for any programming books on designing programs. It'd come in handy, if just for the experience of trying out new and different techniques.

Another thing I'd like to point out is that you check your Pieces 'type' a lot in your code. You shouldn't be doing this as it's bad practice. Maybe you could store what side they are on, if they are black or white. You should try to only couple your game to the Piece class and not the subclasses (apart from when they are created for which you could make a PieceGenerator class, making it the only class coupled to the individual pieces).