Saturday, March 28, 2015

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.

No comments:

Post a Comment