Collectives on Stack Overflow. Learn more. Asked 11 years, 9 months ago. Active 4 years, 9 months ago. Viewed 54k times. This is my code below import javax. Improve this question. Muster 9, 1 1 gold badge 32 32 silver badges 45 45 bronze badges. I agree with Paulo. I mean if you can't put labels on the chessboard, have fun reading up on the minimax theorum for the AI - or even figuring out whether moves are valid or not for each player if you ditch AI all together.
Instead of using labels what should I use? Should I use a Panel or some other form of container? See also this implementation — Andrew Thompson. Add a comment. Active Oldest Votes. Color; import java. Each byte corresponding to a piece white or black or an empty square. This is done for efficiency, although it is not the most efficient possible implementation. We will look at that later.
There is also a number of flags attached to a Position. We need to know if castling happened if there is an en-passant possible and which move we are on. This is to determine if it is white or black move. We are not dealing with the 50 move draw rule here. Here I would ask for your ideas. Can we do much better than an array of bytes while maintaining readability and performance? If you have good ideas, let me know on twitter e4developer or in the comments. The last part is looking at the pieces.
All we need to know are the possible moves, leaving us with a rather simple design:. All we need to get from each Piece is the set of legal transitions possible from a given position.
These are going to be simply static methods, as pieces are actually recorded as bytes for now. InputStreamReader; import java. Else it prints a stacktrace. Improve this question.
Gnik Gnik 1 1 gold badge 6 6 silver badges 19 19 bronze badges. Really glad the work paid off. Add a comment. Active Oldest Votes. The only reason to ever do this is when your employer tells you to. Why not return that directly instead? EDIT extra questions. You already have the method public abstract boolean isValidMove Coordinate initPos,Coordinate finalPos ; and each of the pieces know which squares to check for example, a knight ignores any in between square and a queen needs to check each square in the line between initial and final coordinates.
I'd say this can be achieved by passing in the BoardManager as a parameter to that method: public abstract boolean isValidMove Coordinate initPos,Coordinate finalPos, BoardManager boardManager ; That way the BoardManager can delegate the actual move checking to each of the pieces and only needs to provide the needed methods like isEmpty Coordinate square or isEnPassantSquare Coordinate square. Math class has this as it's first line in the javadoc: The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
To do the same exercise on the Piece class I would describe it as: A class that represents a single Piece on the board. Improve this answer. Imus Imus 4, 6 6 silver badges 25 25 bronze badges. But in the case of the PlayerType and PieceType , I thought It would make the debugging and printing the board easier as I could just print out the enums. Is there a better way to do this? The review seems spot on though. I still have some questions that I have just included in the question.
If you could comment on these additional questions, I would accept this as the best answer. Thank you again for this. So in my simplified PlayerType the printout will be exactly the same as in yours. I also just noticed that you use the type in the toString method. An alternative would be to implement the tostring in each of the specific Piece classes to get the same effect. Then the code is redundant. I will keep that in mind now. Seems like I have a lot to do.
Use the domain language where available Chess boards don't have rows and cols ; they have ranks and files. Duplication of idea When the same idea appears multiple times in your code, you should think about whether the code will be easier to maintain if that idea should have a single representation. Tests Maintaining code without making errors is statistically unlikely; having automated checks significantly reduces the changes of an undetected error.
Make learning at least one testing framework a priority. JUnit is a good starting point. Separate IO from your core The boundary where your code touches the outside is hard to test. Square square; if boardManager. Duplication of state Something to recognize about chess: the current state of the board can be calculated from the initial board layout and the list of moves in the game.
So your logic on BoardManager:move probably needs a re-think. One thing that bothers me is the duplication of state comment. The board manager class does contain a moveList variable that contains the list of moves in the game. Could you clarify that point for me? I am really satisfied with your answer and your review of the general structure of the code. Wish there was a way to mark two correct answers.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Git stats 50 commits. Failed to load latest commit information.
View code. Highlighting possible moves any illegal moves according to the rules are excluded when any piece is clicked which assist beginner players to know which to play. Disallowing any illegal moves ex.
0コメント