Java 基于文本的游戏 - 如何实现 class 选择功能?

Java text based game - How to implement class choosing feature?

我正在尝试在 java 中制作角色扮演游戏。 我是 java 的新人,所以我不是很了解所有事情。 我需要帮助来实现 class 选择功能。

这是我的代码:

package com.rpg.entities;

import java.util.Scanner;

public class Character extends Entity {

    private int EXP; // experience points
    private int gold; // player money
    private double critChance; // critical chance
    private int strength; // attack damage
    private int defense; // reduce taken damage
    private int dexterity; // accuracy only for bow and spells
    private int intelligence; // spell damage
    private int evasion; // dodge attacks
    // armor parts
    private String helmet;
    private String chest;
    private String boots;

    public void setEXP(int exp) {
        this.EXP = exp;
    }

    public int getEXP() {
        return EXP;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    public int getGold() {
        return gold;
    }

    public void setCritChance(int crit) {
        this.critChance = crit;
    }

    public double getCritChance() {
        return critChance;
    }

    public void setStrength(int str) {
        this.strength = str;
    }

    public int getStrength() {
        return strength;
    }

    public void setDefense(int def) {
        this.defense = def;
    }

    public int getDefense() {
        return defense;
    }

    public void setDexterity(int dex) {
        this.dexterity = dex;
    }

    public int getDexterity() {
        return dexterity;
    }

    public void setIntelligence(int inte) {
        this.intelligence = inte;
    }

    public int getIntelligence() {
        return intelligence;
    }

    public void setEvasion(int eva) {
        this.evasion = eva;
    }

    public int getEvasion() {
        return evasion;
    }

    public void checkLevel(int playerExp, int playerLevel, Character a) {

        int[] levelArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2200,
                2400, 2600, 2800, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000 }; // 30

        if (playerExp >= levelArray[playerLevel]) {
            // level up player
            if (playerExp >= levelArray[playerLevel + 1]) {
                a.setEXP(levelArray[playerLevel]);
            } else {
                System.out.println("Level up!");
                a.setLevel(playerLevel + 1);
            }
        }

    }

    public String chooseClass() {

        return "";
    }

}

我想写在角色class中选择玩家class的方法,然后 在 main 函数中调用它,但我不确定这是否是好的做法。并且, 我不知道它应该 return 字符串吗?

换句话说,class 角色应该有方法 chooseClass() 并且你可以用它来选择 classes,例如:

If(name == "warrior") {
    Warrior player = new Warrior();
} else if (name == "mage") {
    Mage player = new Mage();
} else
  System.out.println("Invalid class");

然后 returns player 到 main class 所以我可以使用那个变量 用于执行以下操作:player.setHealth() 等..

更多class是:

package com.rpg.classes;

import com.rpg.entities.Character;
public class Archer extends Character {

    public Archer() {

        this.setMaxHP(100);
        this.setHP(100);
        this.setMaxMP(100);
        this.setMP(100);
        this.setLevel(1);
        this.setDamage(10); // 
        this.setGold(0);
        this.setCritChance(0);  
        this.setStrength(0);
        this.setDefense(0);
        this.setDexterity(10);
        this.setIntelligence(0);
        this.setEvasion(0);
        this.setEXP(100);
    }

}

package com.rpg.classes;

import com.rpg.entities.Character;

public class Mage extends Character {

    public Mage() {

        this.setMaxHP(100);
        this.setHP(100);
        this.setMaxMP(100);
        this.setMP(100);
        this.setLevel(1);
        this.setDamage(10); // popravi posle
        this.setGold(0);
        this.setCritChance(0); // dodaj mehanizam
        this.setStrength(0);
        this.setDefense(0);
        this.setDexterity(0);
        this.setIntelligence(10);
        this.setEvasion(0);
        this.setEXP(100);

    }

}

不确定您需要什么。但是在您的 main 方法中,您可以通过某种方式从用户那里检索所需的 char 类型。为此,例如。我假设法师。所以如果它是一个法师会初始化法师等

    Character character;
    String char_type = "Mage";
    switch(char_type){    
        case "Mage":    
            character = new Mage();
            break;
        case "Archer":    
            character = new Archer();   
            break;
   }

希望对您有所帮助或给您一些想法。

已编辑:出于某种原因我未能阅读您的所有描述

I want to write method for choosing player class in Character class,then to call it in main function, but i am not sure if that is good practice. And also, i don't know should it return String?

In other words, class Character should have method chooseClass() and with it you can choose classes, for example:

If(name == "warrior") {
    Warrior player = new Warrior();
} else if (name == "mage") {
    Mage player = new Mage();
} else
  System.out.println("Invalid class");

And then returns player to main class so i can use that variable for doing stuffs like: player.setHealth(), etc..

//public Static Character chooseClass() {
public Character chooseClass() { //you might need to make this method static. not too sure
    String char_type = "Mage"; //you would get the player they want using Scanner i think it is.
    switch(char_type){    
        case "Mage":    
            return new Mage();
        case "Archer":    
            return new Archer();
        default:
            System.out.println("Error that play doesnt exist!")
   }
    return null;
}

然后在你的 main 方法中你会得到这样的东西。

public static void main(String[] args){
    Character player = Character.chooseClass();
    player.setHealth(100);
}

对我来说,所有角色 class 都具有相同的属性和相同的行为,但初始值不同。

我会考虑一个代表 class 的枚举和一个由 class 创建字符的工厂。工厂可以从单例映射中克隆一些原型,从 JSON 等资源中反序列化,或者使用一些自定义代码简单地进行初始化。

自定义代码初始化通常会创建角色,设置等级和金币等通用属性,并可能通过 class 覆盖开关中的特殊属性。

我知道这不是最佳 OO 实践,但它会保持代码简单并在工厂中隐藏 "ugly"。如果您想在以后更改更多属性时进行更复杂的升级,它会保持灵活性。

您应该将 class 枚举属性设置为最终属性,并考虑将某些设置器设置为非 public,尤其是对于 class 特定属性,以确保一致的字符实例。

想想把属性变更责任放在哪里。在角色内部或外部,也许是从上方的工厂。由于你的角色目前看起来更像一个 POJO,我会避免在混合类型中加入太多逻辑。

由于我不知道你的游戏目标愿景,所以我只能提供一些想法和想法。没有办法,看情况。