Friday, April 24, 2009

Java Desktop Fortune teller

This is a simple java desktop application that "predicts" your fortune. It makes use of a getfortune method to display predefined fortune texts in a label. The getFortune method uses the random function from the "import java.lang.Math.*" package to choose the fortune to display.

fortune teller
public String getFortune() {
String prediction[]={"You will die a horrible death at 3:00 PM","You will win a lottery today","You will slip and fall today","Beware of red Vehicles","You will get a promotion today","You will become king one day","You will win the Nobel prize next year","You will win every contest you enter today","Your boss will go on leave for the next week","You will buy a car soon","You will become God"};
int forval=(int)Math.round(Math.random()*10);
return prediction[forval];

The different fortunes are stored in an array of strings and the value obtained from the random function is used as the array index. The value from random is between 0 and 1, and is multiplied by 10 to get the final value. Hence, the array will not go out of bound.

Thursday, April 23, 2009

Crayon Physics - error - limit on number of drawings

After writing a kind of tutorial on wining the seven different crayon physics puzzles or levels (crayon puzzles) in the demo mode, the game showed few limits, not by itself by the physics engine on which it runs. The error says "You Broke my Game! There's too many objects on the screen . The physics engine can't keep up with it. Unfortunately you will have to restart the level :("

crayon physics errorThe worst thing about this error is it pops up just when you are able to solve :) the puzzle.

The different levels of the demo are:

  1. Tutorial
  2. The 2 towers
  3. Tee Ters
  4. Lots of Space
  5. Log
  6. Sky Captain
  7. Nasty
The background music of the game is pretty melodious and not interfering. The final word is Crayon physics proves beyond doubt tat its an awesomely cool game :)

Wednesday, April 22, 2009

Crayon Physics - Nasty

The Nasty level in crayon physics has two stars to collect. Although its possible to collect both stars in a single go, this method shows collecting each of the stars in separate goes.

To collect the first star, first block the ball at the top. and create structures as in image below.
Nasty crayon Physics
Delete the blocking box to collect the first start. The ball falls down and comes for second go. Block the ball and build the next set of structures as in below image.

Nasty Crayon second starDraw both structures together along with guarding wall at the left bottom corner to collect both stars in a single go.

Crayon Physics - Sky Captain

Sky captain can be solved similar to TEE TERS by balancing the see saws and then setting the ball rolling. But make sure the right side is having some weight to counteract the weight of the square tat gets the ball rolling.

The seesaws can be balanced using a single beam or two separate beams. But make sure to use long beams to get them balanced.

Crayon Physics - Log

The Log can be solved very easily by removing the log and then setting the ball roll. However, can anybody solve it without making the log fall? The acclaimed tunneling effect from quantum physics is required for this :) Any quantum physicists out there? can u do it?

Anyway for the rest of us the easy solution is given below. First get the log out of the way by dropping beams on top of it. Then just get the ball rolling to collect the star.

crayon physics log
If any of you quantum physicists can do the tunneling effect without making the log fall, do let me know how u did it.

Crayon Physics - Lots Of Space

Lots of space seemed a bit difficult to solve to begin with as it required the drawing of boxes of exact sizes so tat the weight got balanced. If you draw a box tats too small or too big, they will both go tumbling down. The challenge is to fill the space and make the ball roll.
crayon physics lots of spaceDraw the leftmost vertical box first and make it stand properly. Next draw the slant pole so tat it gets balanced by the vertical box. Once the the two boxes get balanced, draw a third box to get the ball rolling.

A much easier way is to draw multiple boxes to make the levels nearly equal, and drawing a pole connecting the two. Then once the ball comes near the piled up boxes, delete them quickly so tat the ball falls on top of the star.

The way2 to solve lots of space teaches cantilever concept.
crayon physics lot of space cantileverSo lot of physics learning going on with the crayon.

Crayon Physics - tee ters

The third puzzle called the Teeters or TEE TERS can be solved by drawing a single bar that balances and equalises the two see saws and drawing another box to get the ball rolling.

This seems to be the simplest solution. So should we try to solve the puzzle by drawing the least number of stuffs? is tat the aim of the game? well tat would make the game boring and does not make the full use of the artistic power of Crayon Physics.

Crayon physics demo - the 2 towers

As if solving the first puzzle was not easy enough, the second puzzle can be solved like below. Just need to draw two boxes. One Box connecting the two towers and another to set the ball rolling.

crayon physics

This seems to be like a tutorial to solving the demo version of crayon physics, but dont limit your creativity to these solutions. Do explore more fun ways to solve these puzzles.

Actually dont just think of these as puzzles, they are a kind of art with purpose. The purpose of the art is to collect the star:) So wat are u waiting for pickup the crayons and start sketching.

Crayon Physics - awesomly talented game

Crayon physics is a really great game that tests your artistic sense and physics. No you dont need to be a particle physicist.

The demo version of the game comes with 7 puzzles and is very simple to learn and fun to play. The aim of the game is to collect stars by making a ball to move towards a star. The first puzzle can be solved easily based on the instructions given in the game background.

crayon physics

This is just one way to solve the game, but the same thing can be solved in innumerable different methods. The beauty of the game is tat its not a science, but an art. We learn handling the mouse, physics and art all at the same time :)

Hit the space bar to reset the game and the esc key to goto the menu. Do let me know if you solve the puzzles in the most artistic ways possible.

Wednesday, April 15, 2009

Robot Battle – the Dead Target Robot

Robot battle is a game that requires you to write programs to make the robots battle it out for supremacy. The robots have the same physical features; the program (the robot brain) is the only feature that decides the winning robot.

A distinct programming language called Robot Scripting language (RSL) is used to control the actions of the robot. The documentation included with the game is good enough to start writing your own robots (.prg files).

I just created a Dead Target Robot for doing target Practice. The Sample program created using the documentation included with the Robot Battle game defeats Dead Target Robot easily.

Source of Deadtarget.prg:

#The dumbest robot ever. Just sits and waits to get killed. Good for target practice.
name( "Dead Target" )
author( "Nagarjun V" )
version( "0.1" )

Source of Sample.prg:

# Init section sets up robot and registers handlers
name( "Sample" )
lockgun( true )
regcore( MyCore )
regdtcrobot( FoundRobot, 1 )

# if scan finds a robot, the event handler runs
scan( )
radarright( 5 )

# scan detected a robot, shoot at it then check again
fire( 1 )
scan( )

The results of a game between the above two robots:









Dead Target 0.1




Sunday, April 12, 2009

Borg - inspired from communists ?

Individuality is defined as the state or quality of being an individual; a person separate from other persons and possessing his or her own needs, goals, and desires. Hence, a persons needs, goals and desires make up his or her individuality.

Individualists consider one’s goals and desire more important than any society, group or institution. While collectivism, stresses that communal, community, group, societal, or national goals should take priority over individual goals. A utopia in which the needs, goals and desires of each individual are same or sacrificed for the good of the society will destroy the concept of an individual. Similarly an interconnected collective like the Borg have no individuality.

The Borg is an extreme case of collectivism. Such an extreme type of Collectivism can be considered a fictional form of communism (which prefers common ownership to private ownership). So was the Borg inspired from communists? Does it play on the American fears of communism prevalent at the time Star Trek was written? The assimilation of cultures and beings can be compared to the growth of communism assimilating countries.

The Borg also resemble Ants in many ways:
Ant colonies also have some fertile males called "drones" and one or more fertile females called "queens".
The colonies are sometimes described as super organisms because ants appear to operate as a unified entity, collectively working together to support the colony

So is the Borg inspired from ants or communists or a combination of both?

Infix to postfix or prefix?

The order of operations or precedence rule is used to determine the order in which operations need to be performed. The order of operations is just a protocol used for easier and common understanding of expressions and is not based on any mathematical or logical theorem. The history of the order of precedence is not very clear and its origins cannot be attributed to any single source. The originator of this rule was not brilliant enough to suggest the post or pre fix notation :)

Various acronyms such as (all of which represent the same order of precedence):
PEMDAS - Parentheses Exponents Multiplication Division Addition Subtraction Also remembered using the sentence - "Please Excuse My Dear Aunt Sally"
BEDMAS - Brackets Exponents Division Multiplication Addition Subtraction
BIDMAS - Brackets Indices Division Multiplication Addition Subtraction
BIMDAS - Brackets Indices Multiplication Division Addition Subtraction
BIODMAS- Brackets Indices Of Division Multiplication Addition Subtraction
BODMAS - Brackets Of Division Multiplication Addition Subtraction
BOMDAS - Brackets Of Multiplication Division Addition Subtraction
BPODMAS- Brackets Power Of Division Multiplication Addition Subtraction

The Microsoft calculator program uses the order of operations in the scientific mode but ignored it in the standard mode. The Google calculator adheres to the order of preference strictly as can be see in the result of 6*3/4^2*3+2*(3/6-4)*3^(2-4).

Humans require the order of operations as we use conventional infix notation. However, the computers use a postfix notation like Reverse Polish notation , which obviates the need for the order of operations. Few computer languages use a prefix notation, which does not require the order of precedence as well. So should humans try to move from the infix to postfix or prefix notation?

Temporary variables – 5 common mistakes

Temporary variables are a type of variable which are used to store a value obtained from a particular computation or data source, which will be used in a later stage of the program. Although temporary variables serve very useful purposes, they can be the cause of major software bugs that seem to occur inexplicably. The common coding or programing mistakes associated with temp variables:

1. Loss of precision: The use of a lesser precision variable for a temp variable can lead to loss of precision which leads to bugs.

Ex: Using a Integer variable to store the result of a calculation that includes a higher precision like float or double. Even in langauages like java by explicitly typecasting(coders mistake).

2. Value out of bound: Similar to the first mistake but occurs in arrays and strings. Can mainly lead to rendering mistakes.

Ex:While using the last few characters of a string or last few values of an array.

3. Multiple inputs: The temporary variable value can get overwritten by incorrect code even before its value is used.

Ex: Status being changed even before the code required to be executed in previous status completes execution.

4. Excessive memory usage:The excessive use of temporary variables can have an impact on the memory usage of the program.

Ex:Using temp variables even when they are not absolutely needed.

5. No exception handling ( Like Null value, negative value) :The temporary variables can behave in weird ways if various exceptional cases are not anticipated and guarded against.

Ex:Not guarding against storing negative values in temp variables later used as array index etc.

Friday, April 3, 2009

Goldilocks - program to check the temperature


class Goldilocks
public static void main(String[] args) throws IOException
Console c = System.console();
if (c == null) {
System.err.println("No console.");
String portemp = c.readLine("Enter the temperature of some porridge in degree centigrade: ");
int temp =(int)Float.parseFloat(portemp);
if (temp>40)
System.out.println("Too hot");
else if(temp<40)
System.out.println("Too cold");
System.out.println("just right ");