Chess game java program


















Run the game Using its jar. You can play it player1 vs player2: Server and client one same computer or using two computer Local host on the same computer player1 vs computer is not implemented. ChessFrame; import MainFrame. Point; import java. Color; import java. Graphics; import java. Graphics2D; import java.

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. 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. Chess application in Java Ask Question. Asked 3 years, 8 months ago. Active 3 years, 7 months ago. Viewed 14k times. I have got some specific questions about the implementation that I would love to have answered 1 The BoardManager.

PlayerType; import game. WHITE ; squares[5][0]. WHITE ; squares[1][0]. WHITE ; squares[6][0]. WHITE ; squares[0][0]. WHITE ; squares[7][0].

WHITE ; squares[3][0]. WHITE ; squares[4][0]. WHITE ; squares[0][1]. WHITE ; squares[1][1]. WHITE ; squares[2][1]. WHITE ; squares[3][1]. WHITE ; squares[4][1]. WHITE ; squares[5][1]. WHITE ; squares[6][1].

WHITE ; squares[7][1]. BLACK ; squares[5][7]. BLACK ; squares[1][7]. BLACK ; squares[6][7]. BLACK ; squares[0][7]. BLACK ; squares[7][7]. BLACK ; squares[3][7]. BLACK ; squares[4][7]. BLACK ; squares[0][6]. BLACK ; squares[1][6]. BLACK ; squares[2][6]. BLACK ; squares[3][6]. With alpha-beta, we get a significant boost to the minimax algorithm, as is shown in the following example:. Follow this link to try the alpha-beta improved version of the chess AI. The initial evaluation function is quite naive as we only count the material that is found on the board.

To improve this, we add to the evaluation a factor that takes in account the position of the pieces. For example, a knight on the center of the board is better because it has more options and is thus more active than a knight on the edge of the board.

This said, it still lacks strategic understanding. You can check out the final version is on GitHub. If you want to learn more, check out the chess programming wiki. If this article was helpful, tweet it. Learn to code for free. Get started. Forum Donate.



0コメント

  • 1000 / 1000