Chess game design problems

I want to implement a chess game in java. So far I thought of these classes :

  • Piece : an abstract class. All the pieces types are inherit from this class (like Queen , King , Pawn and so on. The class piece will have methods like getLegalMoves() getMovePath(Cords source, Cords dest) that are implemented different in each sub classes.

  • Board : This class will have a 8X8 array of Piece . NULL ref is meaning for empty square.

  • BoardAlgorithms :This class will consist all the "scanning" algorithms like checking if there is a check, a checkmate, checking if a move is legal and so on.

  • Of course it is just the basic classes but I have some problems i would like to solve before i get into the harder part:

  • I need a Boolean method like isKing() in Piece class for find out that a king is being attacked (will be used in checking if there is a check or checkmate) but it seems to me like not the best solution in the OOP way. Is there an other way for doing this?

  • White pawns and black pawns are moving differently. Should i split the class Pawn to 2 sub classes like WhitePawn and BlackPawn or should i add an enum member like m_type that will hold BLACK or WHITE and according to its value establish the correct move?

  • Before adding GUI and graphics I want to implement a very simple textual view for debug the logic of the game. Again I will need an elegant way to know that when we scanning the board i should print different String for each piece sub class. Also: consider we want to change the view implementation to GUI with graphics in the future. what is the best solution here?


  • I need a Boolean method like isKing() in Piece class for find out that a king is being attacked (will be used in checking if there is a check or checkmate) but it seems to me like not the best solution in the OOP way. Is there an other way for doing this?

    For this, perhaps it's best to also store a reference to each specific King object (in addition to your List of Pieces or whatever you have), and then do a check on every turn on whether that king is in check or checkmate.

    White pawns and black pawns are moving differently. Should i split the class Pawn to 2 sub classes like WhitePawn and BlackPawn or should i add an enum member like m_type that will hold BLACK or WHITE and according to its value establish the correct move?

    Something like the second option sounds better. Having some instance variable added to the Pawn class that describes which direction they can move in is only one more field in the class (which also may help in determining if it's own the correct far side of the board when the time comes to replace the pawn). You could pass this info in during the constructor. Also this wouldn't limit black or white pawns only being allowed to one starting side of the board.

    The Piece objects may have a parent Team object that could store the color and direction they are trying to move in as well that they could query, allowing you to not need to add individual details to Pawn.

    Before adding GUI and graphics I want to implement a very simple textual view for debug the logic of the game. Again I will need an elegant way to know that when we scanning the board i should print different String for each piece sub class. Also: consider we want to change the view implementation to GUI with graphics in the future. what is the best solution here?

    This is a good way to do it. Separating your logic from your GUI code makes for the best design upfront that can easily be changed and modified. I'm not sure if you are asking for what might be an elegant way to print the board textually, but you could just use letters appended by a number, like Q1 is white's queen, Q2 is black's queen. It should be easy to change the view implementation to a GUI when that time comes, your current thought process on separating the view from the data and logic is already a good solution.


    I've written a few chess programs before and you are on the right track. I will try to address your questions one by one:

    I need a Boolean method like isKing() in Piece class for find out that a king is being attacked (will be used in checking if there is a check or checkmate) but it seems to me like not the best solution in the OOP way. Is there an other way for doing this?

    There are several ways to do this, isKing() would work but is not a very good design. It is probably easiest to have the Board or Player class keep a reference to it's king. The King is the only piece that is on the board at all times so it's OK to specifically keep a reference to it. Also some of your check and checkmate (and even isValidMove()) methods need to check conditions to see if the king is being attacked or if it has moved yet etc. so lots of places in your code will need to keep referring to the King

    White pawns and black pawns are moving differently. Should i split the class Pawn to 2 sub classes like WhitePawn and BlackPawn or should i add an enum member like m_type that will hold BLACK or WHITE and according to its value establish the correct move?

    You can have some kind of boolean or enum that stores the direction the pawn moves in or if you have a Player class it could be there and the pawns delegate to their players to determine which direction to move in. While we are talking about specific pawn fields, you might need to also keep track of if the pawn has moved yet since the first pawn move can move 2 spaces. Rooks and Kings need similar flags to help see if they can castle.

    Before adding GUI and graphics I want to implement a very simple textual view for debug the logic of the game. Again I will need an elegant way to know that when we scanning the board i should print different String for each piece sub class. Also: consider we want to change the view implementation to GUI with graphics in the future. what is the best solution here?

    I've found the easiest text is to print an 8x8 board and use uppercase letters for white and lowercase letters for black

     P p = white and black pawns
     B b = white and black bishops
     N n = white and black knights (K's used by king)
     R r = white and black Rooks
     Q q = white and black queens
     K K = white and black kings
    

    Of course feel free to swap the upper/lowercase if you want it the other way around. By using a single letter, you can easily see the layout of the board in a monospaced font and the different case lets you easily distinguish the different players pieces.


    Take look at the design patterns specifically "Creational Pattern." That will give you a framework for both logic and UI. Keep thing as generic as possible and as someone suggested in the comment, enum the type, color and direction for a generic object.

    链接地址: http://www.djcxy.com/p/84604.html

    上一篇: 国际象棋移动(基本,没有AI)

    下一篇: 国际象棋游戏设计问题