Friday, 30 January 2015

On what basis to split classes

What should a generic turn based game consist of? How should classes be designed?

Whenever I program, I always try to think of some physical process. For example, when I was first tackling the sorting problem, I thought of insertion sort. When I sort my cards, that's how I sort. I trace back to how my brain does it and then I make the code do exactly that. Of course there may be limitations to this, but I find that this is a good starting point.

Similar methodology was used when I tackled this problem. When I imagine a game, what entities are involved? First and most obvious are the players. And then there is the some neutral entity that manages the game... I would call it the game master. The game master talks to the players and notifies them of the game state, announces other people's moves, etc. In poker this would be like the dealer. The game master is directly aware of the game state, since it keeps track and modifies it. The game master can learn about the game using the game state. I think of the game state as the chips/panel used to keep track of all the information and a rule book of how to manage the game. The game master refers to the book for what instructions are needed to be given to manage the game and also interacts with the record, modifying it and referring to it as needed. other_info is shown as an example to express that the game board can be extended (current number might be in the other_info). Note that some minor details are omitted and levels are only conceptual.

One "problem" I found with the assignment is that it is open but also closed. For example, in the game state I did not want to generate all possible moves. The rule_book only needs to be able to say whether the move can be made. There is no need to generate all moves. I wanted the computer to be able to find possible moves and so wanted to implement AI that is specific to the game. Part of the player's role is to find the moves given the game. Due to the structure of the requirements, it was more convenient for game state to be able to generate the moves. I  guess this is just a minor issue.

In a future post, maybe I will talk about more detailed implementation details. If you have any questions about how I did things, feel free to comment. I'm also interested in learning about how other people thought to do this.

Thursday, 22 January 2015

Why Use OOP?

Classes and Usefulness.

In class, we talked a lot about classes. I feel that the big question is why use OOP. Especially with the introduction of Stacks, Queues, and Assignment 1 in particular, many people just don't understand what their point is. For assignment 1... it's point is for us to learn, but in general, what are some advantages of using OOP?

I want to first outline briefly the conventional class design description and then discuss some of the advantages of classes.

Classes are containers. They contain information about something. Rather than having everything spread around everywhere, related variables and functions can be tied together. For example, we can have a dog object which might have a name, dog_type, weight, fur_status, and so forth as descriptors and bark, sleep, eat, poop, see, etc as is methods. The variables within the class describe the useful aspects of the object while the methods change the dog or do something depending on the dog.
We know that functions are useful because repetitive tasks are made easier and making changes later becomes easier. To elaborate on the latter, suppose that for sorting, faster algorithm was found. To make changes for all the things, all the positions where sorting was attempted needs to be found and then all of them must be changed. Also, parallel development is easier since the functions can be written simultaneously.

All the advantages of functions apply. You can make changes within the class methods or internal variables without affecting the external code that uses this class. Of course it also prevents one from needing to reinvent the wheel over and over again and allows parallel development.

An additional benefit of classes is evident in the above definition. It serves as a container, which groups concepts and breaks them down to facilitate implementation. Also interesting is the possibility of subclass usage.
The latter is similar to functions in that different functions can be passed. Let us analyze the following code:


Here, the function c is changed based on user input.

A similar task can be accomplished by using classes. Suppose you have a dog and a cat. You don’t know which one you might have… but you know they can both say hi… but the result of that should be different… The dog should output “woof” and the cat “meow.” This is where subclasses are useful. In a way, the act of being an instance of a particular subclass is in this case the instruction for the method say_hi.

Hope this was useful

Monday, 19 January 2015

response post: What is Self?

In my first post, I asked about how contents of an object is bundled together in the memory level in python. I have not found a concise answer but I tried the python visualizer (http://pythontutor.com/csc108h.html#mode=edit) with a code where I define a class and instantiate an object. The above code was executed. I highly recommend using it. It shows you step by step how it got there. But for lazy people, a copy of final result is shown:
you can see that the instance of Point p does not have the methods linked to it as attributes. This does not fully answer my original question fully, but provides more insight into how things are managed at the memory level by python.

Happy Exploring :)

Why Scientists (Geeks) Should Write

Why scientists should write!

I think that writing is important in all science disciplines. It is an effective way to share ideas with a wide variety of people. As Isaac Newton writes, he was standing on the shoulders of giants. It helps you become the giant for the next generation. 

Communication is important for collaboration. For example, new ideas may arise by reading ideas in other fields. For example, suppose the diffusion of water in polymers is not well described, but diffusion of water in soil systems is. Rather than reinventing everything, I could read about the diffusion of water in soil and evaluate aspects that apply to polymer systems or adapt it to make it fit. In this way, writing adds to the community and gives an opportunity for you to get feedback.

There are more personal reasons why scientists should write. In the long run, writing and publicizing your work pays you. For scientific research, you can get more funding. On a personal level, you can show off what kind of projects you were involved in. This may be crucial for computer scientists, in particular, since portfolio building is more important for comp sci than in say biochemistry where publications are more important.

On a more personal level, writing might help you understand concepts better. Sometimes, some ideas are floating in your mind, but you can't really explain... Sometimes, when you're done explaining, you have a new understanding. I personally find that this happens most when I am talking to people outside of the field, especially people not studying science. You often need to think of new ways to express things.

For me personally, I always wanted to write because it's fun to explain things sometimes. Questions I get by explaining allows me to understand things that I take for granted due to immersion in the field. Why do you think scientists should write?

Happy Writing

Monday, 12 January 2015

What is Self?

In today's lecture, concept of "self" when defining methods was elaborated upon. Before going in to understand this, let us consider the following example:


This example defines a point and has one method. The following defines a point P then shows two ways the method can be called:


lines 3 and 4 would evaluate to the same value. The following code however returns an error

TypeError: distance_from_origin() takes exactly 1 argument (2 given)
Here, the two arguments presumably are p_dummy and p.

How is Point.distance_from_origin(X) different from p_dummy.distance_from_origin(X)?
p_dummy is an instance of class Point. Different things happen for p_dummy.method() and Point.method(). When p_dummy.method(X) is called, it searches for Point.method and calls it, passing p_dummy (which is "self") and X as variables.

Let us think about memory space. I could not find specific information about how methods are stored, but I think p_dummy does not have direct information to the method, but rather some direction (like pointer) to it. All the general information is stored in the class Point. At that point, we can think of methods as functions. what the p_dummy.distance_from_origin() does is call the function Point.distance_from_origin() with the value p_dummy.

The above is just a hypothesis about how python works based on observations. I will try to look for detailed info regarding this for a future post.

Monday, 5 January 2015

Hello World!

Hi everyone. This is a course blog for csc148. It is an introductory computer science course. One of the requirements is to maintain a course blog.

Today, during my first lecture in intro to comp sci, core concepts were reviewed. I can't remember much but hopefully it will all come back. Anyhow, I'm going to try to make a post every Monday. I kind of wanted to blog but I just never got the chance to start. I think its cool that it's a requirement. It motivates me to at least start. I guess only time will tell if I stick to at least one a week schedule.
The interesting concept of the day was related to how the variable space is organized. It reminded me of when I was talking to someone with C++ background. He could not understand how things could be done without pointers. Today's lecture made it seem like all variables are pointers. Maybe the list is a collection of pointers? How would the list know where to start and end? Similarly, how can the elements in an object be bound (for example self.x) to the object? If they are in a different "memory space," in a more basic level, how are the lines drawn?