This week in lectures we covered binary
trees, which is a great compliment to my CSC240 class as we were just proving
properties of binary trees in class. But despite how “interesting” binary trees
are, I really want to talk about assignment 1; it was a great obstacle and the
feeling of conquering it felt like no other. I’ll continue where I left off
last week.
For starters, I got the GUI to
run, which feels like a mini accomplishment itself as it really took a while,
included relentless numbers of trials and errors in the implementation of
various functions. It turned out that I did not write some functions, or if I did,
some parameters were off. I was able to finish TOAHModel fairly easily after
figuring out these obvious and simple problems, with ConsoleController right after.
The challenge was really to figure out the difference in the move function
versus the TOAHModel, it had two major changes, one is that instead of raising
errors, I would just print the error by warning user and prompt them to enter
again, to avoid interruption of playing the game; secondly, I feel like to optimize
a player’s experience in manually playing the game, a visual representation
would be very helpful to the operator, so I added animation by printing the
model and a text stating which cheese has been moved, from and to which stool. Other
than that, the user interaction part was not too challenging, and it felt
enjoyable to design a program you’ll own way, with your own problem handling
and style, definitely a highlight in this assignment.
The part I spend most time on, as
with most other people, is the Tour.py. I first started with the recursive
function M(n) as it is from the handout, but it takes two variables n and i, which
made it a little bit problematic. When calling M(n-i) inside the recursive
function M(n), I had to call M(n-i, variable), I first tried i as the variable,
but it made it a infinite recursive function; adding one more variable would
create another parameter. In order to solve the problem, I decided to take a
different approach, and solve it one step of the time, as in move n-i cheeses
to an intermediate stool, i cheeses to the destination stool using the three
stools way, and n-i cheeses from intermediate to the destination. My attempt to
solve the problem included Danny’s move_cheeses function and a whole new class
for calculating minimum moves for four-stool tower. It included the number of
moves for a three-stool tower, with the four-stool tower calling itself to
calculate the minimum moves required from previous four-stool movements as well
as the three-stool function for the movement of i cheeses. It allowed me to
break the movement into parts that I could manage better. My reason to use a
class and not functions is so that I can store a variable in an object without resetting
it through recursive calls, and to avoid running into any global declaration
issues (not to mention global variables are really bad in computer science),
making it possible to access it with a simple self.num_of_moves call to bring
up the minimum number of moves for that specific number of cheeses value. I proceeded
by creating a split function that compares the M(n) formula to the minimum
number of moves, whichever one matches will be the split, returning the value
of i. This means that the split found will be the lowest i value since it was
in a for loop, and the i value might not necessarily be unique, this implies
that the “optimal” move might not be the one and only “optimal” move for that
specific number of cheeses.
Finally, I have noticed that with
the increasing in number of cheeses, the processing time increasing
exponentially, and once it reaches about 18-20 cheeses, it really start to
stack up. Due to the number of recursive calls, calculating one move might take
much longer than another, so in the animating part, in order to avoid lag
between moves, I first record the “optimal” move sequence by running the
function, which could take minutes for bigger number of cheeses; then I made a
new model to read the move sequence of the first model and animating it step by
step using a for loop. Once it prints, it will be a constant time delay based
on delay_btw_moves entered, using the sleep function, with no processing
trouble.
On a side note, it felt great
writing this “progress” report, it helps me in reviewing my work and reflects
on what I struggled on as well as what I enjoyed the most. I will definitely do
the same for assignment 2, to further improve my experience in this course. With
that said, the reading break is coming up, I will be taking a long awaited rest;
I will post again in two weeks to discuss recursion, a topic I have been dying
to talk about.