Results 1 to 2 of 2
  1. #1
    outforlunch is offline Member
    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Default Is there any way to improve this code?

    So i made my first game, it is called kill the dragon. it took me an hour to make.

    It gives the player the option to use 1 of 4 spells to kill the dragon. it works well but is there a way to make it better?

    PHP Code:
    import java.util.*;
    public class killthedragon {
    
    	
    	public static void main(String[] args) {
    		String spellselect;
    		int hp = 160, dhp = 200; //mine and the dragons hp.
    		int fireball, fmin = 20, fmax = 25; //fireball spell
    		int firerange = 20 - 25+1;
    		int swingsword;
    		int waterblast, wmin = 30, wmax = 35;
    		int lightningbolt , lmin = 35, lmax = 40;
    		int forloop = 1;
    			System.out.println("*******************");
    			System.out.println("* Kill the Dragon *");
    			System.out.println("*******************");
    			System.out.println("");
    			System.out.println("It is your quest to kill the dragon");
    			System.out.println("");
    			System.out.println("You have 4 ablilitys");
    			System.out.println("You can use fire bolts");
    			System.out.println("You can shoot water blasts");
    			System.out.println("You can harness the power of lightning");
    			System.out.println("And you always have your trusty sword.");
    			
    			for(;;){
    				for(int i = 0; i < forloop; i++){
    					Scanner scan = new Scanner(System.in);
    					
    					Random fposs = new Random();//the possiblitiy of the spell casting the spell.
    					int possf = fposs.nextInt(2);//it will make a number between 1 and 2
    					Random wposs = new Random();//water posibiliy
    					int possw = wposs.nextInt(2);
    					Random lposs = new Random();
    					int possl = lposs.nextInt(3);
    					Random dragonposs = new Random();
    					int dragonhit = dragonposs.nextInt(3);
    					
    					
    					
    					Random fire = new Random() ;//fire
    					fireball = fire.nextInt(25 - 20) + 20;//fire ball within the range of 25 though 20. weakest spell.
    					Random water = new Random();//water
    					waterblast = water.nextInt(35 - 30)+30;
    					Random lightning = new Random();
    					lightningbolt = lightning.nextInt(40 - 35)+ 35;
    					Random sword = new Random();//the trusty sword
    					swingsword = sword.nextInt(17 - 15)+15;
    					Random dragonattack = new Random();//the amount of damage the player takes.
    					int playerdmg = dragonattack.nextInt(27 - 25)+ 25;//dragons dmg point range.
    					
    					System.out.println("How to do you want to attack?");
    					
    					System.out.println("Do you want to use fire, water, lightning, or swing your mighty sword?");
    					System.out.println("fire is 20-25dmg, water is 30-35, lighning is 35-40. sword is 15 but will hit every time.");
    					spellselect = scan.next();
    					if(spellselect.equals("fire")){
    						if(possf != 1){
    							System.out.println("Your spell missed entirely.");
    						}
    						if(possf == 1){
    						dhp=dhp-fireball;
    						System.out.println("WHOOSH! you did "+ fireball + " damage. the dragon still has "+ dhp + " HP");
    						}
    					}
    					if(spellselect.equals("water")){
    						if(possw != 1){
    							System.out.println("Your spell missed entirely");
    						}
    						if(possw == 1){
    						dhp = dhp-waterblast;
    						System.out.println("SPLASH! You did "+ waterblast + " To the dragon. it still has "+ dhp + " left.");
    						}
    					}
    					if(spellselect.equals("lightning")){
    						if(possl != 2){
    							System.out.println("Your spell missed entirely.");
    						}
    						if(possl == 2){
    						dhp= dhp-lightningbolt;
    						System.out.println("ZZZZZTTT! You shocked the dragon doing " + lightningbolt + "Damage. it still has "+ dhp + " left.");
    						}
    					}
    					if(spellselect.equals("sword")){
    						dhp= dhp-swingsword;
    						System.out.println("DING! you swung your sword and did " + swingsword +" damage. the dragon still has "+ dhp + "HP.");
    					}
    					System.out.println("");
    					System.out.println("");
    					
    					if(dragonhit != 1){
    						System.out.println("The dragon is attacking....but he missed.");
    					}
    					if(dragonhit == 1){
    						hp= hp-playerdmg;
    						System.out.println("The dragon is attacking. the Dragon did " + playerdmg +" damage. you still have "+ hp + "HP");
    					}
    					System.out.println("");
    					System.out.println("");
    					
    					
    					if(hp <= 0){
    						System.out.println("You died, but your bravery will never be forgoten...what were we talking about.");
    						System.exit(0);
    					}
    					if(dhp <= 0){
    						System.out.println("YOU WON! YOU KILLED THE DRAGON!!!");
    						System.exit(0);
    					}
    					
    				}
    			
    	}
    
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Is there any way to improve this code?

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Can I improve this code?
    By jobud9 in forum New To Java
    Replies: 4
    Last Post: 02-15-2012, 05:10 AM
  2. joptionpane quizz , improve my code ....
    By pipikrk in forum New To Java
    Replies: 6
    Last Post: 08-30-2011, 02:04 AM
  3. Improve my GUI!
    By AJArmstron@aol.com in forum New To Java
    Replies: 8
    Last Post: 04-27-2010, 10:17 PM
  4. How to improve this code
    By Raymond in forum Advanced Java
    Replies: 9
    Last Post: 03-19-2010, 09:16 AM
  5. Replies: 16
    Last Post: 08-05-2008, 12:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •