#### Archived

This topic is now archived and is closed to further replies.

# Two player Java chess program

This topic is 5214 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I''m currently writing a two-player chess game in Java at the moment and I have just under a month to complete it. The game is to be played between two human players so I don''t need to worry about computer AI for the moment (although I noticed the articles from GameDev.net gave some excellent coverage of the topic). I have decided that before I start on the game engine to monitor whose turn it is, provide error messages for invalid moves etc. I thought I would concentrate on the chess board and the pieces themselves. I am implementing the full rules of the game (hopefully including pawn promotion, en passant and castling depending on time restrictions) so that pieces move only in the way they are allowed, pieces can only capture enemy pieces and check/checkmate/stalemate are detected correctly. Currently my program displays the board and the pieces on it. Pieces can be moved and placed so I have now moved onto the move validation. I have already determined the conditions for each pieces move validation but I am having trouble implementing them. I have started with the move validation for white pawn''s. The initial move works correctly (i.e. it has a choice to move 1 or 2 spaces forward) but when I try and move the pawn after making its initial move, the piece''s own internal board position (i.e. the position the piece itself believes it is at) seems to be wrong. This really messes things up as I can''t make a valid move if the piece thinks it is somewhere else. I''ve wracked my brain with this one and I honestly can''t understand why it is doing this. If any Java-savvy programmers out there could put me right I would be most grateful. Also any general hints on how to improve the design of my program (I believe I''ve made the right choices as far as OO is concerned) make it a little faster, check/checkmate detection etc. would also be most welcome. If anyone would like to make a significant code contribution I would be happy to reimburse them for their time (a paltry sum I admit but as much as I can afford). Please contact me to let me know if you would like to do this. Source code can be obtained from: http://www.dur.ac.uk/j.d.p.murphy/Java/chess.html I will post updates at this address on a ad hoc basis if anyone is interested Many thanks

##### Share on other sites
Hi im currently developing a Java chess game for college and im under a bit of stress myself to get it completed so ive got no time to look at your code, but i can give you some help with your problem.

Im using a bitboard representation of the board and have been impressed with the ease and speed of the calculation of the pieces moves.

check out R.Hyatts paper on bitboards if you need to there fairly good

http://www.cis.uab.edu/hyatt/pubs.html

If you are using arrays to hold the board i find there just a pain to work with bitboards are much cleaner and efficient.

I cant help you more than that at the moment unless you give me more specifics

##### Share on other sites
I did think about bitboards but I wasn''t 100% sure how to use them in Java. They look quite tricky to implement.

I figured that you would use the long type for the 64-bit representation. Then each piece type, e.g. a knight, would have a bit board for its position, attacked squares and a bitboard mask to calculate its moves I guess.

Do you have any code that could get me started on this? How easy to use are bitboards in determining check/checkmate?

Thanks

##### Share on other sites
Hi,
I had a look at the code and I think I found the problem. On line 399 in Board.java (that's in the version on the web at the moment), when a move is checked and found to you be valid, you say
selectedPiece.setPosition(bp);

But in the setPosition function you just do a shallow copy of the BoardPosition argument passed in, so the piece's position is pointing to the same BoardPosition object as you use in the Board object. So when you next click on a different square, the 'bp' object gets updated, and so the location of the piece that was moved is also changed.
So you just have to change the setPosition() method to do a deep copy of the BoardPosition argument passed in.
That seemed to fix it, but I didn't do much testing.

[edited by - simb on April 6, 2004 9:51:15 AM]

##### Share on other sites
Well spotted! Thanks ever so much for pointing that out. I can now use the few remaining brain cells that haven''t been damaged from hitting my head against the wall to actually finish implementing the rules.

I totally understand why it wasn''t working now. I was merely passing the reference to the position and not its value.

I''ll finish implementing the rules later today. I''ll be updating the code as often as I can so check back at the website if you are interested.

Thanks again.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5
gaxio
11

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001837
×