Struggling for Competence

Code Kata - Atari

This Code Kata was set by Keith Braithwaite, at his session TDD As If You Meant It, during Software Craftsmanship 2009.

The challenge is taken from the game of Go. Go is played by two players who alternately place black and white stones on the vacant intersections of a grid of 19×19 lines. A stone is atari if a stone or chain of stones has only one liberty. The liberty is the number of vacant points that are adjacent to a stone or connected group of same coloured stones (diagonals do not count). Some example configurations, with their liberty, are shown in the following pictures:

single stone with a liberty of 4 single stone with a liberty of 2 two adjacent stones with a liberty of 4 two adjacent stones with a liberty of 3

The Kata is to calculate whether a stone is atari, for an arbitrary collection of stones.

Keith had some rules about how you should proceed with coding, which define an extreme limit of TDD:

  1. Write exactly ONE failing test.
  2. Make the test from (1) pass by first writing implementation code IN THE TEST.
  3. Create a new implementation method by:
    • doing extract method on implementation code created as per (2), or
    • moving implementation code as per (2) into an existing implementation method.
  4. Only ever create new methods IN THE TEST CLASS.
  5. Only ever create implementation classes to provide a destination for extracting a method created as per (4).
  6. Populate implementation classes by doing move method from a test class into them.
  7. Refactor as required.
  8. Go to (1).

Some reactions from other attendees of the session are here and here.