My Experiments with JAVA

  Hello everyone. This month has been somewhat less exciting for us. No major articles, no COOL projects. But as you know, everything happens for a good cause. For the last few weeks, we have been busy working with JAVA. As you know, most of our programs are in either C/C++ or Visual Basic. However, we thought it would be worthwhile to give JAVA a try. We have developed several simple programs with JAVA during these weeks. We’re looking forward to develop several JAVA applications in near future. This article shows you an example of my experiments with JAVA.

My Experiments with JAVA

Like I’ve already said, we haven’t developed anything significant in JAVA yet. But you can certainly expect some nice stuff in December, particularly towards the end of the year.

As we have learnt during our encounter with JAVA, JAVA is a totally different beast than C/C++. Programs in JAVA aren’t meant to be executed directly by the OS. They have to be executed by the JVM. So, it is quite annoying when you’re getting started with JAVA programming. Also, unlike Visual C++, you can’t just create a Console Application Project and start developing a game with #, * and $ (like you could do in case of games like the Snake Game). Also, you cannot use the Win32 GDI in JAVA. Swings have to be used instead.

Anyway, here’s what we have accomplished till now. It’s not so COOL, and certainly not great. However, it’s a great start and within a month or so. we’ll have a page on JAVA, listing some of our JAVA projects.

Zombie Killer

Zombie Killer is one of the best parts of my experiments with JAVA. As you know, JAVA is a fully object-oriented language. Hence, everything happens inside classes. There are no global variables or methods. We tried to develop the logic for a zombie killer game, and the below code is what we have come up with. This ‘game’ has three types of zombies defined using classes : ‘Dog Zombie’, ‘Man Zombie’ and ‘Dino Zombie’. There is a ‘ZombieShooter’ class for defining the fighter. This ‘game’ doesn’t actually involve any kind of user interaction. Hence, it looks more like an ‘object-oriented’ commentary.


class game{
    public static void main(String args[]){
        System.out.println("Welcome to ZombieKiller. Kill zombies, Save the World");
        zombieshooter zs=new zombieshooter();

        // Fight the first zombie
        {
            zombiel zl=new zombiel();
            while(zs.endurance>0){
                zl.attack(zs);
                zs.attack(zl); 
                if (zl.endurance <= 0) {
                  zl.dead();
                  break;
                  }
                }
                if ((zl.alive==false)&&(zs.alive==true)){
                  zs.endurance += 50;
                  System.out.println("Congratulations, you have proceeded to the next level");
                }
                else{
                  zs.dead();
                  System.exit(0);
                  }
                }
       // Fight the second zombie
       {
            zombiem zm=new zombiem(); 
            while(zs.endurance>0){
                zm.attack(zs);
                zs.attack(zm);          
                if (zm.endurance <= 0){ 
                  zm.dead(); 
                  break; 
                  } 
                } 
                if ((zm.alive==false)&&(zs.alive==true)) {
                     zs.endurance += 100;
                     System.out.println("Congratulations, you have proceeded to the next level");
                }
                else {
                     zs.dead(); 
                     System.exit(0); 
                     }
       }
      // Fight the third zombie 
      {
            zombieh zh=new zombieh(); 
            while(zh.endurance>0){
                zh.attack(zs);
                zs.attack(zh);             
                if (zh.endurance <= 0){
                    zh.dead();
                    break;
                }
            }

            if ((zh.alive==false)&&(zs.alive==true))
            {
                zs.victory();
            }
            else
            {
                zs.dead();
                System.exit(0);
            }
        }
    }
}

class zombiel{
    int power;
    int endurance;
    boolean alive;

    zombiel(){
        alive = true;
        power = 10;
        endurance = 100;
    }
    void attack(zombieshooter z){
        System.out.println("Dog zombie attacked you.");
        z.endurance -= power;
    }
    void dead(){
        alive = false;
        System.out.println("Dog zombie is dead.");
    }
}

class zombiem{
    int power;
    int endurance;
    boolean alive;

    zombiem(){
        alive = true;
        power = 25;
        endurance = 200;
    }
    void attack(zombieshooter z){
        System.out.println("Man zombie attacked you.");
        z.endurance -= power;
    }
    void dead(){
        alive = false;
        System.out.println("Man zombie is dead.");
    }
}

class zombieh{
    int power;
    int endurance;
    boolean alive;

    zombieh(){
        alive = true;
        power = 50;
        endurance = 300;
    }
    void attack(zombieshooter z){
        System.out.println("Dino zombie attacked you.");
        z.endurance -= power;
    }
    void dead(){
        alive = false;
        System.out.println("Dino zombie is dead.");
    }
}

class zombieshooter{
    int power;
    int endurance;
    boolean alive;

    zombieshooter(){
        alive = true;
        power=50;
        endurance=200;
    }
    void attack(zombiel z){
        System.out.println("You fired a shot");
        z.endurance -= power;
    }
    void attack(zombiem z){
        System.out.println("You fired a shot");
        z.endurance -= power;
    }
    void attack(zombieh z){
        System.out.println("You fired a shot");
        z.endurance -= power;
    }
    void dead(){
        System.out.println("You are dead. The zombies will take over the world... :'(");
    }
    void victory(){
        System.out.println("Hurrah.. You did it !!! You saved the world :)");
    }
}    
    	
// Zombie Killer - My Experiments with JAVA

My Experiments with JAVA screenshots

My Experiments with JAVA

Very soon, we’ll publish our JAVA projects. The initial projects would be based on topics already used in our C++ and Visual Basic programs. The next few projects might be included in the My Experiments with JAVA series.

(Visited 195 times, 1 visits today)

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *