X

Geertjan's Blog

  • August 13, 2005

Playing Chess with NetBeans IDE (Part 1)

Geertjan Wielenga
Product Manager
There's a cool web service against which you can play chess. You can find the web service here. Since NetBeans IDE comes with a built-in web service client, you don't need to do any programming at all to play against this web service. However, you do need to be reasonably familiar with Forsyth-Edwards Notation (FEN). Once you're familiar with FEN, you can use the IDE's built-in client to send strings like this to the web service:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1

The above string is the white opening move of E2-E4 in FEN. Each of the letters and numbers above means something special (for example, the "w" indicates that the move was made by white). But, how on earth do you, presumably a FEN-philistine like me, translate a move such as 'E2-E4' to a garbled piece of FEN so that you can play chess with the web service? Short of spending the rest of your life studying FEN, there's got to be a better solution. And there is! Get yourself a FEN generator like this one (click to enlarge):

Download it here and make the first move (assuming you're white) like this:

  1. Leave the "number of move to play" at 1.
  2. Make a move on the board by right-clicking a piece on the board and clicking the square where you want to move it to.
  3. Click "active color to play", so that the active color to play is set to black.
  4. Copy the resulting FEN-String at the bottom of the dialog box into the IDE's built-in client.

When you take the steps above, you'll end up with a FEN string that must have a "b" (not a "w"), because you want to tell the web service that black is active -- i.e., the web service is black. For example, if you move E2-E4 using the instructions above, you should have this FEN string:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1

After completing step 4 above, click Submit and then you'll see the web service's response (click to enlarge):

Now make black's move on the board, following the response received from the web service. Once you've done that, the first move is over. Make your second move (and all subsequent moves) like this:

  1. Increment the "number of move to play".
  2. Make a move on the board.
  3. Click "active color to play".
  4. Copy the resulting FEN-String at the bottom of the dialog box into the IDE's built-in client. For example, if I decide to take black's pawn, the FEN string sent to the web service will be as follows (note that there's a "b" to indicate that it's black's move and that there's a "2" to indicate that it's the second move of the game):

    rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2

Now repeat this ad infinitum until the game is over... Bear in mind that each of the manual steps above needs to be reproduced programmatically in our own implementation -- therefore, it's a good idea to go through the above process a few times, make about ten moves, until you understand what needs to happen from the programming side. So, once you've got the basics, you need to implement a client of your own. Before setting up an entire chessboard and pieces, lets make sure everything works in a very simple implementation. Here's a very simple implementation (click to enlarge):

What I do is this -- I make a move in the FEN converter at the top of the page, I copy the generated FEN string, and then I paste it in a JSP page such as the above. (I implemented it such that you don't need to specify whose move it is -- i.e., it's always black, because you're always white in the current implementation.) Then specify the current move number. And then click "Move!" When you do this, you send the string to the servlet. The servlet sends the string to the web service. Thanks to NetBeans IDE, I had to do virtually no coding at all -- I know next to nothing about SOAP, and I didn't need to know any more than that, because the IDE generated all the code needed for making the connection to the web service. The only thing I had to think about was how to send the FEN string from the JSP page to the servlet and then pass it on as an argument to an operation made available by the web service. So this is literally all I added to the servlet:

String getMove = request.getParameter("move");
String numberOfMove = request.getParameter("numberOfMove");
out.println("<h1>Black's Response (" + numberOfMove + ")</h1>");
String wholemove = getMove + " " + "b" + " KQkq - 0 " + numberOfMove;
String nextMove = getServiceSoap().getNextMove(wholemove,"0",3);
out.println("<b>" + nextMove + "</b>");
out.println("<p><input type=button value=\\"Next Move!\\" onClick=\\"history.go(-1)\\">");

And this is what the servlet spits out after receiving the response from the web service:

So, okay, this is a long way from a fully fledged chess game, but it's a lot closer than it would have been if I had to (a) create the chess generator (which I didn't because the web service was already there) and (b) program all the wiring between the client and the web service (which I didn't because the IDE generated all the code). Now "all" I need to do is create the chessboard and chess pieces -- and then somehow translate the returned moves from the web service to visual changes on the chessboard...

Join the discussion

Comments ( 3 )
  • guest Sunday, August 14, 2005
    Hi Geert-Jan, This website could be very handy to complete the application:
    Internet Chess Toolkit - http://ictk.sourceforge.net/
    The Internet Chess ToolKit is a Java based set of libraries and widgets useful for performing common tasks such as reading SAN, PGN, FEN, and generating legal moves. The net libraries convert ICS (Internet Chess Server) output into java events. This library is licensed under the GPL v2
  • Geertjan Sunday, August 21, 2005
    Thanks for the tip! I've looked at that Toolkit but don't understand how to use it to create FEN strings from positions on the board. Have you used it? Can you give some advice of how to do it?
  • Pablo Rattin Monday, June 20, 2011

    Hi,exelent blog!I need some information about graphics in Java Netbeans,I need make a chessboard with pieces and all the movements,but I don't find any information about,you can send some piece of code or put me in the right direction?

    Thanks in advance and Friendely regards from Uruguay!


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.