File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Chess Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Chess" Watch "Chess" New topic
Author

Chess

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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.



Related links
1. How to setup a chess board
2. An online chess game developed in Java...


Jon
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
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


Arcade : Alien Swarm
Board : Chess - Checkers - Connect 4 - Othello
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.

Here's an article on animation: http://www.developer.com/java/article.php/893471. A bit dated maybe. You can use Swing's Timer class rather than their version.

For moves, just make a Move class which contains the relevant data on squares etc, and store them in a List, or Game class that you make up.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

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:

Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
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!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4387
    
    8

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.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

Party poopers!

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.
Mich Robinson
Ranch Hand

Joined: Jun 28, 2009
Posts: 250
    
    1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chess