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?!?!

A rewarding blog, atlast!

This year's Kriya at PSG Tech introduced an online blogging event. I've been blogging for more than two years(though spent much less time on updating blogs). So I wanted to try my luck in the event. i named the blog "Techster" and started blogging. The Kriya team used MU-Wordpress on their server to host the blogs. i enjoyed working with wordpress after a long time. As usual, I wrote some technical articles and few general ones, altogether 7 of them. With just seven posts on the blog, I never expected to wi the event and was surprised and elated after the results were announced. When i was there at PSG Tech to collect my prize, the event coordinator told me that my blog was selected for the originality of posts. A reward for original work! :)

Monday, March 9, 2009

Swing of a Cricket ball

Its time to write about something which I love the most - Cricket! When we [members of CEG cricket team] were sitting at the sidelines while our batsmen were doing the job against IIT Madras 'B' Team, one of my team mates told me that my bowling is quite effective as I deliver the ball with an upright seam which eventually aids swing. But how does it swing???

The Physics

The fundamental elements involved in the physics of swing of a cricket ball are turbulent and laminar flow of air around the ball. The swing caused is NOT BECAUSE OF MAGNUS EFFECT as the movement is along the axis of rotation of the ball and not perpendicular to it.
Inswing and Outswing

For conventional swing, the ball will move towards the rougher side. When the seam is held such that it points towards the slip and the ball is held with the rougher side on the left side, the flow of air around the ball will be turbulent on the left side and laminar on the right side. This happens when the ball is bowled at about 120 kmph or less. As the pressure on the turbulent side is lesser, in accordance with Bernoulli’s principle, the ball swings towards the left(outswinger). Inswinger occurs in a similar way when the rougher side is on the right.
Reverse swing
When the ball becomes old and if the bowler is able to generate pace higher that 120 kmph, the swing characteristics differ. The ball begins to swing in the direction of the polished side and hence reverse swing. Incase of reverse swing, the magnitude of the ball’s movement is stronger than conventional swing. It also occurs late after the ball is delivered. At higher speeds of the ball, the flow is turbulent on both sides, but the separation of airflow from the ball occurs earlier on the rougher side. So the smoother side will have constant turbulent flow and hence have a low pressure. Thus the ball moves towards the shiny side. The raised seam aids separation of airflow(also for conventional swing).
Well, as we all know Zaheer and Ishant are capable of getting reverse swing consistently(remember Ishant’s 8 over spell against Ponting?). Hope India wins the series against New Zealand :-)