Tuesday - I'd been having trouble figuring out how to implement my main game loop "run". I couldn't think of how to test it. I thought about it for a while but rather than end up wasting too much time I asked Brian. He explained that I shouldn't be testing the implementation of the code. Instead I should be testing the behavior. With that in mind, I wrote tests for and implemented a game_over? function. My test for the main game loop then became "is game_over? true when run is finished?".
Wednesday - At this point I could check that the game was over but not who won. To fix this, I extracted the winner checking code and put in a function called winner. This simplified things a little. My next task was to implement a simple AI player. This required changing my Player class to implement a method called get_move. This allowed me to have the game ask the current player for their move regardless of whether it is a human or the computer.
Thursday - I finished the AI player and was told to make it smarter. There was a specific way I was supposed to make it smarter. I needed to implement the Minimax algorithm. To do that, I first needed to understand it. This required most of my time on Thursday. Looking back, I honestly didn't get it. I couldn't understand how to test it and still hadn't figured out how it worked on a low level by the end of the day. Now it seems so simple. Essentially it checks every possible move combination on the current board and scores it. The goal is to find the move or moves that give the AI the best move that is the worst for the opponent. In Tic-Tac-Toe this translates into if two AI players use this and play against each other then they will always draw.
Friday - I was still having trouble with the Minimax. My mentor recommended looking at the Negamax implementation of Minimax because it is simpler and only involves one function call. I asked a few other apprentices how they had tested it and didn't really make any real progress. This was frustrating because Wikipedia outlines the algorithm in easy to implement pseudo-code but has not advice for implementing it with TDD.
Weekend - I finally started to make some progress. I wrote some sample boards and checked the score that came back from Negamax. It took a while to get this right because I was still having some trouble with understanding how to implement it. I'd given up trying to test it and implement it piece by piece though. Without help, I just couldn't do it. After some web searching though, I came across the vital clue to what I was doing wrong. I needed to take the negative of the result I got out of Negamax. After that, I could no longer beat the AI on my command line interface. This was good, but I still needed tests. In the end, I tested that the function returned the correct move/moves in a number of specific situations. Hopefully that's what I was supposed to do and that I won't get in trouble for doing my TDD a bit backwards this time.
Anyway, two things to take away from this post for me.
- Write these on time so that I don't have so much work to do.
- I keep trying to enter Vim shortcuts into this web interface, so they must be sinking in.
No comments:
Post a Comment