用Java创建数字战斗系统后端

时间:2016-02-07 01:03:19

标签: java oop

我一直试图调试这个我一直试图实现的简单战斗系统。我处于严重的基础级别并且已经制作了一个调试主文件来找出问题的根源。 我创造的主要战斗功能:

package great;
import great.Player;
import java.util.Scanner;

public class battleFunc {

    public static void start(Player pLayer, Player Enemy){
        while(pLayer.getHealth() > 0 || Enemy.getHealth() > 0){
            String result = askPlayer();

            if(result.equals("Defend")){
                playerDefend(pLayer, Enemy);
            }

            if(result.equals("Hit")){
                playerHit(pLayer, Enemy);
            }

            EnemyHit(pLayer, Enemy);
        }
    }

    public static String askPlayer(){
        String x;
        Scanner scanner = new Scanner(System.in);

        System.out.println("What will you do?");
        x = scanner.nextLine();

        return x;
    }

    public static void reportEnemyHealth(Player Enemy){
        System.out.println(Enemy.getName() + "'s health is: " + Enemy.getHealth());
    }

    public static void reportPlayerHealth(Player pLayer){
        System.out.println(pLayer.getName() + "'s health is: " + pLayer.getHealth());
    }

    public static int reportPlayerHit(Player pLayer, Player Enemy){
        int x = pLayer.getStrength() - Enemy.getDefense();
        return x;
    }

    public static void reportEnemyHit(Player pLayer, Player Enemy){
        System.out.println(Enemy.getName() + " hit for " + (Enemy.getStrength() - pLayer.getDefense()));
    }

    public static void playerDefend(Player pLayer, Player Enemy){
        int x = Enemy.getStrength() - (pLayer.getDefense() + 1);

        if (x <= 0) {x = 0;}

        pLayer.setHealth(pLayer.getHealth() - x);
        reportEnemyHit(pLayer, Enemy);
    }

    public static void playerHit(Player pLayer, Player Enemy){
        int x = pLayer.getStrength() - Enemy.getDefense();

        if (x <= 0){x = 0;}

        Enemy.setHealth(4);
        reportPlayerHit(pLayer, Enemy);
    }

    public static void EnemyHit(Player pLayer, Player Enemy){
        int x = Enemy.getStrength() - pLayer.getDefense();

        if (x <= 0){x = 0;}

        pLayer.setHealth(pLayer.getHealth() - x);
        reportEnemyHit(pLayer, Enemy);
    }
}

用于玩家和敌人的玩家类:

package great;

public class Player {
    private int health;
    private String name;
    private int lvl;
    private int defense;
    private int strength;

    public Player(String x){
        name = x;
        lvl = 1;
    }

    public int getHealth(){
        return health;
    }   

    public void setHealth(int x){
        health = x;
    }

    public String getName(){
        return name;
    }   

    public void setName(String x){
        x = name;
    }

    public int getLVL(){
        return lvl;
    }   

    public void setLVL(int x){
        x = lvl;
    }

    public int getDefense(){
        return defense;
    }

    public void setDefense(int x){
        x = defense;
    }

    public int getStrength(){
        return strength;
    }

    public void setStrength(int x){
        x = strength;
    }
}

我用来测试代码的主文件:

package great;
import great.Player;
import great.battleFunc;

public class MainG {

    public static void main(String[] args) {
        Player guy = new Player("Deez Nuts");
        guy.setHealth(20);
        guy.setStrength(7);
        guy.setDefense(2);
        Player fuckboi = new Player("Fuckboi");
        fuckboi.setHealth(10);
        fuckboi.setStrength(3);
        fuckboi.setDefense(1);

        battleFunc.start(guy, fuckboi);
    }
}

运行主文件时,所有攻击值都返回并打印为0,并且不会删除运行状况。这是为什么?

1 个答案:

答案 0 :(得分:0)

此方法:

public void setDefense(int x){
    x = defense;
}

不将玩家的防守设置为x,而是将x设置为玩家的防守。只需将xdefense交换,即可正确更新这些值。同样适用于setStrength(...)。数学返回0的原因很简单,两位球员的实力和防守都是0。