Chess game java code




















If it's a rook move you'll just add 0 to the current position in the other direction so it works anyway. You seem to be rather inconsistent with this. I don't see the point of this class. As far as I could tell it's only ever used when constructing a certain piece.

If you remove this entirely does your code still work the same? I can't tell if it's good or bad to have this at the moment so don't just delete it except if you can handle those cases entirely based on the Piece classes as well.

Why the value at all? Note that it's not wrong to do it this way. I personally would just simplify this entire class to:. I know from experience that this will work but I'm not sure whether this will start at a or A. I believe the most logical refactoring is to make the pieces themselves responsible for checking the full validity of a move. You already have the method.

What you do need is a way for the piece to request information on those in between squares. I'd say this can be achieved by passing in the BoardManager as a parameter to that method:. 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.

A good exercise to check if a class does too much is to try to describe it's main responsibility in 1 to 3 sentences. For example the java. 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. This one line tells exactly what to expect of each of the methods in the class.

Each of those represents a numeric operation and it knows just enough to calculate those. In my eyes your BoardManager class has as main responsibility: Keep track of the state of the board. That way it's expected to find a field with the current state of the board. It's also fine to see the history stored here although this might deserve a specialised class with an undoMove Board board for example that restores the given board to what it was before the last move.

What I don't expect to find is actual logic on the movement of pieces in this class. You already solved this partially by getting the path from the Piece but you can push the responsibility a bit futher into the Piece class.

A class that represents a single Piece on the board. That way it's not surprising to find methods on how a piece can move and even very specialised things like castling moves in a King piece. To make the castling checks easier I would also include a boolean field to tell if that piece had moved already or not.

Might be tricky to undo correctly though. Using null is a valid solution. But like you noticed yourself, if you don't constantly check for null before actually using the piece you'll get a NullPointerException.

Using a good IDE would mitigate this problem a bit since it can warn you at compile time that this field might be null if you didn't check on it yet. I know IntelliJ has this option and expect others like Eclipse or Netbeans to have the same functionality but don't know sure about those. Alternatively you can create a special class to represent a non-Piece.

For example. You can also let certain methods throw specific errors like InvalidMoveException depending on how you'd handle those otherwise in your class. On the plus side you'll get a lot less NullPointerExceptions and you might simplify certain methods. The downside is that you still need to do the actual check for an "emtpy square" anyway so it can take more code to achieve the same functionality.

So to recap on your question: Yes there's an alternative but it's not always better. Look at it case by case. In certain situations it can greatly simplify the code, in others you don't gain anything from the extra Null Object class. There are a lot of raw integer literals in this code; integers are domain agnostic, and don't have any semantic context. For example, the digit 8 appears 30 times in your source code. Do all of those 8s mean the same thing? What about the 25 times 7 appears?

What's really going on here is that you've got some chess ideas the black pawn rank, the king rook file , and you are using integers as in memory representations of those ideas -- but you've written your code in the language of the in memory representation, rather than in the language of your domain. And since there are a lot of different ideas that accidentally share the same in memory representation, it's hard to tell them apart.

Chess boards don't have rows and cols ; they have ranks and files. 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. For example: the black pieces on a chess board are placed opposite their white counterparts ; but that idea is only implied by the fact that the magic numbers in Board:setBlackPieces happen to match those in Board:setWhitePieces.

Maintaining code without making errors is statistically unlikely; having automated checks significantly reduces the changes of an undetected error. The boundary where your code touches the outside is hard to test. Try to make that boundary as skinny as possible. This is really the same idea as the previous; checking the correctness of the UI is hard, so you want to make the adapter between your app and the GUI framework as thin as you possibly can.

Same idea, different spelling: you want the UI adapters to be dumb ; they do what they are told, but don't do logic for themselves. Your UI should know a lot about java. An interesting way to challenge yourself here is to introduce a second UI; for example, the command line.

When you find yourself duplicating logic in the second adapter, you have a big hint that you are doing that work in the wrong place. Look carefully: that's just a function that computes a URL for a row, column pair. So make the function explicit, and then put it where it belongs the functional core. If you are sure that something can never happen, then throw an AssertionError that contains the exception that will "never" happen. 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.

But the board by itself isn't enough. See, for instance, the Langstaff position. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. There are no JREs installed in the workspace that are strictly compatible with this environment. Resource leak: 'scanner' is never closed swagger apiimplicitparam all endpoints cpp live code testng with cucumber android how to get position of a row in listview how to add the last area of an array in java grepper mcm java how to print message to console java removeeventlistener jgit clone in memory action media scanner scan file android 30 deprecated how to check my java heap space size android studio fragment findviewbyid fibonacci map interation in java kotlin android intent pass data java parse boolean to string array of char to string in java how to reverse a number in java write file from a specific location in java android on change class event python to java converter online matrix multiplication javascrpt how to access private vairable in java method ecludis how to count the number of occurrences of an element in a arraylist in java restart java windows trivers json node as node type connect 2 package in android java.

Which one of the following keywords would achieve this? You are currently using Java 1. WrongTypeOfReturnValue minecraft enum with numbers java com. NoSuchMethodError: org. Image; import java. Rectangle2D; import java. BufferedReader; import java. IOException; import java. InputStreamReader; import java. PrintWriter; import java. ServerSocket; import java. Socket; import java. UnknownHostException; import javax. BorderFactory; import javax. JButton; import javax.

JOptionPane; import javax. JPanel; import java. String; import javax. JScrollPane; import javax. BevelBorder; import javax. TitledBorder; import javax. GetInhand g2.



0コメント

  • 1000 / 1000