🔒 Closed Pokemon java!

Status
Not open for further replies.
Ask ko lang po kung sino makakatulong sakin sa Java, nasa pic po yung output. Naka zip naman po yung sample file. Sana matulungan nyo ko. Asap

For Pokemon Class:
Pokemon Attributes:
String name
String type
int life - all starting pokemon will have 100 life
int damage - level 1 starts at 10dmg
int level - always starts at level 1
int exp - 10exp at level 1
int speed - 20 speed

Pokemon Methods:
getLife
getName
getSpeed
getExp
gainExp - add 10exp
fight - method wherein the two pokemons will attack each other
attack - randomly add damage (0dmg-5dmg only) to the pokemon
levelUp - add one level to the pokemon and revert the life to 100

NOTE: all pokemon attributes are private

In the PokemonDemo (the file that contains the main method):
Instantiate two pokemons (name and type).
The pokemon that is faster will attack first.
They will continue to fight until either of the pokemon's
life is 0. The pokemon with the highest HP left is the winner.
The winner will gain 10 experience and level up if the experience is a multiple of 10.
 

Attachments

  • 18485853_1745386272143149_3934348056724594329_n (1).webp
    18485853_1745386272143149_3934348056724594329_n (1).webp
    24.1 KB · Views: 37
  • sample.zip
    sample.zip
    844 bytes · Views: 4
pede pa ba? gawin ko saglit. may mga nakita akong inconsistent sa rules mo:

"The winner will gain 10 experience and level up if the experience is a multiple of 10."
10 is a multiple of 10, so level-up to every time

"..all starting pokemon will have 100 life"
and yet kapag nag level-up 100 parin life.. wala rin setters, so forever 100HP nila (paasa ung "starting" haha)

"fight - method wherein the two pokemons will attack each other"
uh, so iimplement ko to ayon sa pagkaka-intindi ko ah

**and btw, walang faster pokemon kasi laging equal (20) speed nila.. hindi stated sa rules yung kung kelan mag cchange yung speed

EDIT: ganito muna, bwisit kasi ayaw mag-upload nung files. compiled and tested naman na yan, wala yung -HP bug

PokemonDemo.java
Code:
public class PokemonDemo {
   
    public static void main(String[] args) {
        Pokemon a = new Pokemon("Pikachu", "Electric");
        Pokemon b = new Pokemon("Bulbasaur", "Water :("); // haha
       
        System.out.printf("%s fights %s!\n", a.getName(), b.getName());
        System.out.println("--------------------------");
       
        while (a.getLife() > 0 && b.getLife() > 0) {
            // figure out who attacks first
            Pokemon first  = a.getSpeed() > b.getSpeed()? a: b;
            Pokemon second = first == a? b: a;
           
            Pokemon.fight(first, second);
        }
       
    }
   
}

Pokemon.java
Code:
import java.util.Random;

public class Pokemon {
       
    // Avoiding magic numbers like plague
    public static final int BASE_LIFE = 100;
    public static final int BASE_DAMAGE = 10;
    public static final int BASE_EXP = 10;
    public static final int BASE_SPEED = 20;
    public static final int STARTING_LEVEL = 1;

    private String name, type;
   
    private int life, damage, level, exp, speed;
 
    public Pokemon(String name, String type) {
        this.name = name;
        this.type = type;
        this.life = BASE_LIFE;
        this.damage = BASE_DAMAGE;
        this.level  = STARTING_LEVEL;
        this.exp    = BASE_EXP;
        this.speed  = BASE_SPEED;
    }
 
    public int getLife() { return life; }
    public String getName() { return name; }
    public int getSpeed() { return speed; }
    public int getExp() { return exp; }
   
    public void gainExp() {
        exp += getExpGain();
        System.out.printf("%s gained experience.\n", getName());
    }
   
    public static void fight(Pokemon a, Pokemon b) {
        System.out.println("--------------------------");
        a.attack(b);  
       
        // check first if the other pokemon had fainted
        if (b.life > 0)
            b.attack(a);
        else
            System.out.println("--------------------------");
    }
   
    public void attack(Pokemon opponent) {
        int totaldmg = damage + getBonusDamage();
        opponent.life = totaldmg < opponent.life? opponent.life-totaldmg: 0;
       
        System.out.printf("%s attacked %s!\n", getName(), opponent.getName());
        System.out.printf("%s's HP left: %d!\n", opponent.getName(), opponent.getLife());
        if (opponent.life == 0) {
            System.out.println("--------------------------");
            System.out.printf("%s won!\n", getName());
            gainExp();
           
            // acc. to rules, we will level-up every time!!
            if (exp%10 == 0) levelUp();
        }
    }
   
    public void levelUp() {
        level += 1;
        life = BASE_LIFE;
        System.out.printf("%s levelled up!!\n", getName());
    }
   
    // Below are methods that aren't explicity stated as rules, pero di 
    // naman pwede gumawa ng bagong class/fields kaya ginawa ko nalang
    // na private static methods...
   
    /** Returns the amount of exp gain. */
    private static int getExpGain() {
        return 10;
    }
   
    /** Rolls a dice and returns bonus 0-5 damage. */
    private static int getBonusDamage() {
        Random random = new Random();
        return random.nextInt(5);
    }
   
}

C:\path-to-folder-location> javac PokemonDemo.java & java PokemonDemo
 
pede pa ba? gawin ko saglit. may mga nakita akong inconsistent sa rules mo:

"The winner will gain 10 experience and level up if the experience is a multiple of 10."
10 is a multiple of 10, so level-up to every time

"..all starting pokemon will have 100 life"
and yet kapag nag level-up 100 parin life.. wala rin setters, so forever 100HP nila (paasa ung "starting" haha)

"fight - method wherein the two pokemons will attack each other"
uh, so iimplement ko to ayon sa pagkaka-intindi ko ah

**and btw, walang faster pokemon kasi laging equal (20) speed nila.. hindi stated sa rules yung kung kelan mag cchange yung speed

EDIT: ganito muna, bwisit kasi ayaw mag-upload nung files. compiled and tested naman na yan, wala yung -HP bug

PokemonDemo.java
Code:
public class PokemonDemo {
  
    public static void main(String[] args) {
        Pokemon a = new Pokemon("Pikachu", "Electric");
        Pokemon b = new Pokemon("Bulbasaur", "Water :("); // haha
      
        System.out.printf("%s fights %s!\n", a.getName(), b.getName());
        System.out.println("--------------------------");
      
        while (a.getLife() > 0 && b.getLife() > 0) {
            // figure out who attacks first
            Pokemon first  = a.getSpeed() > b.getSpeed()? a: b;
            Pokemon second = first == a? b: a;
          
            Pokemon.fight(first, second);
        }
      
    }
  
}

Pokemon.java
Code:
import java.util.Random;

public class Pokemon {
      
    // Avoiding magic numbers like plague
    public static final int BASE_LIFE = 100;
    public static final int BASE_DAMAGE = 10;
    public static final int BASE_EXP = 10;
    public static final int BASE_SPEED = 20;
    public static final int STARTING_LEVEL = 1;

    private String name, type;
  
    private int life, damage, level, exp, speed;
 
    public Pokemon(String name, String type) {
        this.name = name;
        this.type = type;
        this.life = BASE_LIFE;
        this.damage = BASE_DAMAGE;
        this.level  = STARTING_LEVEL;
        this.exp    = BASE_EXP;
        this.speed  = BASE_SPEED;
    }
 
    public int getLife() { return life; }
    public String getName() { return name; }
    public int getSpeed() { return speed; }
    public int getExp() { return exp; }
  
    public void gainExp() {
        exp += getExpGain();
        System.out.printf("%s gained experience.\n", getName());
    }
  
    public static void fight(Pokemon a, Pokemon b) {
        System.out.println("--------------------------");
        a.attack(b); 
      
        // check first if the other pokemon had fainted
        if (b.life > 0)
            b.attack(a);
        else
            System.out.println("--------------------------");
    }
  
    public void attack(Pokemon opponent) {
        int totaldmg = damage + getBonusDamage();
        opponent.life = totaldmg < opponent.life? opponent.life-totaldmg: 0;
      
        System.out.printf("%s attacked %s!\n", getName(), opponent.getName());
        System.out.printf("%s's HP left: %d!\n", opponent.getName(), opponent.getLife());
        if (opponent.life == 0) {
            System.out.println("--------------------------");
            System.out.printf("%s won!\n", getName());
            gainExp();
          
            // acc. to rules, we will level-up every time!!
            if (exp%10 == 0) levelUp();
        }
    }
  
    public void levelUp() {
        level += 1;
        life = BASE_LIFE;
        System.out.printf("%s levelled up!!\n", getName());
    }
  
    // Below are methods that aren't explicity stated as rules, pero di
    // naman pwede gumawa ng bagong class/fields kaya ginawa ko nalang
    // na private static methods...
  
    /** Returns the amount of exp gain. */
    private static int getExpGain() {
        return 10;
    }
  
    /** Rolls a dice and returns bonus 0-5 damage. */
    private static int getBonusDamage() {
        Random random = new Random();
        return random.nextInt(5);
    }
  
}

C:\path-to-folder-location> javac PokemonDemo.java & java PokemonDemo
Hi sir, tinry ko and gumana siya kaso nung sunday po kase ang deadline but thankyou pa rin hehe. I'll keep it naman. Good job sir, galing nyo:):):)
 
Status
Not open for further replies.

About this Thread

  • 16
    Replies
  • 1K
    Views
  • 6
    Participants
Last reply from:
Benofficial

Trending Topics

Online now

Members online
310
Guests online
1,133
Total visitors
1,443

Forum statistics

Threads
2,274,125
Posts
28,953,781
Members
1,235,089
Latest member
zuhniex
Back
Top