mercredi 26 octobre 2011

AI contest

If you are interested in game programming and AI, you might want to take a look at the AI ants challenge. It is a challenge sponsored by google, and this is the presentation of the contest by the official website :
The AI Challenge is all about creating artificial intelligence, whether you are a beginning programmer or an expert. 
All you have to do is pick your favourite programming language. There is already about 20 of the most popular languages, and the contest organizers are willing to add another ones if they receive requests on the forum. Once you know what programming language you are going to use, you can download the tools that will help you in your quest, and then download the starter kit for that language. The game engine is written in python, so you'll need to install python on your computer. Don't worry if you don't know any python at all, you don't need to. As for the installation of python, it's pretty straightforward. If you are on a Linux machine, it should be installed already. If you are on windows, you can download a windows installer which will do the work for you, or you can use cygwin.

You should participate regardless of your level. You could get a good rank in the leader board, which will look good on your resume, but even if you don't, it was a good learning opportunity, and you'll have some prerequisites for future contests.

Good luck everyone!

vendredi 21 octobre 2011

Creating an object in actionscript

The creation of a visual component is an operation that requires many steps.
First, we start by constructing the component, either using the default constructor or any constructor you have defined on your own

var imDaButton:Button = new Button(); 

Now the component needs some properties, "hey, I'm like all the other buttons out there, give me some properties so that I feel a little bit different", said imDaButton
Here you go

imDaButton.label="warrior";

imDaButton.setStyle("cornerRadius", 2);

imDaButton.addEventListener(MouseEvent.CLICK, doStuff);



Ok, now our button is happy, it has some properties of its own. A label to be proud of, some weird corners, and it does stuff when it's clicked.

Once you've got your button all ready to hit the road, you can add it to a parent.


someParent.addChild(imDaButton); 

The method initialize() is called automatically when we call addChild(). This method is used to initialize the internal structure of the component.
It first dispatches a preinitialize event which could be used for example to load some external resources, like loading styles dynamically or loading resource bundles. Be careful when you use a listener for this event in your container and try to access children for the container. For example, if you have:


someParent.addEventListener(FlexEvent.PREINITIALIZE, parentPre); 


private function parentPre(event:FlexEvent):void

{
    imDaButton.enabled=false; 
}


In this case you'll have an error #1009 null has no properties since imDaButton wasn't created yet. So, when is it created?

That's simple, the method createChildren() is going to do that for you. That's where the component internal structure is created. It could be the class managing the button's label, which is UITextField in this case. This method can be overriden in custom components so that you create the children you want, and place them where you want once this method gets called


Now that your button was created, along with any other component in your container, the initialize event is dispatched, and you can now play with the children of your container using the listener to this event. 


After that, creationComplete event is dispatched to notify us that the children, and their children were created. Note that many other steps happen during the creation of the internal structure of a component: property processing, measuring, layout, and drawing.


dimanche 16 octobre 2011

Pointers

Pointers is such a difficult concept for beginners. Back when I started learning C, I was never able to understand them, not until I've read countless tutorials about them. So I'll try to explain them for anyone who will face the same issues as me. You already know what variables are and what we use them for. Let's take the example of the city you live in.  It's a mixture of houses, and different kinds of buildings. Our variables here are the buildings.

int my_var;  

Every building has an address. Likewise, every variable has an address in memory.

/** we can access the address of a variable in memory using &my_var

to print this value, we have to use %p in printf to print an address*/  

printf("the variable my_var  has the address: %p ", &my_var);

So what's a pointer, a pointer is also a variable. A variable which contains the address in memory of another variable. How do we illustrate this concept in our city. Suppose you want to send a postcard to your friend. In order for that postcard to reach its destination, you have to write the address of your friend. So a pointer in real life is that postcard. A pointer could be also, a direction sign pointing to your city. So when someone needs to get to your city, he has to "ask" that pointer. In C, some functions, the functions that take a pointer as parameter need to "ask" the pointer to know the value of the variable they point to.

/** declaring a pointer is pretty straightforward, you just give him the type of the variable he needs to point to, and the variable he points to */

int my_var = 7;

int *pointer_to_my_var = &my_var;

printf("the value of my_var is: %d", *pointer_to_my_var);

printf("the address of my_var is : %p", pointer_to_my_var);


To access the variable my_var using the pointer we have to use *, which is also called the indirection operator. Therefore, you should be careful, as you can see, it's not *pointer_to_my_var that contains the address of my_var, but pointer_to_my_var. So the following declaration is equivalent to the previous one.
int my_var = 7;

int *pointer_to_my_var ;   

pointer_to_my_var = &my_var ;


Ok, so what about functions that take as parameter a pointer.
int add(int * x, int * y) {

     return *x + *y;

}

//to call that function we have two methods

int x=7;

int y=9;

//we use the address of the variable directly

add(&x, &y);

// or declare a pointer to x and to y and pass them to the function

int *pointer_to_x=&x;

int *pointer_to_y = &y;

add(pointer_to_x, pointer_to_y);


That's what I called earlier asking the pointer to know the value of the variable it points to, it's like the postman read the address on your postcard so that he could deliver the postcard to your friend.

vendredi 14 octobre 2011

Thoughts about programming

IMHO, programming is not just about writing code, correcting bugs, learning a couple of programming languages, or knowing how to do some tricks with them. Programming is about solving problems, or even better, it's about the approach taken to solve a problem. Sometimes there is only one way to do things. But most of the time, there are many ways. What makes the difference between an average programmer and a good one is that the latter will always find a better way to solve the problem.

It's not that the first one is not able to find that solution, it's just that when he found the first solution that came to his mind, he didn't take the time to think further about the problem, he didn't ask himself the questions you should ask yourself: Is this the only way to do it? Isn't there a better way? Why don't I try to solve this problem in more elegant way. You have to challenge yourself, try to be better than yourself.

That's what makes the difference. There are average programmers who remain average either because they don't want to go that extra mile and try to find that better solution, or because someone taught them in the beginning some way to think, which was not the best way and they didn't have the chance to learn another way. As for the rest, those who aspire to become always better, stop writing code for a second, take the time to think deeply, try to read some code written by professional people and ask yourself why did they do it this way not that way. And as long as you are passionate about programming and you keep learning new things, you'll end up being the one finding the best solution.