If the piece can't move diagonally, it's illegal.If there's no piece on B3, it's illegal.What squares can be attacked by the piece at a particular location?.In addition, based on the current state of the board, we can ask it to calculate: A board is just a 2D array where cells either do, or don't, have pieces. It has a color, and a type (rook, king, queen, etc), but that's it. It also knows about any graphics information like sprites. The game state knows about the board, and also about extraneous information such as captured pieces. It knows only about the pieces and how they're arranged. They only know what kind of piece they are, and what color they are (black or white) Let's break your program down into layers. The more information you give to an object, the more complex it becomes, and the harder it is to keep track of it. My suggestion is to make things as simple as possible. That would lead to a solution where a Map> per piece is required, but without the necessity of actively observing other board changes. If it turns out later that this method is called quite often for the same, unchanged board so it becomes a performance bottleneck (which I doubt), you can still cache its results using memoization. However, if you put this method into the board instead of the piece, pieces will not have to know anything about the board, so this avoids a cyclic dependency between boards and pieces. It may be debatable if it should really be a method of the piece, or a method of the board. Instead of remembering all those cells for each piece "just in case it could become slow", why not give a piece a method with the board as parameter, which returns the related List (or Map>) by calculating it when the method is called? This way, no notifications or observer pattern or "32 listeners" are needed any more. Having a "Map object that keeps track of all the cells to which it can travel" looks to me like an example of premature optimization. So in any case anything happens in any of the cells watched by a particular set of pieces, only those pieces will be notified.īut then I am not sure if this is a good approach as I have to have at max 32 listeners for every cell. I was thinking of going with the Observer pattern where every cell will be a subject and only the pieces watching them will be observers. What best way can I notify the Queen or any other piece watching that particular cell of the event so that each piece can call its own update method and redraw its path of attack? Now, say a Piece of the Queen's own team comes in its way of attack in the FORWARD direction and puts itself on the cell that's located at chessBoard So I have designed an abstract Piece class, which among others, has a Map object that keeps track of all the cells to which it can travel direction-wise, something like this Basically I want to show the paths / directions in which the it can travel / attack in a different color. My idea is to show the number of squares / cells a piece can travel to when selected. I am trying to design a Chess Game using OOPs concepts that has a UI element to it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |