I'm planning to write a chess game, and starting off with the UI, I was planning to use an 8 X 8 Layered Panel on top of which 8 labels would represent the board, would you think that's a good idea? I find it a bit floppy so far to be honest, even, though I've loaded up NetBeans to help me with form designing.
Have you ever implemented a Chess Game in Java?
Then I need some sort of graphic for representing the pieces (King, Queen etc), which I'm not sure how to draw in a manner that the pieces appear to be moving. Any ideas?
I also need to find a way of passing rules to the 'computer player' so as to remember sequences of strategic moves (e.g. Black King to Q4, White Queen to Q3, Black King eats White Queen), so some class design and a console will probably help.
Just have images to represent each type of piece and two images for the board (light and dark squares). Use google to show you images of chess pieces and then use your favourite drawing package (mines Paint.net) to create a correctly sized image of each piece. Use java's drawImage to draw the images. Remember to draw the board first and then the pieces afterwards. The moving piece will just have it's x&y coordinates altered slowly until it moves to it's new position.
Now the tough part of the program has been solved you just have the easy part left of writing the chess playing bit but personally I'd suggest starting with a simpler board game (noughts and crosses etc) and then working your way up from there
I started writing a chess game a few months ago... currently sitting near the bottom of my pile of unfinished projects.
I started the other way round, writing a quick method to print a position to the standard out, using Unicode chess symbols. If you use the DejaVu Sans font in Netbeans it looks OK. Then I started work on defining how the pieces move etc, and didn't get much further than that.
I wouldn't use Swing components for actually outputting the graphics, unless for a quick prototype. They are meant for designing GUIs rather than drawing scenes. Use a JPanel and override paintComponent to draw your scene.
I guess I would just use a single custom JPanel as the chess board, and draw everything using the paintComponent(Graphics) method. When you need to paint, you can just iterate over a list of graphical representations of your chess pieces, that are responsible for drawing them in the correct location.
When you want to move a piece, you can use a new thread to periodically update the location of a particular graphical representation, and then repaint (part of) the JPanel. Here is a skeleton:
Here's a screenshot of an implementation I made using this skeleton:
Joined: Jun 28, 2009
Game programming is definitely fascinating but I suspect you're biting off more than you can chew. Have you ever programmed any board game before? If you are an old hand at game programming then it will probably take you between 1 and 2 years to program and after that time you'll have a program that will still get thrashed by every other free computer chess program out there. If you're really good at this sort of stuff (and quite talented at chess) then perhaps in 5 years it might start to win games against these other programs. More likely though will be that you'll decide that perhaps it would of been better to have started with a more achievable goal in the first place.
I'm just trying to offer some honest advice. I'm also assuming that you haven't written game playing programs before and should point out that chess is a fairly complicated game. Programming any game is worthwhile though and certainly teaches you a lot about programming and about the game. Abandoning a project half way through won't really teach you much at all and you'll simply feel less inclined to attempt other projects in future.
My brother wrote a chess program in Java many years ago - it used a publicly available opening book and then used his own evaluation routines. It played so he was happy but I remember him saying it would play like Kasparov while in the opening book and from then on play like Micky Mouse!
If you want a game that will actually play chess at a reasonable level, I'd suggest searching for an open source chess engine. These are designed to be plugged into chess GUIs - they just provide the analysis and an interface to let you access it - so you could plug it into yours.
That way you could concentrate on the GUI and other features you wanted to add to the application.
I doubt OP is trying to write the next Deep Blue. It's definitely a worthwhile project if you're learning to code, regardless of whether it can beat other chess programs.
Some guy here wrote a chess program in less than 3k of code. I played it a couple of times and it beat me thoroughly (I like to think I'm an above-average player). It doesn't have to be some huge project that takes months to write.
Joined: Jun 28, 2009
Luigi Plinge wrote:Some guy here wrote a chess program in less than 3k of code. I played it a couple of times and it beat me thoroughly (I like to think I'm an above-average player). It doesn't have to be some huge project that takes months to write.
OMG It only took him 6 weeks to write! and then a year to clean up the bugs!
It plays surprisingly well for such a tiny program. I made a mistake by trying to get it out of it's opening book early on before realising that it probably didn't have an opening book! I then cheated a bit because I guessed it would want to play tactically (strategic evaluation takes up lots of code) so I just took away all it's mobility and then slowly strangled it. It also only looks 5 moves ahead which I'm sure you could use to your advantage. I certainly enjoyed playing it though. Hats off to the guy.