Hey, this week, I want to talk about Assignment 2, which was to design a strategy and a new game state. This assignment is more difficult than Assignment 1.
First, let's talk about Minimax. In assignment 1, we were told to design a random choosing strategy, so we only need to import random and choose a possible move from the list. But Minimax is a strategy such that computer will try to win and if there is a chance to win, it will definitely win. So it is a recursive strategy and I don't think there's other way to implement besides using recursive structure. After figuring out its structure, here comes the most difficult part, writing code. I read the handout very carefully and find the picture interesting. It is basically saying that we need to consider all possible moves and all states can be derived from that move, so it is quite obvious that some of them will end with A win or B win. If A wins, then, the it return 1 to the last state will made the move. So when we reach the node, we will always try to make a decision of which one to choose, given that all of them end up with a winning condition. I was thinking if we need to compute how complex each subtree is and choose the simplest tree, the code becomes very long and tedious. Besides, it may take computer very long time to reach leaves and again compute complexity of every subtree at the very beginning. So I decided to choose first possible move that maybe end up with a winning condition.
Second, that's about TippyGameState. Tippy is a very interesting game, very different from Tic-Tac-Toe. The winning condition is somehow weird but in this way, random strategy and Minimax strategy are more useful. If what we implement is Tic-Tac-Toe, it is pretty likely game end with a tie. The winning condition of Tippy reduces this possibility and makes it more challenge. However, implementation of detection of winning block is not that simple. I worked out an algorithm that using a for loop and for very coordinate that has an 'X' or 'O', I look at its surrounding for specific shape. If there is , return True. Else return False. It is like:
for coord:
if <some condition>:
return True
elif <some condition>:
return True
elif <seom condition>:
return True
return False
I have to say this assignment is interesting and challenging, especially for those worked solo like me. I am thinking of working in partner next time.
2015年3月13日星期五
2015年3月1日星期日
Recursion
This week's topic is chosen among three assigned topic. And I chose recursion because I was surprised by its beauty and efficiency, along with difficulty of implementing. In this slog, I will try to demonstrate some recursive structure and how it is different from looping structure.
It is my very first time to notice recursive structure when I read the structure with self inside. At that time, it was a function about nest list. Before learning recursion, I always wondered how to reach the element in a nest list with different 'depth', let's say [1, 2, 3, 4, [5, [6, 7]]], I can surely point out there are seven elements in this nested list. However, for computer, it is a list with five elements in it, and it is not true that every element is a list with elements in it. So, my first impression is we need a if statement and an else statement to differentiate these situations. At that time, I thought the code would be like this:
i = 0
if #element is a list:
#count the element in this list
for num in element:
i += 1
elif #element is not a list:
#i += 1
But later on, I find that it only works with list of 'depth' 2, if we have a complicated list such as [1, 2, 3, [4, 5, [6, [7]]]]. Then the situation is the num in the function is a list as well. So, obviously, using a loop might enable us to solve the problems with knowing how deep the list is. The problem, however, can be very sophisticated and we may not be able to always find the depth of 'target list'.
At that time, it was about week 4, Danny introduced us the solution to nested list, a recursive function. It is to sum up all numbers in a nested list. What surprised me is code, it is quite concise and tidy. The code is like:
def sum_list(L):
'''
# omited
'''
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else:
return L
At that time, I didn't know list comprehension, so I cannot figure out what this code really does. There is only one place where sum() shows, that is if L is a list, but why there appears else, and if we need a else statement, In what kind of situation do we need to use it? I have so many question. In the following weeks, Danny let us to trace recursion, first example was always pretty simple, which is base case that wouldn't use recursive part. When it comes more complicated part, I feels like can understand why code is like that, but cannot write out this code by myself.
It is my very first time to notice recursive structure when I read the structure with self inside. At that time, it was a function about nest list. Before learning recursion, I always wondered how to reach the element in a nest list with different 'depth', let's say [1, 2, 3, 4, [5, [6, 7]]], I can surely point out there are seven elements in this nested list. However, for computer, it is a list with five elements in it, and it is not true that every element is a list with elements in it. So, my first impression is we need a if statement and an else statement to differentiate these situations. At that time, I thought the code would be like this:
i = 0
if #element is a list:
#count the element in this list
for num in element:
i += 1
elif #element is not a list:
#i += 1
But later on, I find that it only works with list of 'depth' 2, if we have a complicated list such as [1, 2, 3, [4, 5, [6, [7]]]]. Then the situation is the num in the function is a list as well. So, obviously, using a loop might enable us to solve the problems with knowing how deep the list is. The problem, however, can be very sophisticated and we may not be able to always find the depth of 'target list'.
At that time, it was about week 4, Danny introduced us the solution to nested list, a recursive function. It is to sum up all numbers in a nested list. What surprised me is code, it is quite concise and tidy. The code is like:
def sum_list(L):
'''
# omited
'''
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else:
return L
At that time, I didn't know list comprehension, so I cannot figure out what this code really does. There is only one place where sum() shows, that is if L is a list, but why there appears else, and if we need a else statement, In what kind of situation do we need to use it? I have so many question. In the following weeks, Danny let us to trace recursion, first example was always pretty simple, which is base case that wouldn't use recursive part. When it comes more complicated part, I feels like can understand why code is like that, but cannot write out this code by myself.
订阅:
博文 (Atom)