Click here to Skip to main content
12,951,034 members (57,784 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 17 Feb 2004

Yet Another Chess Board Control

, 17 Feb 2004
Rate this:
Please Sign up or sign in to vote.
Chess control with full move validation including checks, mates, stalemates. PGN and FEN parsers.


I've had fun developing this control and believe it will be useful for many reasons. The first and foremost reason is to get a good peer review. The second is to help share ideas, coding, and of course the code itself. I've been interested in chess for about 7 years now and you can say I'm quite addicted. The positive note is my wife would rather see me out playing chess on a Friday night vs drinking in a bar. I was about 80% done with my code when I noticed that Chris R. Chapman had put out his version of a control, using the famous Bitboards (Good job Chris!). So any one interested should check out his control as well. There are two main differences that you will note: Chris pre generates his attack boards where as I generate mine on the fly, I draw the chess board in an offscreen buffer then splash it to the screen, and finally I trap the mouse clicks and locate the corresponding squares.

In regards to the Bitboards I've always been curious to the differences in speed between the two approaches. There has been some arguments in the online 'chess' groups that the pre-generated versions are no faster than the build as you go, I personally have not tested this but now maybe I can. I've found my code to execute very nicely and do not see any performance problems. I believe if I change anything in performance it would be the mouse click / square location routines. I think simply dividing the board into quarters and associating each quarter with the underlying squares should speed things up some, but to be honest it runs pretty fast now so I may be lazy and skip this part. I have successfully validated quite a few different and weird checks, mates, and stalemates so I believe the move validation is pretty solid. If you find any problems let me know, email me FEN strings. You can find some of the FEN strings in the header for StandardValidation.isKingCheckedMatedStaled method.

I draw the chess board in the background and then splashed to the screen and this gives me pretty smooth piece movement. The intentions here were to give the drawing code the ability to be called from a web server for display on an HTML page. That's why it's hooked up to the FEN events, pass the string and generate the graphics. See below discussion on events.

There is also a PGN brute force parser that has example code in the demo program that converts the PGN to XML. It will save the file into the same location as the PGN but with the .XML extension. The intent here was to use the XML for the format in the background for displaying, navigating, and annotating chess games. It can also be used to display an annotated game in HTML with a little XSL processing (if anyone writes one let me know). I have some example pgn files saved in the directory with the control for testing, a couple with multiple variations and comments. The game navigation is the next thing I'm going to work on finishing. Please be aware that I'm unsure how the XML parser will work with a totally junked file and I have not tested it 100% as I'm just starting to working on the game navigation control.

Note the VCR buttons do not move through a game yet. I'm working on that in my spare time utilizing the XML generated above.

There is some cleanup I still need to perform so don't be too critical of the code.


I would recommend that you read articles on Bitboards and their use in chess computer. Crafty source code is an insane place to look if you enjoy reading C code. But the easiest method is to perform a simple search on the topic. But beware, you may get redirected to this very column where you will find yourself in a never ending do/while loop.

Seriously though I hope to show a use of interfaces and events in my first attempt and this article. In regards to interfaces and events, I find that they go very well together. I first think of the events I want a particular object to implement, then I group these event methods into an interface. I then define a bare minimum interface for the object itself in which I declare two methods: addEvents(ievents) and removeEvents(ievents) where the parameter ievents would be the event methods/interface I previously created. The body of the methods addEvents() and removeEvents() simply hook/detach up all of the methods defined in the interface to the object producing the events.

To better understand my point look at the code in the file ChessInterfaces.cs. In particular find the IPosition and IPositionEvents interfaces. The IPosition defines the methods that a class must implement to allow the events defined in IPoisitionEvents to be hooked up and ready for action. Thus in the interface IPosition we have the two methods named: IPosition.removeEvents(IPositionEvents ievents) and IPosition.addEvents(IPositionEvents ievents). Now open up the file FenNotation.cs. Here you will find public class FenNotation : IPosition which wants other classes to be aware of his ability to parse a FEN string. Now open up the two files ChessBitmap.cs and StandardValidation.cs to reveal the following: public class ChessBitmap : IPositionEvents and public class StandardValidation: IPositionEvents, IValidation . Making sense yet? Ok just a little more to go. Once you've studied the aforementioned code, mainly the addEvents / removeEvents, open the file: ChessBoard.cs and find the code in the constructor:

coFen = new FenNotation();
// Assign our graphical and bit board to
// the FEN class so that we can
// capture the events.

Now you should be able to see what we've accomplished here: First with a little bit of extra work I've made it very simple for others to use the events of my object, Second I've made it very easy for me to use the events of my object. To create another object that listens to the events I simply declare an object and have it implement the interface for the events that I am interested in receiving. Remember VS will ask you if you want the interface skeleton code generated, simply say yes and write the body of the code to handle the events. Then it is a simple function call to hook that object up to receive events. For another example look at the PGN parser and the XML conversion that takes place in the sample applications.

Using the code

Couldn't get any easier: Compile the projects and let em rip....

Seriously though it is that easy. Just open the solution in the ChessBoardCtrl directory named: ChessBoardCtrl.sln. This solution has references to the demo applications that hosts the control and implements some of the features. By looking at the demo program you can get a good understanding of how the control works, play with that first then try it your self. I'll update this more at a later time (when I have a little more time). Until then let me know what you guys think!


Points of Interest

If there is any particular area you want me to discuss, just post a message!


  • This is the first release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Developer for 12+ years in:
Assembly, C, C++, Java, .NET and some others.
Love Chess and Music

You may also be interested in...


Comments and Discussions

QuestionStalemate problem Pin
Member 1221030115-Mar-16 2:54
memberMember 1221030115-Mar-16 2:54 
QuestionDraw States Pin
Kasparov924-Sep-15 6:05
memberKasparov924-Sep-15 6:05 
QuestionYour GUI, my Engine Pin
Palavos24-Sep-11 2:35
memberPalavos24-Sep-11 2:35 
Generalhepl me Pin
hieunh8424-Sep-07 18:05
memberhieunh8424-Sep-07 18:05 
QuestionPuzzle with pictures Pin
BarabashNazar19-Dec-06 12:07
memberBarabashNazar19-Dec-06 12:07 
GeneralCode Example Pin
AngelPortal23-Nov-06 23:29
memberAngelPortal23-Nov-06 23:29 
GeneralOld Studio.NET Pin
GWG30-Mar-04 6:09
memberGWG30-Mar-04 6:09 
GeneralRe: Old Studio.NET Pin
Heath Stewart30-Mar-04 6:34
editorHeath Stewart30-Mar-04 6:34 
GeneralRe: Old Studio.NET Pin
GWG30-Mar-04 10:08
memberGWG30-Mar-04 10:08 
GeneralRe: Old Studio.NET Pin
Heath Stewart30-Mar-04 10:19
editorHeath Stewart30-Mar-04 10:19 
GeneralRe: Old Studio.NET Pin
Cafechess30-Mar-04 10:23
memberCafechess30-Mar-04 10:23 
GeneralRe: Old Studio.NET Pin
GWG30-Mar-04 11:00
memberGWG30-Mar-04 11:00 
GeneralGet Updates @ Pin
Cafechess26-Mar-04 5:03
memberCafechess26-Mar-04 5:03 
QuestionRe: Get Updates @ Pin
mmwlada31-Oct-07 11:46
membermmwlada31-Oct-07 11:46 
AnswerRe: Get Updates @ Pin
tobis4523-May-10 1:58
membertobis4523-May-10 1:58 
QuestionAny topic you want me to elaborate on? Pin
Cafechess21-Feb-04 10:22
memberCafechess21-Feb-04 10:22 
AnswerRe: Any topic you want me to elaborate on? Pin
rjn11-Feb-05 4:20
memberrjn11-Feb-05 4:20 
GeneralRe: Any topic you want me to elaborate on? Pin
Cafechess11-Feb-05 4:51
memberCafechess11-Feb-05 4:51 
GeneralI suggest you add hyper link to Pin
Zhefu Zhang18-Feb-04 16:29
memberZhefu Zhang18-Feb-04 16:29 
GeneralRe: I suggest you add hyper link to Pin
Per Søderlind18-Feb-04 21:23
sussPer Søderlind18-Feb-04 21:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 18 Feb 2004
Article Copyright 2004 by Cafechess
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid