Saturday, March 28, 2015

Week 7 impression

    The introduction of binary tree node (BTNode) seemed a little irrelevant to the topic. What's more bizarre was the pre-, post-, and inorder.
    BTNode is a tree similar to a normal tree. The difference is that in BTNode, The root has three attributes defined in the __init__ method: data, left, and right. Data is similar to the "value" in a normal tree, left is a subtree that has all the value that is smaller than the "data", and right is a subtree that contains all values larger than the "data". In a sense, BTNode is a sorted tree.
    The pre-, post-, and inorder seems complicated.
        Preorder:
            (root)->(left)->(right)
        Inorder
            (left)->(root)->(right)
        Postorder
            (left)->(right)->(root)
    The above is the way nodes of a tree is orderly visited.
    For example, if we have a tree like this:

                    Root
                   /       \
                A         B
              /    \       /    \    
             C   D     E    F
    Preorder would visit the tree in an order of: root, A, C, D, B, E, F
    Inorder: C, A, D, Root, E, B, F
    Postorder: C, D, A, E, F, B, Root

    However, in the assignment, we needed a tree to create all the possible next moves. What is showed in class seemed to be irrelevant. I can understand what BTNode and those orders are, but no idea where it may be useful.

Abstract Data Type

    What is abstract data types?Abstract data types (ADTs) are ways to store and retrieve data without detailed information about how they are done. That is, for example, what people need is a software that does something, but they don't need to or want to know how everything is put together to work.
    One type of ADTs is stack. A stack is a list of data, which in real life can be a stack of books, but the only way a stack can be modified is through the top one (eg. you can only add or remove from the top).
    For a class stack, we can often see methods like "push", which adds item on top of a stack; "pop", which removes the top item from a stack; "is_empty", which checks whether the stack is empty.
    Another type of ADTs is queue. While other properties are similar to a stack, queue, instead of removing starts from the top, starts removing from the bottom.
    The only difference between codes of stack and queue is the following:
    in (stack) method pop(self):
        return self._data.pop()
    in (queue) method dequeue(self):
        return self._data.pop(0)
    The "_" before "data" is used as a way of hiding information (which is already set up in the __init__ method) so that user don't see it. The clients need only to know what needs to be known, and hiding the data can protect local data being modified either by accident or with intention.

Object-Oriented Programming concepts

    Lets define some concepts to begin with.
    Class is a place where objects are defined (to have attributes that characterize what it is), which works in the way similar to how def works. Class also includes grouping of data and functions, which is called methods. One can imagine class being a blueprint for creating an object. 
    In assignment 1, we were asked to define a game called Subtract Square. In order to create this game, we need to use class to define a it, such as SubtractSquareGameState, to keep track of the state of the game, a strategy such as Strategy_Random, to select the next steps for computer, and SubtractSquareMove, to actually making a move. In the above classes, regardless of how complex our codes are, we defined the fundamental objects such as a move, a strategy, and a game state for the game.
    Attributes of the objects we wanted to create will be defined in an __init__ method, that when the class is called, such attributes will be implemented into the object. Those attributes are built upon "self", which, intuitively refers to the object itself. Methods are then used to define the object using the existing data.
    In the assignment, we also encounter inheritance. For example, from a general class strategy, a specific strategy called RandomStrategy can inherit all the traits from the general class, and it can also overwrite them. The advantage is when there is a number of strategies (general class) we want to define, and we don't want to waste time writing repeated codes, inheritance can be very helpful.
    Next, after we've created the definition of the game, we create objects following those definitions in another file, game_view. We use this file mainly to create all the objects we need in order to properly play a subtract square game, and by importing from previously defined classes, we are able to play the game.
    In the end, this simple game seems to be rather a complex object to create. I would not want to see how much code an actually enjoyable game requires.