h1

Finally, a win this year

February 6, 2010

It was a weird experience. Playing in a league match, the opposing team’s top board (who I was playing) was ranked lower than our board 5. Despite that, our game should have really been a draw but I squeaked by with a win in time trouble and we won the match 4-1. The game can be replayed here (I was black).

1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 a6 6. Bg5 e6 7. Bxf6 Qxf6 8. g3 Nc6 9. Nxc6 bxc6 10. Bg2 d5 11. exd5 cxd5 12. Qf3 Qd8 13. a3 Be7 14. O-O O-O 15. Rfd1 Qc7
The opening has been quite dry and I was trying my best to keep as many pieces on the board and try and outplay my opponent. Here, I initially thought that he might be able to play 16. Rxd5?!. I run into trouble after 16… exd5 17. Nxd5 Bb7 but then realized that 16… Bb7 17. Rd7 Qxd7 18. Qxb7 and black is better.
16. h4 Rb8 17. Rab1 Bb7 18. Qe2 Bc5
I debated between the text move and 18… Bf6, unsure as to which diagonal the bishop should sit on.
19. Qd3 Rfc8 20. Ne2 Qb6 21. Rf1 a5 22. Qd2 Ba6 23. c3 Qc7
23… a4! never crossed my mind, but it fixes white’s pawns and gives me concrete targets to aim at. From this point on, my opponent loses the thread a bit, letting his pieces stay passive and allowing me to improve my position.
24. b4? Ba7 25. Kh1 Bxe2 26. Qxe2 Qxc3 27. Qb2 axb4 28. axb4 g6 29. Rfc1 Qxb2 30. Rxc8+ Rxc8 31. Rxb2 Kg7 32. b5 Bd4 33. Rd2 Rc4 34. Bf1 Rb4 35. Kg2 Kf6 36. Rd1 e5 37. Be2 h5 38. f3 Rb2 39. Kf1 Ke6 40. f4 Be3 41. fxe5 Kxe5 42. Ke1 Ke4 43. Kf1 f5 44. Rd3 f4?
44… d4 was probably better, not yet allowing the bishop check on d3, as happens later in the game.
45. gxf4 Bxf4 46. Rd1 d4?
And this is definitely an error, and now the position is a dead draw! 46… Rb3, not allowing Bd3+ was needed.
47. Bd3+! Kf3 48. Bxg6 Rxb5 49. Bxh5+??
My opponent now commits a fatal blunder, in deep time trouble – he was down to less than a minute by this point. 49. Rxd4 maintains the equilibrium.
49… Rxh5 50. Rxd4 Rxh4 51. Rd3+ Be3 0-1
Mate follows.
h1

Lost on the 6th move

February 6, 2010

That’s what happened to me a week ago. I have played the same opponent 4 times before, but each time with black(here, here, here, and here). I was feeling optimistic that I would do well with white but fell for an opening trap, as you would have guessed, on the 6th move. I played on for a while longer, hoping for a blunder in return, but my opponent played solidly (as you would expect) and it was soon time to end the misery. Replay the game here.

1. e4 e5 2. Nf3 Nf6 3. Nxe5 Nc6?! 4. Nxc6 dxc6 5. e5
Now I knew my opponent played the very rare 3rd move, sacrificing a pawn for insufficient compensation. I had played this in a few friendly, rapid games before and always went for the “safe” approach with 5. d3 and tried to take the steam out of his counter-play in the opening. For this game, I decided to go for the move that Fritz advised!
5… Ne4?!
And of course he plays the one move that Fritz never even considered, and I didn’t take a look at this.
6. d3??
I only looked at 6…Nxf2 as his main sacrificing alternative and considered it ok. I completely missed his next move. 6. d4! and white holds a significant advantage.
6… Bc5!
I looked long and hard at 7. Be3 Bxe3 8. fxe3 Qh4+ 9. g3 Nxg3. It was a choice between 2 bad variations and I chose the one that I thought might lead to positions where I could try and swindle him. Alas, no such thing happened and he wrapped up the point efficiently.
7. Qf3 Nxf2 8. b4 Bb6 9. c3 Nxh1 10. g3 Nf2 11. d4 Ng4 12. Bc4 Qe7 13. h3 Nxe5 14. dxe5 Qxe5+ 15. Be2 Be6 16. Bf4 Qf6 17. Qe4 Qf5 18. Nd2 O-O-O 19. Qg2 Qxh3 20. Qxh3 Bxh3 21. Kd1 Rhe8 22. Bf3 Bf5 23. Kc1 Be3 0-1
h1

What is your coding style?

February 6, 2010

I wanted to traverse a list of strings until a certain string occurred, then do something with that index. My code was:

int i;
for (i=0; i<list.size() && !("string".equals(list.get(i))); i++);
// do something with i

During the build, where we use several tools and metrics, Checkstyle complained that an empty statement in the for-loop was badness. Why?

This of course led to an argument about whether this is easy code to read. Checkstyle was happier when the above code was refactored to:

int i;
for (i=0; i<list.size(); i++)
  if ("string".equals(list.get(i)))
    break;
// do something with i

And another way to write the same code is:

int i;
while (i < list.size() && !(“string”.equals(list.get(i))))
i++;
// do something with i

The 1st and 3rd ways are crystal-clear to me and intuitively, the way I write (and think about) code.

Another example I have (and which is perhaps a deeper question about how you approach mathematical problems) is, “How do you compute a to the power of b?”. I would, instinctively, write a recursive method. Others I know would do it iteratively. What is your coding style?

h1

My first game of the year

January 24, 2010

What a horrible way to start off the new year – a bad loss, personally, and for our team (we lost 4.5-0.5). My opponent and I shared the same rating, and its the first time in many months that I lost to someone not higher rated than me! Anyway, you can replay the game here (I play black).

1. d4 d5 2. c4 c6 3. Nf3 Nf6 4. cxd5 cxd5 5. g3 g6 6. Bg2 Bg7 7. O-O O-O 8. Nc3 Nc6 9. a3
How much more boring can an opening be? I take efforts to spice up the game a little!
9…Ne4 10. e3 Nxc3 11. bxc3 Qc7 12. Nd2 Rd8 13. e4 dxe4 14. Bxe4
I had looked at 14. Nxe4 Nxd4 15. cxd4 Bxd4 as leading to an interesting position where I get the queen and 3 pawns for a rook, knight and bishop, or win the a1 rook. Thankfully for me, my opponent chose not go into this line because if we had, he would most likely have found 16. Bd2! Bxa1 17.Qxa1.
14… f5
The beginning of my troubles. I should have just stuck to staking my claim in the centre with e5. Both of these lines leave me with a slight plus. 14… e5 15. d5 Na5 or 14… Bh3 15. Re1 e5 16. Nf3 exd4 17.Bf4 Qd7 18. cxd4
15. Qb3+ Kh8 16. Bg2 Na5
(16… e5)
17. Qb4 Be6 18. Nf3 Rac8
(18… Nc6 19. Qb5 a6 20. Qe2 Bd5 21. Bf4 Qa5)
19. Bf4
(19. Re1 Nc6 20. Qb5 Bd7 21. Bf4)
19… Qxc3
During the game, I thought that this was the blunder that caused all my trouble later in the game. But the computer suggests this was possibly the best move I had at this point in time and actually points to my 22nd move as the real blunder. All in all, I think I should have never allowed myself to get into this position!
20. Qxc3 Rxc3 21. Bd2 Rxf3 22. Bxa5 Rxd4?
(22… b6 23. Bxf3 bxa5 24. Rfe1 Bxd4 25. Rac1 Bg8 26. Rxe7 Bb2 27. Rb1 Bxa3 28. Rxa7 Bb4) (22…Rd5 23. Bb4 Rd3 24. Rae1 Bg8 25. Bxd5 Bxd5 26. Bxe7 Kg8 27. Rd1 Rb3)
23. Bxf3 Ra4 24. Bd8
(24. Rae1 Bc4 25. Bb4 Bxf1 26. Rxe7 Bf6 27. Re8+ Kg7 28. Kxf1)
The rest of the game was probably needless – I could’ve resigned right here. But with my opponent in time-trouble until the very end, I decided to drag it out – not one of my best moments!
24… Bxa1 25. Rxa1 Kg7 26. Bxe7 Rc4 27. Bg5 b6 28. Re1 Kf7 29. Rc1 Rd4 30. Rc7+ Kf8 31. Rxa7 Rd3 32. Be3 Rb3 33. Ra8+ Ke7 34. Ra7+ Kd6 35. Bf4+ Kc5 36. Rc7+ Kd4 37. Be3+ Ke5 38. a4 g5 39. Bd1 Rb4 40. Bxg5 Kd6 41. Rc1 Bd5 42. Bf4+ Ke6 43. Be3 Kd6 44. h4 Bc6 45. Ra1 Be4 46. Bh6 Rb2 47. Kf1 Kd5 48. Ke1 Bd3 49. Bg7 Rb4 50. Ra2 Re4+ 51. Kd2 Bc4 52. Rb2 Re6 53. Bf3+ Kc5 54. Kc3 b5 55. axb5 Bd5 56. Bxd5 Kxd5 57. b6 Re7 58. b7 Rxb7 59. Rxb7 Ke4 60. Bd4 h5 61. Rh7 f4 62. Rxh5 1-0
h1

Calling on Windows gurus for a little help

January 1, 2010

My father-in-law seems to have run into an issue with IE8 on his XP machine:

When it starts up, it gives him the message: memory could not be “written” and crashes. He was having trouble with ie7 crashes and so upgraded to ie8 a couple of days ago. I did a screen share and tried out various tips and tricks to sort him out, all to no avail:

  1. Full virus scan with up-to-date virus definitions, all clean.
  2. Malware scan, all clean.
  3. Tried disabling IE add-ons, no effect.
  4. Cleared out internet cache, browsing history, etc, no effect.
  5. Saw something about the DEP tool, went into Advanced Settings and unchecked the box about helping with mitigating memory issues, no effect.

Anybody with any ideas so he doesn’t think I know nothing about computers (which may be true but he doesn’t need to know that)? :)

h1

My last slow game of the year, the London Chess Classic and 18 blitz games

December 19, 2009

Even though its still quite busy at work, I am in the holiday mood when it comes to the blog, so I will keep it short. Here is my last game:

1. e4 c5 2. Nf3 d6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 a6 6. a4 Nc6 7. f4
(7. Be2)

7… Qb6 8. Nf3 e6 9. Bd3 Be7 10. Qe2 Bd7 11. Rb1 Nb4
(11… O-O 12. e5 dxe5 13. fxe5 Nd5 14. Ne4 Nd4 15. Nxd4 Qxd4)

12. Be3 Nxd3+ 13. cxd3 Qa5 14. Nd2 Bc6 15. b4 Qc7 16. O-O b5 17. a5 O-O 18. d4?
(18. Rfc1) (18. Nf3)

18… Qb7! 19. e5 Nd5 20. Nxd5
(20. Nce4 dxe5)

20… Bxd5 21. Qg4 f5 22. exf6 Rxf6 23. Rf2 Rg6 24. Qh3 Rc8 25. Nf3 Rc3?
(25… Be4! 26. Re1 (26. Ng5 Bxg5 27. Rc1 Rh6 28. Rxc8+ Qxc8 29. Qg4 Bh4 30. Re2 Qc4) 26… Bf5 27. Qh5 Bg4 28. Qxg6 hxg6)

26. Bd2??
(26. Re1 Qc8 27. Bd2 Rh6 28. Qg4 Rc2 29. Qg3)

26… Rxf3 27. Rxf3 Bxf3 28. g3 Qe4 29. Re1 Qxd4+ 0-1

The London Chess Classic took place in London from the 8th-15th of December, with Carlsen winning with an unbeaten +3 performance in the 7 round event. I attended on 2 days, including on the 1st round when Carlsen scored a brilliant win over Kramnik! It was awesome to see a few hundred spectators come in every day; the GM commentary was also quite good. I even managed to get Carlsen’s and Nakamura’s autographs. I played in the evening foyer blitz tournament both days I attended, scored 3 out of 6 the first time and a much more impressive 4 out of 6 the second time around, where 5 out of my 6 games were against people rated much higher than me!

I also played in my club’s Christmas blitz tournament and scored an average 3 out of 6 – I beat three players rated below me and lost to three above me – nothing exciting there at all! No more chess (except the on-line variety) until the New Year when my first game is on the 7th.

Happy Holidays!

h1

A frustrating draw

December 1, 2009

I had played against the same opponent twice before (once as white, once as black) and beaten him twice, so I went in with hopes of a third win – which would have left me in sole possession of first place in the U140 Swiss with a round to go. Instead, we played out a draw and my opponent has held on to his half-point lead, with 3 of us on his heels. Replay game here.

1. e4 e5!
Certainly a good way to catch me by surprise. I was expecting a pitched battle in an open Sicilian!

2. Nf3 d6 3. d4 Nc6 4. dxe5??
Its not that this is necessarily a bad move – it is just that after this move, the position became very static and it was really hard to break through with an attack!

4… Nxe5 5. Nxe5 dxe5 6. Qxd8+ Kxd8 7. Bc4 f6 8. Be3 Bg4 9. Nd2 Bb4 10. f3 Bh5 11. O-O-O Ke8 12. Nb3 b6 13. g4?
Not the type of game I wanted to get into. I looked at 13. Bb5+! Kf8 14. a3 Bd6 and didn’t see what else I could achieve. But the computer shows 15. c4! Be8 16. Bxe8 Kxe8 17. c5 bxc5 18. Nxc5 and the position is better for white. I didn’t even consider the idea of c2-c4-c5, assuming that the opening of lines on the queenside would favor black. Instead, with the better developed pieces, white is doing fine here.

13… Bg6 14. h4
Again missing an opportunity to hold on to an advantage with 14. Bb5+ Kf8 15. c4 Be8 16. Ba6.

14… Ne7 15. c3?
Here again I looked at Bb5+ and thought it even worse because after 15. Bb5+ c6 16. Bc4, all I seem to have done is lose a tempo! Instead, it actually works in my favor because Black can no longer retreat his bishop to d6, blocking the d-file!. 15. Bb5+ Kf8 16. Kb1 Be8 17. Ba6 is also fine for white.

15… Bd6 16. Kb1
See a pattern? Bb5+ is required in all lines where I hope to maintain an advantage! 16. Bb5+ Kf8 17. Kc2 Be8 18. Be2. I thought about 16. Kc2 as well, where the king is one tempo closer to the center of the board but wasn’t sure if it might get into trouble if lines opened up for black’s rooks. Turned out to be another miscalculation on my part. Nothing worthwhile happened in the rest of the game. I tried to push on in the endgame but my opponent defended easily to reach a drawn ending.

16… Rd8 17. Nc1 Bf7 18. Bxf7+ Kxf7 19. Ne2 Rd7 20. Ng3 Rhd8 21. g5 Nc8 22. gxf6 gxf6 23. Nf5 Bf8 24. Rxd7+ Rxd7 25. Rg1 Ne7 26. Nxe7 Kxe7 27. Kc2 Ke6 28. a4 Rg7 29. Rxg7 Bxg7 30. h5 f5 31. a5 f4 32. Bf2 Bf6 33. axb6 axb6 34. Kb3 Bd8 35. Kc4 c6 36. b4 Kd6 37. b5 c5 1/2-1/2

h1

U140 KO Champion

November 22, 2009

Even though the other tournaments in this year’s internal competitions are 2 rounds away from completion, the U140 KO tournament was decided this past week! I had played my opponent once before a year ago, so I could prepare for his opening choice if I won the toss and played white – which I did.

1. e4 c5 2. Nf3 Nc6 3. d4 cxd4 4. Nxd4 g6 5. c4 Bg7 6. Nb3 Nf6 7. Nc3 O-O 8. Be2 d6 9. O-O b6 10. Be3 Bb7 11. f4 Rc8 12. Rc1 Nd7 13. Nd4 Nc5 14. Bf3 Nxd4 15. Bxd4 Ne6 16. Bxg7 Kxg7 17. f5
We had both played the opening reasonably well. Along with the text move here, I considered two main alternatives. 17. Nd5 Bxd5 18. exd5 Nc7 (18… Nxf4? 19. Qd4+, winning) seemed very promising for white. But Bxd5 wasn’t forced and I couldn’t see how I could push on with my attack. 17. Be2 didn’t offer me much hope either in opening up lines to his king.

17… Ng5 18. Qd4+ f6 19. Nd5
(19. Bg4 Qd7 20. Rcd1)

19… Nxf3+
19… e6 20. fxe6 Nxe6 was an option I thought he might go for, as the position is completely drawn.

20. Rxf3 Qd7 21. Nf4
If I could get my knight to e6, I would be winning. Or so I thought. Instead the position was now dead equal, and was going to remain so for the next several moves! 21. b4 Bxd5 22. exd5 gxf5 23. Qd3 or 21. Re1 were two ways to keep a (very slight) edge in the position.

21… gxf5 22. Nh5+ Kh8 23. Rxf5 Qe6 24. Ng3 Rc5 25. Rxc5 dxc5 26. Qd3 Rg8 27. Rd1 Bc6 28. b3 a6 29. Qe3 Qg4 30. Rf1 Rd8 31. e5 Qg5 32. Qxg5 fxg5 33. e6 Rd2?!
33… h5 34. Nxh5 Rd2 35. Rf2 Rd1+ 36. Rf1 Rd2 is a draw by perpetual. Now I could play for a win!

34. Rf8+ Kg7 35. Rf7+ Kg6 36. Rxe7?
This throws away the afore-mentioned win. 36. Nf5 Kh5 37. Nxe7 Bxg2 38. Ng8 Bc6 39. e7 Be8 40. Nf6+ Kg6 41. Rf8 Re2 42. Nxe8 Rxe7 43. Rf6+ Kh5 44. Re6 Rxe6 45. Ng7+ Kg4 46. Nxe6 is a line the computer spits out. I thought about 36. Nf5 but had trouble seeing the right follow-up. Instead, the text move could even be losing. But it required my opponent to see a tricky defense (and neither one of us saw it)!

36… Bxg2?
And I fully capitalize this time and bring in the full point! I was more worried about 36…Rxg2 37. Kf1 Rxh2. But this isn’t sufficient either. The really tricky move to find is 37… Rd2! 38. Rc7 Rd6 39. e7 Kf7 40. Nf5 Rf6 41. Kg1 and it is black that’s playing for a win!

37. Rd7! Rxa2 38. e7 Kf7 39. Nf5 Re2 40. Nd6+ Kg6 41. e8=Q+ Rxe8 42. Nxe8 Bh3 43. Rb7 Bf5 44. Rxb6+ Kh5 45. Rxa6 Bc2 46. Rb6 Kh4 47. Kf2 h5 48. Nf6 g4 49. b4 cxb4 50. Rxb4 Kh3 51. Nxh5 Kxh2 52. c5 Bg6 53. Ng3 Be8 54. Rxg4 1-0

In the endgame, I missed a couple of easier mating lines but the result was never really in doubt. Great to notch a tournament victory!

h1

My last game before I head off for vacation

October 19, 2009

Played last week, at the start of the new league season. Previous games against the same opponent are here, here, and here. Last week’s game is here (I am black).

1. e4 c5 2. Nc3 e6 3. f4 d5 4. e5 Nc6 5. Nf3 Nh6 6. Be2 Qb6 7. a3 Bd7 8. d3 Be7 9. Na4??
Completely misses my next move, loses a couple of pawns and gives me a monster passed a-pawn.

9… Qa5+ 10. Nc3 d4 11. b4 cxb4
11… Nxb4 12. Bd2 Nxd3+ 13. Bxd3 dxc3 14. Be3 was possibly even stronger!

12. Ne4 bxa3+ 13. Bd2 Qc7 14. O-O b5 15. c3 Qb6?!
Nothing wrong with my move, though the idea is flawed – my idea leads me to lose a piece though I still have good chances due to the strong, advanced connected passers. (15… dxc3 16. Nxc3 b4)

16. Kh1 Nf5?
(16… dxc3 17. Nxc3 b4)

17. cxd4 Nfxd4?
(17… O-O 18. g4 Nfxd4 19. Be3 Rad8 20. Rc1 b4 21. Rc4 Be8)

18. Be3 b4
(18… O-O)

19. Rc1 Rd8
(19… b3)

20. Rc4 O-O 21. Nxd4 Nxd4 22. Bxd4 Qa5 23. Bc5
(23. Nd6 Bxd6 24. exd6 Bb5 25. Rc1 Rxd6 26. Bc5 Rc6)

23… Bxc5 24. Rxc5
(24. Nxc5 Bb5)

24… Qb6 25. Qb3
25. d4! is very strong instead, activating the bishop. My pawns are not yet ready to go marching down.

25… Bc6 26. Rc4?
(26. Rfc1! Bd5)

26… Bd5 27. Qxb4
(27. Nd6 Qe3 28. Bf3 Bxc4 29. Qxc4 Rb8)

27… Bxc4 28. Qxc4
Now, I am definitely better and the a-pawn cannot be stopped. 28. Qxb6 axb6 29. dxc4 Rd4 was an option.

28… Qb2 29. Re1
(29. Bh5 a2)

29… Rc8 30. Qa4
( 30. Nc5 a2)

30… Qxe2
I fully expected my opponent to resign here. I was shocked when he played on for a few more moves!

31. Ra1 Rc2
(31… Qb2 32. Qd1 Rc2 33. Qf1 a2)

32. Rg1 Qxd3 33. Nd6 a2 34. Qxa7 Qb3 35. h3 Rb2 36. Ne4 Rb1 37. Nd2 Rxg1+ 38. Kxg1 Qd1+ 0-1

h1

Testing log statements

September 25, 2009

Of late, this blog has become an almost exclusive chess blog. Let me redress that balance, at least for one post.

Unit testing your log statements is unusual, but not unheard of. There might be many reasons for this – you might have a separate process monitoring your log files and taking action on certain log messages…or, you might be handling an event which you choose to ignore except to log its invocation and you want to test that you are logging properly. Whatever the reason, there isn’t any easy way (at least in Java) to test your log statements.

One way of testing is to read from the log file and ensure the log statement gets written out. But we don’t ideally want to be reading from log files in our unit tests. Here’s what you could do, if you were using log4j:

First, have a class called VectorAppender.java:

import java.util.Vector;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class VectorAppender extends AppenderSkeleton {
 private static Vector <String>messages = new Vector<String>();
 public static final boolean VECTORAPPENDER_SYSOUT = false;

 @Override
 protected void append(LoggingEvent arg0) {
  if (VectorAppender.VECTORAPPENDER_SYSOUT)
   System.out.println(arg0.getLoggerName() + "  - " + arg0.getRenderedMessage());
  messages.add(arg0.getLoggerName() + "  - " + arg0.getRenderedMessage());
 }

 public void close() {}

 public boolean requiresLayout() {
  return false;
 }

 public static void clear() {
  messages = new Vector<String>();
 }

 public static Vector<String> getMessages() {
  return messages;
 }
}

Then, we invoke initLogging() in the setup to our unit test:

public static void initLogging() {
 Properties p = new Properties();
 p.put("log4j.rootCategory", "DEBUG, stdout, LOGFILE");
 p.put("log4j.appender.LOGFILE", VectorAppender.class.getName());
 p.put("log4j.appender.stdout.Threshold", "DEBUG");
 p.put("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
 p.put("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
 p.put("log4j.appender.stdout.layout.ConversionPattern", "%d %-5p [%C{1}.%M() %L] - %m%n");
 org.apache.log4j.PropertyConfigurator.configure(p);

 VectorAppender.clear();
}

Now, VectorAppender.getMessages() gives you everything that’s been logged and you can assert to your heart’s content!