Sunday, February 16, 2014

Week 6: Finishing A1

I've finally managed to finish A1 over the course of the last week.  Everything seems to be working smoothly and there don't appear to be any bugs (fingers crossed!), so I'm happy with the end result of my program.  It's pretty amazing how three weeks ago I had very little idea of where to even begin--this huge pile of code was just basically sitting there, waiting to be sorted out.  Even now, I still don't quite understand the details of GUIController (what exactly does lambda do, and how does Python create a graphical version of the game?), which I hope to solve by learning about graphic interfaces and more of Python's built-in functions and keywords.

Perhaps the greatest challenge I encountered during the course of A1 was writing the Tour.py module.  We had previously been shown a function for solving (impressively, in three lines) the Tower of Hanoi problem, and I initially planned to base my tour_of_four_stools method on that solution with minor adjustments.  My first draft was almost identical to the original solution (i.e. three recursive calls to tour_of_four_stools with a base case of move(source, dest), the only difference being the addition of an extra stool in the parameter and setting a value i as equal to the ceiling of n/2), but I found out to my surprise that it would only work properly with four or fewer cheeses.  I went back to the assignment sheet and realized that if I replaced the second line of my method with that of the code for three stools, the entire method would work.  I ended up with two separate methods (one for three stools and one for four stools, which called the former method) just to come up with a solution for Tour, but it worked perfectly and returned the optimal number of moves.  As it turns out, my first implementation of tour_of_four_stools would eventually force a bigger cheese to stack onto a smaller cheese, causing the program to raise an IllegalMoveError.

Looking at others' Piazza posts and SLOGs, I got the sense that I wasn't alone in finding Tour difficult to code, so that's reassuring.  As a side note, I did have trouble with my time.sleep() function which very few people seemed to have.  I tried importing the sys module and using write(), but I just could not get time.sleep() to work properly on my computer.

Besides A1, I was introduced to binary trees in class this week.  The premise seems simple enough: a tree is comprised of a parent node with(out) several other child nodes that may or may not have their own child nodes.  This concept is very closely related to recursion, as seen in its preorder, postorder, and inorder traversal methods.  Its structure made me wonder if trees could possibly play a role in creating lighting effects in video games or CGI, as each node could point to a different shade or color.  Either way, I hope to read more about the use and purpose of trees in computer science.

No comments:

Post a Comment