Saturday 29 October 2011

Hosting chess games in your blog posts

Finally I have found the chess game viewer I wanted in order to publish my own PGN games in the blog! The name is Chess Tempo PGN Viewer and it is well written, fast, supports annotations and variations and is very configurable. Most of all, it is all Javascript (sorry for the occasional Java prompts. I almost caved in and did what I swore I wouldn't ever do: have Java applets in my blog).

Please tell me if you have issues with the new chess viewer.

The Stonewall attack chess opening

Luckily for me, some chess videos are for beginners like me. Here is one from OnlineChessLessons, describing a simple, clear opening called The Stonewall Attack. After watching the video I played a game with my trusted Nokia phone and managed to create a game starting with this opening. I then analysed it using chess engines Houdini and Rybka and annotated it manually. Check it out, including the variations.

Video first:

Make sure you also follow the article attached to the video.

And now my game:
[Event "29/10/2011 1:08:49 pm"]
[Date "29/10/2011"]
[White "Siderite"]
[Black "Nokia Easy5"]
[Result "1-0"]
[ECO "D05"]
[Opening "Colle"]
[Variation "5.c3 Nc6 6.Nbd2 Bd6 7.O-O O-O"]
[TimeControl "600"]
[Termination "normal"]
[PlyCount "59"]
[WhiteType "human"]
[BlackType "computer"]


1.d4 e6 2.e3 d5 3.Bd3 Bd6 4.f4 Nf6 5.Nd2 O-O 6.Ngf3 c5 7.c3 Nc6 8.O-O c4 9.Bc2 Ng4
{At this point, the engines suggest Bxh7, a classic sacrifice.}
10.Qe2
{However, I moved the queen to defend e3.}
( 10.Bxh7+ Kxh7 11.Ng5+ Kg8 12.Qxg4
{This variation wins the h7 pawn, but moves away from the spirit of the original game.}
12...Qf6 13.e4 ) 10...f5
{At this point, engines suggest Ne5, followed by knight exchange from black.}
11.h3
{I chose to shoo the knight away, but weakening g3, where the knight would love to come later.}
( 11.Ne5 Ngxe5 12.dxe5 Be7 13.b4 ) 11...Nf6 12.Ne5 Qa5 13.g4 fxg4 14.hxg4 Qb6
{engines would want me to attack the knight on f6 before moving the rook out.}
15.Rf2 ( 15.g5 Bxe5 16.dxe5 Ne8 17.Kg2 g6 18.b3
{Engines decide to try a queen side attack as well, in order to weaken the black pawn chain. I was not interested in that.}
) 15...h6
{engines suggest attacking the rook and with Ng6, which is a natural attacking move and a lovely outpost.}
16.Rh2 ( 16.Ng6 Rf7 17.g5 Nh7 18.Qh5 Ne7 19.Nxe7+ Rxe7 20.gxh6 Nf8 21.Rg2 Qc7 22.Qg5
{However at this point the game moves into queen side attacks and a slower attacking pace.}
) 16...Ne7 17.g5 hxg5 18.fxg5 Bxe5 19.dxe5 Nd7
{engines suggest now a beautiful move: Rh8, followed by a munching of black pieces or/and mate. Make sure you check out the variation.}
20.Nf3 ( 20.Rh8+ Kf7 ( 20...Kxh8
{Taking the rook leads to a quick mate.}
21.Qh5+ Kg8 22.Bh7+ Kh8 23.Bg6+ Kg8 24.Qh7# ) 21.Qh5+ g6 22.Bxg6+ Nxg6 23.Qh7+ Ke8 24.Qxg6+ Kd8 25.Rxf8+ Nxf8 26.Qf6+ Ke8 27.Nf3
{Try this variation on a chess engine to see it to the end. White is only one pawn up, but it is a passed one. The king is safe as well.}
) 20...Rb8 21.g6 Nf5 22.Rh3 Nh6 23.Kh1
{I felt like the pin on e3 was annoying and stopping me from using the black bishop. The engines recommend moving Qh2 instead, which is much better.}
( 23.Qh2 Nc5 24.Ng5 Ne4 25.Bxe4 dxe4 26.Nh7 Qc7 ( 26...Rd8
{If you wanted to know why black did not move the rook when attacked by the knight, follow this variation through.}
27.Rxh6 Rd1+ 28.Kg2 Qd8 29.Nf6+ Kf8 30.Rh8+ Ke7 31.Qh4 Rd2+ 32.Bxd2 b6 33.a4 Qxd2+ 34.Kh1 Qe1+ 35.Rxe1 Bb7 36.Rxb8 Bc8 37.Rxc8
gxf6 38.Qh7# ) 27.Nxf8 Qd8 28.Rxh6 Qg5+ 29.Kf2 Qxh6 30.Qxh6 gxh6 31.Nh7
{At this point white is a knight up, but what a boring continuation.}
) 23...Nf5 24.Qh2 Ng3+
{Engines suggest I move the king and concentrating on the attack, but I took the knight with the rook.}
25.Rxg3 ( 25.Kg1 Qxe3+ 26.Bxe3 Ne2+ 27.Kg2 Nf4+ 28.Bxf4 Rf5 29.Rh8# ) 25...Re8 26.Qh7+ Kf8 27.e4
{At this point, a mate in 8 is found.}
27...Qc7
{But with this move, mate will happen in 4.}
28.Bg5 Nf6 29.exf6 Rd8 30.Qh8# 1-0


Enjoy!

P.S. I am currently looking for a method of displaying the game as I want it on the blog: dynamic, with annotation and variation support, preferably something that is not Java and optimally something that reads the PGN from a span and replaces it with a nice looking chess interface. Right now, the usual game engine fails for some reason I need to analyse.

Friday 28 October 2011

Optimizing Visual Studio solutions and projects or how good intentions can pave a lot of road

I've had a horrible week. It all started with a good Scrum sprint (or so I thought) followed by a period of quiet in which I could concentrate on my own ideas. And one of my ideas was to optimize the structure of the solution we work on, containing 48 projects, in order to save space and compilation time. In my eyes, I was a hero, considering that for a company with tens to hundreds of devs, even a one second increase in speed would be important. So, I set up doing that.

Of course, the sprint was not as good as I had imagined. A single stored procedure led to not less than four bugs in production, with me being to blame for them all. People lost more time working on reproducing the bugs, deploying the fix, code reviewing, etc. At long last I thought I was done with it and I could show everyone how great the solution looked now (on my computer) and atone for my sins.

So from a solution that spanned from 700Mb clean and 4Gb after compilation, I managed to get it to a maximum of 1.4Gb. In fact, it was so small I could put it all in a Ram disk, leading to enormous speeds. In comparison, a normal drive goes to about 30MB per second, an SSD drive (without encryption) goes to about 250MB/s, while my RamDisk was running at a whooping 3.6GB/s. That sped up the compilation and parsing of files. Moreover, I had discovered that MsBuild has this /m parameter that makes it use more processors. A compilation would go to about 40 seconds, down from two minutes and a half. Great! Alas, it was not to be so easy.

First of all, the steps I was considering were simple:
  • Take all projects and make them have a single output folder. That would decrease the size of the solution since there would be no copies of the .dll files, Then the sheer speed of the compilation would have to increase, since there would be less copying and less compilation.
  • More importantly, I was considering making a symlink to a RAM drive and using it instead of the destination folder.
  • Another step I was considering was making all references to the dll files in the output folder, not to the projects, allowing for projects to be opened independently.


At first I was amazed the solution decreased in size so much and I just placed the entirety of it into a RAM drive. This fixed some of the issues with Visual Studio, because when I was selecting a file through a symlink to add as a reference, it would resolve to the target folder instead of the name of the symlink. And it was't easy either. Imagine removing all project references and replacing them with dll references for 48 projects. It took forever.

Finally I had the glorious compilation. Speed, power, size, no warnings either (since I also worked on that) and a few bug fixes thrown in there for good measure. I was a god! Then the problems appeared.

Problem 1: I had finished the previous sprint with a buggy stored procedure committed to production. Clients were losing money and complaining. That put a serious dent in my pride, especially since there were multiple problems coming from both less attention to how I wrote the code to downright lack of knowledge of the flow of the application. For the last part I am not really the only one to blame, but it was my responsibility.

Problem 2: The application was throwing some errors about the target framework of a dll. It was enough to make me understand a major flaw in my design: there were .Net 3.5 and .Net 4.0 assemblies in the solution and placing them all in the same output folder would break some build scripts. Even worse, the 8 web projects in the solution needed to have their output in the bin folder, so that IIS would find them. Fixed it only to see the size of the solution rise back to 3Gb.

Problem 3: Visual Studio would not be so smart as to understand that if a project is loaded, going to the declaration of a member in the compiled assembly means I want to see the actual source, not the IL code. Well, sometime it worked, but sometimes it didn't. As a result I restored the project references instead of the assembly references.

Problem 4: the MsBuild /m flag would do wonders on my machine, but it would not do much on the build server. Nor would it do its magic on slower, less multiprocessor computers than my own.

Problem 5: Facing a flood of problems coming from me, my colleagues lost faith and decided to not even try the modifications that removed the compilation warnings from the solution.

Conclusion: The build went marginally faster, but not enough to justify a whole week of work on it. The size decreased by 25%, making it feasible to put it all in a RAM Drive, so that was great, to the detriment of working memory. I still have to see if that is a good or a bad thing. The multiprocessor hacks didn't do much, the warnings are still there and even some of my bug fixes were problematic because someone else also worked on them and didn't tell anyone. All in a week's work.

Things I have learned from all this: Baby steps. When I feel enthusiasm, I must take it as a sign of trouble. I must be dispassionate as an ice cube and think things through. If I am working on a branch, integrate the trunk into it every day, so as to not make it harder to do at the end. When doing something, do it from start to finish, no matter what horrors I see while doing it. Move away from Sodom and not look back at it. Someone else will fix that, maybe, you just do your task well. When finishing something, commit it into the source control so it can easily be reverted through a single atomic operation.

It is difficult to me to adjust to something that involves this amount of planning and focus. I feel as if the chaotic development years of my youth were somewhat better, even if at the time I felt that it was stupid and focus and planning was needed. As a good Romanian, I am neurotic enough to see the worst side of everything, master at complaining about it, but incapable of actually doing something. Yeah... this was a bad week.

Saturday 22 October 2011

Two interesting news and no news

No news from my personal or work fields. However, I've found two interesting news just today and I wanted to share them.

First, the development of a camera to capture pictures that you can focus later. Although I have heard of solid metal lenses that would be less than 1$ to make and would achieve the same effect, the only actually functioning system I've heard of so far is the Lytro Living Picture camera. Here is an Ars Technica article on it and here is a YouTube video demo.

The second news is more IT related. It involves the cryptographic standards for XML, as defined by W3C. They failed! Here is an article about how they were cracked by using a vulnerability in the Cipher Block Chaining and here is a link to their press release.

Monday 17 October 2011

How Life Imitates Chess, by Garry Kasparov

Book cover
The complete title of the book is How Life Imitates Chess: Making the Right Moves, from the Board to the Boardroom, which is a mouthful, but very precise. It does explain how principles of chess, economics and politics apply in all three fields and how Garry Kasparov has evolved from chess player to world champion and, nowadays, into a political anti-Putin figure.

If you ask me, abandoning chess to get into business and worse, politics, it's a complete loss. However I do understand the guy, he got bored. Someday I may abandon computer programming.

Back to the book, though, it felt a lot like The Art of Learning, also written by a brilliant chess player (who incidentally also abandoned chess... hmm). It was more precise, most logical, though, looking at things from a more of a clinical perspective. I would have wanted to learn more about Kasparov's relationship with Karpov, for example, since he is always calling him his nemesis, but never says anything about how he felt about the guy.

This book is peppered with good advice, historic comparisons and great quotes from chess players and great men. Also, short descriptions of the relationship between famous "chess pairs" are giving the book an extra chess dimension. All in all I recommend it highly, although it felt more like a useful reference than a soul book like The Art of Learning.

Thursday 13 October 2011

Tuesday 11 October 2011

Optimal use of case insensitive Dictionary in .Net

A question arose at the office today: What is faster? Using a Dictionary<string,object> with a StringComparer.OrdinalCaseInsensitive constructor parameter or using a normal constructor call and using ToLower on the key before using it. The quick answer: using ToLower on the key.

The longer answer is that StringComparer.OrdinalCaseInsensitive implements IEqualityComparer<string> by using a native code function for GetHasCode(), which is very efficient. Unfortunately, it must use the case insensitive string comparison on both input key and stored keys, while calling ToLower on the keys before using them makes the comparison only once.