Allowing Your Code to Tell You Exactly What it's Doing
19 Jul 2012
I am working on my second round of writing an unbeatable Tic Tac Toe (TTT) in Ruby. Even if you are an amazing TTT player, you will never win in my first version. I promise.(If you use my local copy and not the Heroku version. See below for why. Quite amusing.) My first version was a rails app. The second version will be a command line app, turned into a gem, and then converted to a rails app.(Totally pumped about making a gem. :) )
Although I am already looking at my first version and thinking, "Wow. This needs a MAJOR refactoring session.", I am also thinking, "Wow. I am still really proud that I made this work!" When given the initial task of writing it, I was feeling severely overwhelmed. I had never had done that big of a programming task before. After the initial paralyzation wore off, I just started working on it one baby step at a time and thinking about what I could do instead of what I couldn't. I never looked at anyone else's TTT code.( I did look at minimax pseudo code and how to beat someone in real TTT .) I wanted this to reflect where I was at in my stage of programming and to show what my mind would naturally create.
When I saw it actually work for the first time I have to admit I was so used to seeing it being broken that I did not think I was done. After it hit me that I had finished I got a huge rush. I realized that I could make things out of nothing!
For my first version, I did not use tests. I had never done testing outside of a tutorial and thought that the challenge of creating an unbeatable TTT would be enough for me at the time. In not using testing I have learned the value of testing. There were times I felt like I was going insane trying to find where my code was breaking. I would place @message variables all over the place to output messages to the screen to tell me what my code was actually returning vs. what I thought it was returning.
Of, course I wanted to share my genius with my family and friends, so I pushed my app up to Heroku. As people started to play it, they also started to tell me that the game was having some wild behavior. Experience it for yourself here. Play a few times to get the full "experience". You should get a good laugh. I never thought about more than one person playing at a time and when a bunch of people started playing at once, they were effecting each others games. Every new game would create a row in the database but all the logic is based off of the first row in the database so the results get pretty wacky. Total n00b move.
Anyways, for the second round of TTT I am going to use minimax. I was trying to use minimax the first time but there seemed to be something I was missing. I had a feeling that my code was close but not quite there. I have confirmed the feeling that I was close with my mentor and decided to revisit my old code. As I was reading my code I was becoming annoyed that I had to keep scrolling up and down and reading methods to see what was going on. I decided to paste each method inside where it was called so that I could see what was going on. Something very interesting happened.
In taking my code and making it completely procedural, I was able to see some things that I could not before and I did not expect to see what I did. I could see where I was using two methods where I could use one. I could see where I could refactor some naming to be more clear in the context of what was happening as a whole. It was really enlightening. If you are stuck on something, or just for kicks at some point, you should give it a try. You too may be surprised at what you find.
It didn't take me as far as giving me a lightning flash of how to solve minimax, but hey, I'll get there. One test at a time. :)