首页 > 解决方案 > 口袋妖怪课程中调整健康的问题

问题描述

我有两个名为 Pokemon.java 和 Move.java 的类,其中包含创建和修改 Pokemon 及其动作的方法。我已经创建了所有必需的方法,但是我在使用攻击方法时遇到了问题,该方法应该在受到攻击时减去对手的生命值。

这是 Pokemon.java 类的代码:

import java.util.ArrayList;
public class Pokemon
{
    // Copy over your code for the Pokemon class here
    // Private constants
    private static final int MAX_HEALTH = 100;
    private static final int MAX_MOVES = 4;
    private String name;
    private int health;
    private int opponentHealth;
    public static int numMovesForPokemon = Move.getNumOfMoves();
    private Move move;
    private static ArrayList<Move> moveListForPokemon = new ArrayList<Move>();
    private String pokemonImage;

    // Write your Pokemon class here
    public Pokemon(String theName, int theHealth)
    {
        name = theName;
        if(theHealth <= MAX_HEALTH)
        {
            health = theHealth;
        }
    }

    public Pokemon(String name, String image)
    {
        this.name = name;
        health = 100;
        pokemonImage = image;
    }

    public Pokemon(String theName)
    {
        name = theName;
    }    

    public void setImage(String image)
    {
        pokemonImage = image;
    }

    public String getImage()
    {
        return pokemonImage;
    }

    public String getName()
    {
        return name;
    }

    public int getHealth()
    {
        return health;
    }

    public boolean hasFainted()
    {
        if(health <= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean canLearnMoreMoves()
    {
        if(numMovesForPokemon < 4)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean learnMove(Move other)
    {
        if(canLearnMoreMoves())
        {
            moveListForPokemon = Move.getList();
            moveListForPokemon.add(other);
            numMovesForPokemon++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void forgetMove(Move other)
    {
            moveListForPokemon.remove(other);
    }

    public static ArrayList<Move> displayList()
    {
        return moveListForPokemon;
    }

    public boolean knowsMove(Move move)
    {
        if(moveListForPokemon.contains(move))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean knowsMove(String moveName)
    {
        if(moveListForPokemon.contains(move.getName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean attack(Pokemon opponent, Move move)
    {
        if(knowsMove(move))
        {
            opponentHealth = opponent.getHealth();
            opponentHealth -= move.getDamage();
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean attack(Pokemon opponent, String moveName)
    {
        if(knowsMove(moveName))
        {
            opponentHealth = opponent.getHealth();
            opponentHealth -= move.getDamage();
            return true;
        }
        else
        {
            return false;
        }
    }    

    public String toString()
    {
        return pokemonImage + "\n" + name + " (Health: " + health + " / " + MAX_HEALTH + ")";
    }
    // Add the methods specified in the exercise description
}

这是 Move.java 类的代码:

import java.util.ArrayList;
public class Move
{
    // Copy over your code for the Move class here
    private static final int MAX_DAMAGE = 25;
    private String name;
    private int damage;
    public static int numMoves;
    private static ArrayList<Move> moveList = new ArrayList<Move>();

    public Move(String theName, int theDamage)
    {
        name = theName;
        if(theDamage <= MAX_DAMAGE)
        {
            damage = theDamage;
        }
    }

    public String getName()
    {
        return name;
    }

    public int getDamage()
    {
        return damage;
    }

    public static int getNumOfMoves()
    {
        return numMoves;
    }

    public static ArrayList<Move> getList()
    {
        return moveList;
    }    

    public String toString()
    {
        return name + " (" + damage + " damage)";
    }    
    // Add an equals method so we can compare Moves against each other

    public boolean equals(Move other)
    {
        if(name.equals(other.getName()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }     
}

最后,这是我测试方法的 PokemonTester.java 的代码:

public class PokemonTester extends ConsoleProgram
{
    private PokemonImages images = new PokemonImages();
    public void run()
    {
        // Test out your Pokemon class here!
        Pokemon p1 = new Pokemon("Charrizard", 100);
        Pokemon p2 = new Pokemon("Pikachu", 100);
        Move m1 = new Move("Flamethrower", 20);
        Move m2 = new Move("Fire Breath", 15);
        p1.learnMove(m1);
        System.out.println(p1.knowsMove(m1));
        System.out.println(p1.knowsMove("Flamethrower"));
        System.out.println(p1.attack(p2, m1));
        System.out.println(p2.getHealth());
    }
}

标签: java

解决方案


我想你的问题是这样的:

opponentHealth = opponent.getHealth();
opponentHealth -= move.getDamage();

这段代码有几个问题:

  • 我建议使用局部变量opponentHealth而不是类级别字段
  • 对手不知道它的生命值被减去了。您必须与他分享这些知识,例如通过引入 setter forhealth然后调用opponent.setHealth(opponentHealth)

推荐阅读