Hi to everybody; I've written my own chess game using classes, and I'd like to discuss with you the choices I've made, cause I'm sure something can be improved; basing partly on what has been said in other posts, my main choices are the following:
- I used only one class to model the Piece, having a label telling me the kind of piece; this simplifies things like the promotion of a pawn, since I only have to change this label;
- then I moved the knowledge of how a piece can move to another class, Move, which takes responsibility of getting input from the user and testing if the move proposed is legal in two steps, first theoretically ( a necessary condition for a certain piece to go to a certain square is that the piece can move that way, and other issues concernig the occupation of the squares involved ), then practially ( check for the king, briefly ).
I have two other classes, Square and Board; the first contains a pointer to piece, and can give back information about its occupation, the color of the piece it stores, etc; the second, besides of printing the chess board, can check also for stalemate and checkmate. Board is basically a 12x12 array of squares ( 12 instead of 8 simplifies, from my point of view, checking if a piece can do a certain move ).
Now, I was wondering if it would be better to turn the Piece class in an ABC; so that I can give it the knowledge of the moves it can do, simplyfying the life of Move class, and making my program a little more OO. But I'm now facing two main concerns:
- how can I insert that knowledge into the piece, e.g., knight, such as I'm able to access from the outside an array
deltaK [] = {-25,-23,-14,-10,10,14,23,25}
necessary when I make this test
1 2 3 4 5
|
for (int i = 0; i < 8; i++)
{
to = pos + deltaK[i];
other stuff..
}
|
which is made to see if a move is legal?
- how can I handle the promotion of a pawn, say to a queen or knight, using new and delete?
Thanks in advance for your help.
PS I'm not posting the whole code for two reasons: it's commented in Italian and it's quite long; I'm not having bugs or other problems, but just want to improve the design. If someone thinks that it's better I post it, just tell me.