Pages

Sunday, March 29, 2009

AI Programming

From the time I entered college, I was fascinated about game programming. Once my senior told me, when the API of the game is already in you hands, the coding part can be as easy and fun as it can ever get. Yet it took me almost two years to get the determination to code for a game programming event. It was for Abacus '09, the National level symposium organised by the Department of computer science in our college. The game scenario was simple. Its similar to the snake game that everyone invariably must have played on a nokia mobile. The difference was there will be two snakes on the arena and food will be randomly generated at four points on the play area. Also, the snakes will have an energy associated with them which will decrease by a unit for every move and increase by some units for each food particle consumed. The organisers provided the API and all we had to do is to code a function that returns one of the four characters - U, D, R, L as output corresponding to the up, down, right and left movement of the snake. We are provided with the snake's position i.e., row and column number of each of the element of the snake's body, opponents position, our energy and opponents energy. Each coordinate of the play area is assigned a character where 'E' corresponds to empty, 'F'-food, 'X'-boundary, 'A'-snake A's body, 'B'-snake B's body. With so much information available at hand, the coding becomes really easy. but still, I'm a beginner! :)

I started coding and tested the game by using the same code for both the snakes(during the event, one snake will use your code and the other will use your opponent's code). After a couple of days, I thought I've done whatever I could do and submitted the code. As I'm not a great programmer, I didnt plan the algorithm at the beginning. I kept changing the structure as I kept testing the game. during submission, I could easily see that my code was divided into 4 parts.

1) Locating all the 4 food particles
2) Locating the food particle that is nearest to the snake's position
3) An initial instruction for navigation towards the food(avoiding the boundary)
4) Checking whether the initial instruction is a valid move(bumping into yourself or the opponent means you are out of the game!) and giving a valid and safe instruction.

This code works efficiently until the randomly generated food makes you move in such a way that you make a trap for yourself!


In the above picture you can see the snakes moving towards the nearest food.

In this picture, snake a bumped onto itself and lost the game!

As, many of the participants(except one) coded in a similar way, it was obvious that the random generation of the food will be the factor determining the winner. The better your luck, the greater your chances of winning. My luck was so good that my code won the second position!! Only later I heard from the organisers that the guy's code which won the first place was far more superior than the rest. That code was such that the snake wont trap itself often and also it'll try to blog the opponent and trap it!! How about that?!?!

3 comments:

  1. This one is for abacus na.......Heard about the results??? Evrytime only the first place was bagged by the same person....Other four places came out randomly.........

    ReplyDelete
  2. Yep! Its the Abacus event. You are right.. It was luck which determined the positions other than the first! The code which won the first place seem to have won 7/8 games!

    ReplyDelete
  3. for your info...prem's snake finished second !!! incredible work by a mechanical engineer... :-)

    ReplyDelete

Please leave your comment here.