I currently am working on programming a chess game. Nothing difficult, just a board with pieces that can be moved around. I got the first part working (draw board with pieces), but I am having problems with writing the methods for the pieces to be moved (MousePressed, mouseDragged, mouseReleased). Could any of you please help me? I am still a beginner, so lots of help would be appreciated. Thank you in advance.
P.S. I know this sounds horrible, but if the answer could contain a finished version of this code that would be heavens... but maybe thats really me asking too much already (i just learn better that way)
Vasiliy Shcherbinin wrote:if the answer could contain a finished version of this code that would be heavens... but maybe thats really me asking too much already (i just learn better that way)
We generally don't give out full-blown solutions here. Folks here love to help, but that means 'help' and not 'do it for me'.
Now, I notice it's been a few days and nobody has responded. You may want to read some of our FAQ on HowToAskQuestionsOnJavaRanch. You have done many, but your post is still a little broad. We tend to respond better with specific, focused questions, rather than general "This doesn't work, please help" kinds of question.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
first of all: have you run your program and is your chess board showing? What strikes me in your code is that
your Chess2 class extends JFrame, but in the body you define a separate frame (line 11).
Then you start filling this Jframe with lots of stuff, and putting it on the screen (line 79),
but if you look at line 25, you request the content pane of class Chrss2, not of that JFrame,
and it is to that content pane you are adding the chesspane (line 25 again).
I'm not sure what the effect of all this is, therefore my question.
Another remark is that you use two images for your rooks at a8 and h8. Since one rook is just
like any other (same colored) rook, you could well use just one image. Same goes for the
night and the bishop, and certainly for the pawns.
A final remark is about line 46. You add your JPanels to the frame, but for the content pane
the standard layout manager is BorderLayout. I wonder what the effect is.
Well so far my first impressions.
Now about moving pieces, detecting clicks, and so on. That is too much to treat it all here,
and besides, there are "many different ways leading to Rome".
So I focus on detecting clicks on your chessboard.
A possibility is to use subclasses of JPanel for your squares. Such a subclass would have two
extra fields, say int row, and int file. In your constructor (in line 36) you could use
and in your constructor you set the row and file to a and b. The advantage is that
each square now knows its own coordinates. If you add a method in the
subclass, like "getRow()" and "getFile()", then you can add a MouseListener
to all your squares, and all that that Listener has to do is get the source of the
click and ask that source for its coordinates. It could then pass these
coordinates to the methods that handle a possible move.
Well, so much for this time. Hope you get some ideas how to proceed.
There are three kinds of actuaries: those who can count, and those who can't.
I suggest you write a few simpler game programs first. Writing a chess program, even one that doesn't play itself and just validates your moves is a substantial task. Starting an impossible task with little experience just means you're going to get extremely frustrated and then give up. It's far more rewarding to start off with achievable goals. I once had a brainwave on how to write a chess program and this was the applet I produced after a very knackering weekend. It plays ok and the code is available to use. A reasonable player should be able to crush the program and it also has a few issues with understanding all the rules of castling, en passant, queening etc but at least it plays a fun game and looks ok. You're welcome to use the code as long as you say where you got it from.