In week 9, I learned more techniques on choosing test cases and tracing function calls. In lectures, Prof. Danny showed us one possible solution to minimax. When I was working on assignment 2, writing functions to make minimax work was the most challenging task. I thought that every game state is a node, but didn't know how to return a move that leads a winning state. In lecture, I learned that tuple is a choice of good data structure that can be used to link a move with a state. An initial state have a list of possible moves, and each of the moves can lead to another game state, and so on. Because of the nature of the data, recursion is the suitable strategy to return what initial move can lead to a final winning state (if there is one). I learned that recursion can be applied to each of the moves, until the very end, where a player wins. That final game state would generate a score of +1 for the player who won. Once the leave nodes are calculated with a score, recursion can trace back the path that led there, thereby suggesting an initial move.
On top of learning the algorithm on solving minimax, I also learned in class of how to trace function calls. For example, the following program, when run with python, would generate a value of 12. However, the process of reaching there involves many steps. I learned that python prioritizes an input value over another variable value, even though they may have the same variable name. When h(1) is called, the function h(N) is called, with N=1, but not 5, even though that the variable N is given the value 5 in the beginning. Furthermore, because h(N) involves a nested call for both functions g and f, each time the input N is changed according to the value returned from the nested call.
Tracing calls like this seems really easy and basic, but it is a very good practice which helps programmers to follow through the code and see what the code does at each step This can be very beneficial if in the future that we ever have to write complicated programs that involve many variables.
No comments:
Post a Comment