16th October (Mon)

After a mostly relaxing weekend seeing family and friends in Cambridge on Saturday and then chilling out on Sunday, I’m back to Ruby!

Today I made quite a few improvements to my Ruby Tic Tac Toe, including making the board print more nicely (I learnt how to do multiline string interpolation in Ruby), having the Ui class clear the screen when a new game starts (this actually made it look a lot more professional).

I also debugged my minimax in the morning (still had one failing test), it turned out that the algorithm was actually working fine already, the problem was that without realising it, in my example case I had inadvertently set up a situation where the next move created a fork, this meant that although there was a winning move if playing in 3, there was also a guaranteed (albeit not immediate) win from playing in 2. The algorithm picked the non-immediate win simply because the index 2 is lower than 3. I actually was already aware that this could happen as I’m not weighting my algorithm in accordance with the recursion depth at all, it took me manually stepping through quite a few steps to notice that I’d accidentally set it up with a fork!

In the afternoon I was working on adding input validation when players choose their moves. I implemented a MoveRequester class to try to keep the validation out of the Ui completely. I think it went pretty well, and I feel like I’m getting the hang of TDD more and more! Ruby’s rspec also makes it super easy to stub and spy on methods of other class instances and I made use of this for TDDing my new class.

I then also managed to squeeze in extracting a Game class from my App class. I think I’ve done it a lot better this time in Ruby than I did it in Java — I was trying to be really careful to TDD/add test coverage for the Game class while extracting it and removing the unnecessary tests from the App class.