Stories about Software


Chess TDD 49: Castling is Hard

In this episode, I start to discover that castling is hard.  There’s unusual movement, the fact that 2 pieces move simultaneously, and the fact that you have to keep track of a lot of status.  People have even commented on this being a particularly hard facet of the game.  Oh well, c’est la vie.  I think I came up with an idea for a helpful strategy during the course of this episode, even though this episode itself wasn’t wholly productive.

One thing to note if you’ve been following this series is that I’ve switched to Visual Studio 2015.  I brough CodeRush, NDepend, and NCrunch along for the ride, not to mention some of my preferred VS plugins.

What I accomplish in this clip:

  • Fixed castling implementation to the long side of the board.
  • Got a little more organized in Trello.
  • Started on implementation of not allowing castling when pieces have moved.

Here are some lessons to take away:

  • When you need to correct a mistake or bug, make sure you start with a red test that exposes the mistake.
  • It’s okay to have a situation where making a test you’ve altered green makes others red.  If those tests are now wrong or out of date, this gives you basically a checklist of the tests you need to fix.
  • If you consider yourself advanced enough to skip a step, it’s still always possible that you’re making a mistake.  TDD is all about micro-hypotheses and verification of your understanding of the code.  If you find yourself being wrong, slow down and get back to basics.
  • No matter how long you’ve been at this, you’ll still make mistakes, especially if you get a little fast and loose.
  • If you write a test to confirm your understanding of behavior in a certain context, I recommend leaving it in.  If you were wondering about that behavior, chances are someone else will, later, as well.

Newest Most Voted
Inline Feedbacks
View all comments
Oded Coster
Oded Coster
8 years ago

One additional (possibly obvious) to castling, is that it isn’t allowed if there are any pieces (same side or other side) between the king and rook.

Only reason I mention it is that you still have a few king moves acceptance (and unit) tests that seem to allow such moves.

Erik Dietrich
Erik Dietrich
8 years ago
Reply to  Oded Coster

I’ll put it in the backlog to check for that at the board level. Thanks. 🙂

Colin Higgins
Colin Higgins
8 years ago

It’s just a huge blob of checking state sequentially in my case.

Erik Dietrich
Erik Dietrich
8 years ago
Reply to  Colin Higgins

Yeah, it seems like in this domain, that’s just what some things come down to.