OOP如何将失败的敌人的钱加到主角的包里?

时间:2019-12-12 01:29:45

标签: java oop methods text-based

我几乎已经完成了这个Java游戏项目。我只需要在主角的包里增加一个被击败的敌人的钱。如果它们比主角的武器和盔甲更好,我还必须添加武器和盔甲。如何将敌人的钱加到主角的钱上?在我看来,main_ch.getBag()。getMoney()= main_ch.getBag()。getMoney()+敌人[selected] .getBag()。getMoney();应该可以,但是不能。

这是我的主要课程:

import java.util.Scanner;

import Character.Character;

import java.util.Random;
public class Main {

public static void main(String[] args) {    
    Scanner scanner = new Scanner(System.in);
    System.out.println("> Welcome to The Game Project <");
    System.out.println("\n >> Input Main Character Name: ");
    String main_name = scanner.nextLine();
    System.out.println(">> Input Main Character Power: ");
    int main_power=scanner.nextInt();
    System.out.println(">> Input Main Character Hp: ");
    int main_hp=scanner.nextInt();
    System.out.println("----------------------------------------------------");

    Character main_ch=new Character (main_hp,main_power,main_name);
    show_status(main_ch);
    check_bag(main_ch);

    Character enemies[]=new Character [10];
    enemies[0]= new Character("Werewolf");
    enemies[1]= new Character("Vampire");
    enemies[2]= new Character("Alien");
    enemies[3]= new Character("Witch");
    enemies[4]= new Character("Ghost");
    enemies[5]= new Character("Skeleton");
    enemies[6]= new Character("Zombie");
    enemies[7]= new Character("Demon");
    enemies[8]= new Character("Mummy");
    enemies[9]= new Character("Dragon");

    boolean check = false;

    int dead_count=0;
    while(true) {

        Random rnd=new Random();
        int selected = rnd.nextInt(enemies.length); //random enemy selected

        System.out.println();
        System.out.println(">>>>> An enemy has appeared! <<<<<");
            while(enemies[selected].getHp()>0) { 
                System.out.println();
                System.out.println(">>>>> "+enemies[selected].getName()+" wants to fight!");
                show_status(enemies[selected]);
                check_bag(enemies[selected]);
                System.out.println();
                System.out.println(">> What do you want to do?");
                System.out.println("\t1. Fight!");
                System.out.println("\t2. Use skill.");
                System.out.println("\t3. Check your stats.");
                int input = scanner.nextInt();

                    if(input==1) {

                            int damageToEnemy = main_ch.hit_point();
                            int damageTaken = enemies[selected].hit_point();
                            enemies[selected].hp -= damageToEnemy;
                            main_ch.hp -= damageTaken;
                            if(enemies[selected].hp <= 0) {
                                enemies[selected].hp=0;
                                   dead_count=dead_count+1;
                                main_ch.level=main_ch.level+1; //gain one level after enemy defeated
                                System.out.println(">> You defeated the enemy and gained a level!");

// The below code also doesn't work.
                                int pickUpMoney = main_ch.getBag().getMoney();
                                pickUpMoney=main_ch.getBag().getMoney() + enemies[selected].getBag().getMoney();
                                System.out.println();
                                System.out.println("You found "+enemies[selected].getBag().getMoney()+" dollars. You now have "+main_ch.getBag().getMoney()+" dollars in your bag."); //take defeated enemy's money

                                for(int i = 0; i<4; i++) {
                                    if(enemies[selected].getWeapon().getPower() > main_ch.getWeapon().getPower()) { 
                                        main_ch.getBag().getWeaponArray()[i]=enemies[selected].getBag().getWeaponArray()[i];
                                        System.out.println("You found better weapons! They have been added to your bag.");
                                    }
                                }
                                for(int i = 0; i<4; i++) {
                                    if(enemies[selected].getArmor().getDefense()>main_ch.getArmor().getDefense()) {

                                        main_ch.getBag().getArmorArray()[i]=enemies[selected].getBag().getArmorArray()[i];
                                        System.out.println("You found better armor! They have been added to your bag.");
                                    }


                                }
                                break;

                            }
                            System.out.println("\n>> You caused "+ damageToEnemy +" damage to the enemy! Their hp is now "+ enemies[selected].hp+".");
                            System.out.println(">> You received "+ damageTaken +" damage from the enemy! Your hp is now "+main_ch.hp+".");

                            if(main_ch.hp <=0) {
                                System.out.println();
                                System.out.println(">> Oh no! You died! Better luck next time. Thanks for playing!");
                                System.out.println();
                            break;
                            }   

                        }

                    else if(input==2) {
                        if(main_ch.getSkill()>0 && main_ch.getMp()>0) {
                            main_ch.useSkill();
                            System.out.println("\t>> You used a skill. Your hit point increased to "+main_ch.hit_point()+". Your MP decreased to "+main_ch.getMp()+".");
                        }
                        else {
                            if(main_ch.getSkill()<=0) {
                                System.out.println("You have no skill points left.");
                            }
                            else{
                            System.out.println("\t>> Your MP is too low to use skill.");
                        }
                    }
                    }
                    else if(input==3) {
                        System.out.println();
                        show_status(main_ch);
                        check_bag(main_ch);                                             
                        }
                    else {
                        System.out.println(">> You have entered an invalid key.");

                    }
                    }



            if(dead_count==enemies.length) {
                check=true;
            }
            if(check) {
                System.out.println();
                System.out.println(">>>>>>>>>> You won! Congratulations, you defeated all of your enemies! <<<<<<<<<");
                break;
           }
            if(main_ch.hp <=0) {
                System.out.println();
                System.out.println(">> Oh no! You died! Better luck next time. Thanks for playing!");
                System.out.println();
                break;
            }



            }
        }
public static void show_status(Character character) {
    System.out.println("----------------- Character Status -----------------");
    System.out.println("\tCharacter Name:\t\t"+character.getName());
    System.out.println("\tCharacter HP:\t\t"+character.getHp());
    System.out.println("\tCharacter Power:\t"+character.getPower());
    System.out.println("\tCharacter Defense:\t"+character.getDefense());
    System.out.println("\tCharacter MP:\t\t"+character.getMp());
    System.out.println("\tCharacter Level:\t"+character.getLevel());
    System.out.println("\tCharacter Hit Point:\t"+character.hit_point());
    System.out.println("\tCharacter Skill:\t"+character.getSkill());
    System.out.println("\tWeapon Name:\t\t"+character.getWeapon().getName());
    System.out.println("\tWeapon Power:\t\t"+character.getWeapon().getPower());
    System.out.println("\tArmor Name:\t\t"+character.getArmor().getName());
    System.out.println("\tArmor Defense:\t\t"+character.getArmor().getDefense());
    System.out.println("----------------------------------------------------");
}
public static void check_bag(Character character) {
    System.out.println("-------------------- Bag Status --------------------");
    System.out.println("\tMoney:\t\t\t$"+ character.getBag().getMoney());
    for(int i = 0; i<4; i++) {
        System.out.println("\tWeapon Name/Power:\t"+ character.getBag().getWeaponArray()[i].getName()+" // "+character.getBag().getWeaponArray()[i].getPower());
    }
    for(int i = 0; i<4; i++) {
        System.out.println("\tArmor  Name/Defense:\t"+ character.getBag().getArmorArray()[i].getName()+" // "+character.getBag().getArmorArray()[i].getDefense());
    }
    System.out.println("----------------------------------------------------");
}

}


这是我的角色班级:

package Character;
import java.util.Random;
import Equipment.*;

public class Character {
    private Armor armor = new Armor();
    private Weapon weapon = new Weapon();
    private Bag bag = new Bag();

    public static String server_name = "CS172";
    public int hp, power, defense, mp, level, skill;
    private String name;
    Random rnd=new Random(); 

    public Character(String name) {
        this.name=name;
        Random rnd=new Random();
        this.hp=rnd.nextInt(500)+1;
        this.power=rnd.nextInt(100)+1;
        this.defense=rnd.nextInt(100)+1;
        this.mp=rnd.nextInt(50)+1;
        this.level=1;
        this.skill=5;
}
    public Character(int hp, int power, String name) {
        this.hp=hp;
        this.power=power;
        this.name=name;
        this.defense=rnd.nextInt(100)+1;
        this.mp=rnd.nextInt(50)+1;
        this.level=1;
        this.skill=5;
}
    public int getHp() {
        return hp;
}
    public void setHp(int hp) {
        this.hp = hp;
}
    public int getPower() {
        return power;
}
    public void setPower(int power) {
        this.power = power;
}
    public int getDefense() {
        return defense;
}
    public void setDefense(int defense) {
        this.defense = defense;
}
    public int getMp() {
        return mp;
}
    public void setMp(int mp) {
        this.mp = mp;
}
    public int getLevel() {
        return level;
}
    public void setLevel(int level) {
        this.level = level;
    }

    public String getName() {
        return name;
}
    public int damage(int enemy_power) {
        int damage = enemy_power - this.defense;
        if(damage<0){ //avoid healing by damage
            damage=0;
}
        this.hp=this.hp - damage;
        if(this.hp<0) { //avoid negative hp
            this.hp = 0;
}
        return damage;
}

    public Armor getArmor() {
    return armor;
}
    public void setArmor(Armor armor) {
        this.armor = armor;
}
    public Weapon getWeapon() {
        return weapon;
}
    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
}
    public int hit_point() {
        int total_power = this.power + this.weapon.getPower();
        return total_power;
    }
    //------------------------------------------------------why isn't this increasing the hit point at all?
    public int useSkill() {
        this.mp=this.mp-1;
        this.skill--;
        return this.hit_point()+30;
    }
    public int getSkill() {
        return skill;
    }

    public Bag getBag() {
        return bag;
    }
    public void setBag(Bag bag) {
        this.bag = bag;
    }

    public class Bag{
        Weapon weaponArray[] = new Weapon[4];
        Armor armorArray[] = new Armor[4];
        int money = 150;
        public Bag(){
            for(int i=0; i<weaponArray.length; i++) {
                weaponArray[i] = new Weapon();
                armorArray[i] = new Armor();
            }
        }
        public Weapon[] getWeaponArray() {
            return weaponArray;
        }
        public void setWeaponArray(int yourWeaponIndex, Weapon enemyWeapon) {
            this.weaponArray[yourWeaponIndex] = enemyWeapon;
        }
        public Armor[] getArmorArray() {
            return armorArray;
        }
        public void setArmorArray(Armor[] armorArray) {
            this.armorArray = armorArray;
        }
        public int getMoney() {
            return money;
        }
        public void setMoney(int money) {
            this.money = money;
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您正在调用访问器方法,就像它也会设置一样,但没有设置。尝试这样的事情:

from IPython.display import display, Math
from sympy.abc import *
from sympy import *

def imaginary_square_root(x, y):
    return(sqrt((x + sqrt(x**2 + y**2)) / (2)) + I*((y*sqrt(2)) / (2*sqrt(x + sqrt(x**2 + y**2))))) # calculates the square root of a complex number

def find_imaginary_square_root(polynomial): # 'polynomial' used because this function is meant to change expressions including variables such as 'x'
    polynomial = str(polynomial).replace('.0 ', ' ')
    exec(f'polynomial = {polynomial}')

    list_of_square_roots = [] # list of string instances of square roots and their contents
    list_of_square_root_indexes = [] # list of indexes at which the square roots can be found in the string
    polynomial_string = str(polynomial)
    temp_polynomial_string = polynomial_string # string used and chopped up, hence the prefix 'temp_...'
    current_count = 0 # counter variable used for two seperate jobs

    while 'sqrt' in temp_polynomial_string: # gets indexes of every instance of 'sqrt'
        list_of_square_root_indexes.append(temp_polynomial_string.index('sqrt') + current_count)
        temp_polynomial_string = temp_polynomial_string[list_of_square_root_indexes[-1] + 4:]
        current_count += list_of_square_root_indexes[-1] + 4

    for square_root_location in list_of_square_root_indexes:
        current_count = 1 # second job for 'current_count'

        for index, char in enumerate(polynomial_string[square_root_location + 5:]):
            if char == '(':
                current_count += 1

            elif char == ')':
                current_count -= 1

            if not current_count: # when current_count == 0, we know that the end of the sqrt contents have been reached
                list_of_square_roots.append(polynomial_string[square_root_location:square_root_location + index + 6]) # adds the square root with contents to a list
                break

    for individual_square_root in list_of_square_roots:
        if individual_square_root in str(polynomial):
            evaluate = individual_square_root[5:-1]
            x = re(evaluate)
            y = im(evaluate)

            polynomial = polynomial.replace(eval(individual_square_root), imaginary_square_root(x, y)) # replace function used here is Sympy's replace function for polynomials

    return polynomial

poly = str(-sqrt(-2.0 + 2*sqrt(3)*I))

display(Math(latex(find_imaginary_square_root(poly))))