Friday, 6 March 2015

Week 7: Assignments and Binary trees

For assignment 2, we have to write code for a game called tippy. Tippy uses a similar game view as tic-tac-toe. To win the game, you must form a z shape using x's or o's in any form of rotation. We create a strategy called minimax that chooses the best possible moves to win the game. Minimax is a recursive code which goes lists all the best moves from all the moves available. It assumes the opponent to also use the best possible moves, so minimax must choose the moves that will put the opponent in a losing position.We use recursion so minimax can look at all the game states not matter what game it is and choose the game state that will give the optimal score. In our case, the score will be 1 (for a win), 0 (for a tie), -1 (for a loss).

Lately, in class, we have been learning about trees. What are trees? A tree has set of nodes with directed edges between some pairs of nodes. One node is called a root. In trees, there are children and every child has exactly one parent. A leaf is a node with no children. An internal node is a node with one or more children. Many functions applied to the tree uses recursion to access the children.
















general form of recursion:
if condition to detect a base case:
     do something without recursion
else:  # general case
     do something that involves recursive call(s)
Usually, the base case occurs when there is a leaf and the general case will keep going through the tree until it hits the leaf. Recursion is extremely helpful in trees.

No comments:

Post a Comment