Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.2 kB
5
Indexable
Never
import java.util.Random;
import java.util.Scanner;

public class BasicPokemon1 {
    String name;
    int hitpoints;
    // Constructor to make a BasicPokemon object with a name and hitpoints
    public BasicPokemon(String name1, int hitpoints1) {
        name = name1;
        hitpoints = hitpoints1;
        
    // Gets the current hitpoints of the Pokemon
    public int getHitpoints() {
           return hitpoints;
    }
    // Set the hitpoints of the Pokemon
    public void setHitpoints(int hitpoints1) {
        hitpoints = hitpoints1;
    }
    // Reduce the hitpoints of the Pokemon by a specified amount
    public void takeDamage(int amount) {
        hitpoints -= amount;
    }
    // Attack 
    public void attack(BasicPokemon enemy) {
        Random random = new Random();
        int damage = random.nextInt(6);

        if (damage == 0) {
            System.out.println(name + " Attacks and misses!");
            System.out.println("════════════════════════════════════"); //Print damage message and separator
            System.out.println("\nPress 'enter' key to continue the game!");

        } else {
            System.out.println(name + " Attacks and deals " + damage + " damage!"); 
            System.out.println("════════════════════════════════════"); //Print damage message and separator
            System.out.println("\nPress 'enter' key to continue the game!");

        }

        enemy.takeDamage(damage);
    }
    // Check if the Pokemon is still alive
        if (hitpoints > 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        BasicPokemon ally = new BasicPokemon("Pikachu", 40);// Ally Pokemon is named Pikachu with 40 hitpoints
        BasicPokemon enemy = new BasicPokemon("Charmander", 40);// Enemy Pokemon is named Charmander with 40 hitpoints

        Scanner input = new Scanner(System.in);

        System.out.println("Press 'enter' key to start a game!");
        //Start game loop
        while (ally.isAlive() && enemy.isAlive()) { //Game continues as long as both Pokemon are alive


            if (ally.isAlive()) {  // Ally attacks enemy
                input.nextLine();
                ally.attack(enemy);
            }
            
            if (enemy.isAlive()) { // Enemy attacks ally
                enemy.attack(ally);
            }
        }
        //ame ending here
        if (ally.isAlive()) {                       // Print ally victory message
            System.out.println(ally.getName() + " Won against "    
                    + enemy.getName() + " With "
                    + ally.getHitpoints() + " Hitpoints remaining!");
                    

        } else if (enemy.isAlive()) {               // Print enemy victory message
            System.out.println(enemy.getName() + " Won against "
                    + ally.getName() + " With "
                    + enemy.getHitpoints() + " Hitpoints remaining!");
        } else {
            System.out.println("Error"); // Error if both pokemons healths are at 0
        }
    }
}
Leave a Comment