My Chess (another chess application)
I came up with the idea of My Chess when I wanted to play games with my friends and I noticed that I don’t have time to do that :-) So I decided to write an application that allows me to play chess with my friends just by using small amount of time every now and then (approach is pretty different compared to the “normal” chess). Small amount of time means something like 10 seconds per move so and not more... Of course if you WANT to use more time you’re allowed to do that.
Here is list of other features that I wanted from My Chess:
- Possibility to play offline. In another words I don’t want to think about the transport at all. I would just like to pass the transport related stuff to someone else.
- Mobile version of the application (HTC Touch Pro since I have one)
- “Social chess”: I wanted to have possibility to add comment when making move. Just because I want to tell to my opponents how good move I have made and remind them about their bad ones :-)
- Game should be fast and easy. You should be able to make your move with just few clicks.
I decided to go with C++ since I didn’t want to take start up delay of .NET application on my mobile device. This one was easy decision :-)
Then I thought that I could use email as my transport because it automatically solves issues with the connectivity. On desktop I could rely on Outlook and mobile device I could rely on Pocket Outlook. More about technical details later in this post.
But then I was forced to think proper way to solve how to transmit the game state. I mean that how could I pass the current state of the game to my opponent so that it would be easy for him/her to continue from that state. Use of attachment at the email was one option. But the story for the end user on mobile device with attachments isn’t that good so I didn’t use effort on this option. I took another approach which was to define URL format that I could use to serialize the game state. Here is one example game state:
Game state uses my protocol which I have mapped to application (Registering an Application to a URL Protocol on MSDN). Of course My Chess is configured according to the article so that it will called when my protocol is launched. Here is example .reg how custom application can be mapped to protocol:
So it really is that simple. Just define own key under HKEY_CLASSES_ROOT and set path to your application and it will be called automatically by the OS.
If you look the same with regedit it looks like this:
If you now open up links starting with my protocol they will eventually take you to the configured application. But you probably see this kind of security dialogs before your own application starts (at least for the first time):
Just uncheck “Always ask before opening this type of address” and press Allow.
Same goes with this dialog. Just check “Do not show me the warning for this program again” and press Allow.
NOTE: Make sure you understand the consequences of allowing protocol to map to application. If you’re application doesn’t validate data correctly it may lead to security issues when someone passes malicious data to your application. Remember you have been warned!
After that you should finally be at your application and you can use the information passed on the command line to execute the required actions.
So basically now I have tools to build the application... But I haven’t yet solved the “creating the email message” part of this solution. Remember I have both desktop application and mobile application to built.
On desktop I’m using Outlook as my mail transport and it has pretty good instructions in here:
Using Outlook on desktop with C++ is almost as easy as using it from .NET. It’s straight forward thing. So far so good right!
On mobile device things aren’t quite that easy since you need to use MAPI for sending the mail message. Here are few examples for that:
But as always things aren’t as easy as they first seem to be. Since even if I did register my protocol at the mobile device it didn’t work as I expected. The problem was that Pocket Outlook always launched the URL to the default browser even if I had registration done to my own protocol. After some digging I found out that there are many security features that prevent that from happening. So in order to do it the right way I should have to edit/configure some files to allow protocol to be launched correctly. But since this is more concept than production kind-of-application I took another route... I chose to *hijack* MMS protocol for my custom application :-) I did that because it’s one of those protocols that are configured properly to the system. It normally launches Windows Media Player but I changed that to launch My Chess instead. This is the reason why you see two links at the emails underneath. The URL structure is exactly the same... Only the protocol part is either my or mms depending on the link. I admit that this one was dirty hack...
Now we have solved pretty much everything. But what about graphics then? Well I used images from Wikipedia at the Chess piece article (example King where you can see the licenses as well). Graphics is another interesting difference between desktop application and mobile application since GDI+ headers and libraries aren’t available for the mobile environment :-( But I wanted to use GDI+ on my desktop application even if I was forced to use GDI at the mobile application. Yeah I know... someone else would have built them using the same codebase but not me.
I used following functionalities from GDI+: Bitmap, Graphics (DrawImage, DrawCachedBitmap, DrawRectangle, FillRectangle), Graphics::FromImage, FontFamily, Font, SolidBrush, Pen, CachedBitmap etc.
I used following functionalities from GDI: DeleteDC, DeleteObject, CreateCompatibleDC, CreateCompatibleBitmap, SelectObject, Rectangle, TransparentBlt, BitBlt etc.
Are you already eager to see the final outcome? Well here it goes:
Mobile version (screenshot from emulator. It looks better on real device):
So how does it work in real life? Here is small example:
First if start My Chess without parameters so it creates automatically new game (in this example I play against myself):
In order to update the registry correctly for the protocol My Chess needs to be run for the first time in the “Run as administrator” –mode:
So now you have new game and you can use mouse to make the move (see simple coloring to indicate the move):
When you have done that you can press Send menu which then creates email message for you where you can add your own comment to your move (everything else is generated.. you just need to add last line):
Now the game state is passed to the opponent as just normal email. When opponent receives it and presses the link following security prompt is displayed (Note: on mobile device application is launched directly without any dialogs):
After that My Chess is opened and it’s opens the current game state directly:
And now you’re ready to make your move and then send it to the opponent with some nasty comment of course.
Current status of the My Chess is pretty much experimental / concept state since I haven’t yet implemented all the necessary functions to make it _real_ chess application:
- Move validation is currently missing and all special moves haven’t been implemented (promotion is only special move that has been implemented)
- Currently there isn’t way to see previous move or move history (it’s stored behind the covers but it’s not visible). I’m going to implement animations so that when user opens My Chess something like last 3 moves of the game would be animated. And user could browse the move history (with animations of course!) to see what has happened previously on the game.
- I could CC the email message to email box that could be read by machine. It then could insert the game state to database, create some statistics about the games, history of games etc. This brings more possibilities to extend the game in the future. But I probably leave this to someone else...
By the way... you might have noticed that I used chess as in my URL to define which game should be launched from the my protocol. This indicates that something else might come in the future. That remains to be seen!
How could you use information provided in this blog entry for your own needs? Well here’s one: I have many times heard that customers need to “Send shortcuts” from their existing applications to each other. This could be way to do that. Just add functionality to “copy shortcut” from applications current state and then allow user to send it. Then at the receiving end you could create “proxy application” that interprets received information and opens the application into same state that the other user had. It could be pretty good improvement in many cases.
Anyways... Happy hacking!