Thursday, October 28, 2004

In a groove...then not!

Well I finished the check tests for all pieces and managed to remove quite a bit of redundancy last night. The code had 32 tests and they all passed. The only missing piece was checkmate. I was feeling good. My productivity was great and then BAM! When I wrote checkmate I realized that I desperately needed subclasses for each piece (I know it is hard to believe that up until this point all code for the chess rules was contained in ChessBoard! But I was trying to do the simplest design that worked.) The signs where everywhere...switches, methods with the names of the pieces in them. How blind I had been.

This wasn't going to be easy. I first yanked the type field out of ChessPiece and then created a subclass for each piece. Nothing compiled! I decided that I would start by just getting one test to pass and then add the code for each test. Incremental refactoring! So I commented out all code that didn't involve setting up the board. My first goal was getting Pawn working then so on and so forth. I didn't get done, but 17 out of 32 tests are passing and all piece move logic is located in the respective class. The quality and amount of my tests gave me confidence to embark on a massive change without much fear. This confidence was fed as I was fairly easily able to get tests to pass with just a little work at a time. I foresee challenges coming with certain rules, but given the track record of my past experiences with TDD I am not afraid at all.

I'm back down to 491 non-commented source lines and 201 of them are test. I have commented out tests 18-32! I also have commented out all code for castling, enpassant and check.

No comments: