Monday, October 5, 2009

The Chessboard

OK, before I start talking about the program itself, I'll just say a few words about how the project will proceed, to make it easier for you to follow along.

The program will be written in Sun java. It will be a complete program that will include chess engine, a GUI, database functionality, and maybe even some live or ePostal play between human opponents. So the program will be more than just an opponent, it will be a useful tool for chess study. However, I do not expect to be competitive with existing freeware applications such as Scid, Chess Assistant Light, or ChessBase Light. This is primarily a hobby and a learning project for me, and hopefully others will benefit from the blog.

I'll divide the project into two phases.

1. The alpha phase will see quantum leaps in the program as I add functionality. A lot of the work will be re-used from the previous version of the program, so this phase will proceed fairly quickly.

2. The beta phase will proceed more slowly, with incremental changes in functionality and playing strength as time, and my own knowledge level, permits.

Each post to this blog will correspond to a version, such as alpha1, alpha2, alpha3 etc. The link below will connect you with my website for the project, where you will find some brief comments about each version, a link to a web applet version of the current release, and a downloadable zip file containing complete source code.

For now, I have disabled comments for this blog, I may change that in the future as I become more familiar with the process.

OK, on to the program.


The first step is to design a chess board. First comes the GUI, without that I am unable to use the program. I have created two significant java classes for the GUI. The Table class, which extends JPanel, will contain as much of the GUI as possible. Table will have three sections: the board, a control panel for buttons and check boxes, and text area for program output. During development the text area will be useful for displaying diagnostics information, after which it will display chess notation. Table will also contain most of the mouseListeners and Event Handlers.

The Board class is a separate class that also extends JPanel. It is responsible for displaying the board and pieces on the screen. The Board class will not contain any event handling code, but the board object which is owned by the Table class, will have mouse listeners attached to it there. The Table will act as the listener for the board, and contain all the event and mouse handlers.

There are two useful arithmetic formulas that are introduced here. First of all the java graphics context that is the chessboard is measured in pixels. The origin (0,0) is top left, bottom right is (width,height). Then there is the numerical enumeration of the squares themselves. Top left will be (0,0), bottom right will be (7,7). The formula that links these two systems is as follows:

squareSize: ss = width/8.

for any row,col combination in chessboard space, the xy co-ordinate of the top left corner of the square is x = col*ss, y = row*ss. The top left xy co-rdinate of the square tells java where to draw the square, and later where to draw the piece. The row/col co-ordinates tell us what color to paint the square. If (row+col)%2 = 0, it is a light colored square, otherwise it is a dark colored square.

In the next post, I will define the internal position array, create the chess pieces, and add the mouse event handlers which allow us to move the pieces around on the board.

At this point, programmers who are following along should be familiar with the chess comcepts of algebraic square notatation, and fen position notation.

Thanks for reading this far, I hope you find this journey as interesting as i will.

No comments:

Post a Comment