Monday, March 16, 2015

the object-oriented programming concept

My initial impression of 'object-oriented' programming came from CSC108H (Introduction to Computer Programming) as did most of my classmates in CSC148H (Introduction to Computer Science). The initial impression was that it was confusing to learn. Why did we need to learn about this concept as opposed to designing functions and eventually creating a program from it? The answer was not as obvious at first, but it became more clearer after the completion of the first assignment (for CSC148H). It's a tool for programmers to write code in a more efficient way that is intuitive, and potentially, reduce the amount of code written in the future.

In object-oriented programming, there are classes and sub-classes. The classes are meant to contain, if suitable, generic code that could be applied in numerous situations. The game state is an example of this from Assignment #1. From there, we could implement generic methods such as legal moves, score, number of players. If we decided to implement tic-tac-toe and blackjack, we immediately notice that we will run into some issues. A generic method implemented into the class that establishes that we are designing two-player games, such as setting up 'p1' and 'p2' will be perfectly fine for both games. However, we have completely different legal moves, and score tracking for both games. If we didn't use classes, we could just use various functions and eventually implement it into the program. On the other hand, we can use a sub-class for each two-player game we design, one for tic-tac-toe and blackjack. This will allow us to override each method for the main class, if we need to, in order to create individual methods for each game. In the end, we'll be able to reduce the amount of code to put together a program that allows you to play tic-tac-toe or blackjack. For instance, in order to determine the score, we can call upon the method for the game that the user has selected. Using classes and sub-classes, you can see why programmers might elect to go that route. By writing code to run the program, we can structure code for any general game to run, rather than write more code to run each specific game.

In conclusion, it's a very good idea to practice with classes and subclasses. Perhaps, even visualizing what else you could do with the code. For example, I want to develop a text-based hockey game. I've got a class for each period of play. But wait, what about overtime and the shootout? So we'll create a generic class for the Hockey game state, and have a class for a period in hockey, overtime, and shootout. In the period state, the game is over when the time has run down to zero, and we have already gone through two periods of hockey. We can call that the game_over method, and we can put it in the generic Hockey game state to be overrode by the subclasses. The overtime subclass will have to override game_over, since the game ends when a goal is scored. The same goes for the shootout subclass where it's more complex with the skater-by-skater round process. Designing an actual hockey text-based game is more complex, but you can see how the framework are being put together using classes and sub-classes.


No comments:

Post a Comment