首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫雷资源计算器

扫雷资源计算器
EN

Code Review用户
提问于 2018-08-12 17:50:57
回答 1查看 743关注 0票数 1

我正在编写一个基本程序,计算在“我的世界”中制作物品所需的总基本资源。它的目的是模块化的支持修正版本的“我的世界”(新的项目或不同的食谱)。

首先,我创建了一个作为基本资源的Item类。基础资源是指我已决定在“我的世界”(如木板或煤)上可收获的东西,或者是容易制造且不值得自己制作的东西(如铁锭,通过冶炼铁矿石容易获得)。

代码语言:javascript
复制
public class Item {

String name;
int count;
boolean isPopulated;
boolean isItem;

public Item() {
    this.name = "";
    this.count = 1;
    this.isPopulated = false;
    this.isItem = true;
}

public Item(String name) {
    this.name = name;
    this.count = 1;
    this.isPopulated = false;
    this.isItem = true;
}

public Item(int count) {
    this.name = "";
    this.count = count;
    this.isPopulated = false;
    this.isItem = true;
}

public Item(int count, String name) {
    this.name = name;
    this.count = count;
    this.isPopulated = false;
    this.isItem = true;
}

public String getName() {
    return this.name;
}

public int getCount() {
    return this.count;
}

public void setName(String name) {
    this.name = name;
}

public void setcount(int count) {
    this.count = count;
}
}

下面是一个Item类的示例:

代码语言:javascript
复制
public class IronIngot extends Item{
    public IronIngot(int count) {
        super(count);
        this.name = "Iron Ingot";
    }
}

接下来是棘手的部分(至少对我来说是这样):我创建了一个菜谱类,其中包含用于制作可制作项的项目(或其他菜谱)的列表。我能够使用某种形式的递归来解释那些其食谱中包含可制作项目的可手工项目。

代码语言:javascript
复制
public class Recipe extends Item {

    List<Item> components;
    List<Item> materials;

    public Recipe() {
        this.components = new ArrayList<>();
        this.materials = new ArrayList<>();
        this.isItem = false;
    }

    public Recipe(int count) {
        super(count);
        this.components = new ArrayList<>();
        this.materials = new ArrayList<>();
        this.isItem = false;
    }

    public void printRecipe() {

        System.out.println("Recipe for " + this.name + ":");
        this.components.forEach((item) -> {
            System.out.println("\t" + item.name + ": " + item.count);
        });
        System.out.println();
    }

    public void printMaterials() {
        populateMaterials();
        System.out.println("Materials needed for " + this.name + ":");
        this.materials.forEach((item) -> {
            System.out.println("\t" + item.name + ": " + item.count);
        });
        System.out.println();
    }

    public void populateMaterials() {
        this.components.forEach((item) -> {
            if (!item.isPopulated) {
                if (item.isItem) {
                    this.addMaterial(item);
                } else {
                    Recipe tempRecipe = (Recipe) item;
                }
            }
        });
    }

    public void addMaterial(Item item) {
        this.materials.forEach((material) -> {
            if (material.name.equals(item.name)) {
                item.isPopulated = true;
                material.count += item.count;

            }
        });
        if (!item.isPopulated) {
            item.isPopulated = true;
            this.materials.add(item);
        }
    }

    public void addMaterials(List<Item> materials) {
        materials.forEach((material) -> {

            if (!material.isItem) {
                Recipe tempRecipe = (Recipe) material;
                this.addMaterials(tempRecipe.getMaterials());
            } else {
                this.addMaterial((Item) material);
            }
        });
    }

    public List getItems() {
        return this.components;
    }

    public List getMaterials() {
        populateMaterials();
        this.materials.forEach((material) -> {
            material.isPopulated = false;
        });
        return this.materials;
    }

}

下面是一个菜谱类的示例:

代码语言:javascript
复制
public class CopperCoilBlock extends Recipe{
    public CopperCoilBlock(){
        this.name = "Copper Coil Block";
        this.components.add(new LVWireCoil(8));
        this.components.add(new IronIngot(1));
    }

    public CopperCoilBlock(int count){
        super(count);
        this.name = "Copper Coil Block";
        this.components.add(new LVWireCoil(8));
        this.components.add(new IronIngot(1));
    }
}

我的问题是:如何核算制作食谱的资源成本,从而产生多个项目?例如,要使一个电源单元(低),我需要一个铜线圈块。为了制造一个铜线圈块,我需要8个LV钢丝圈和1个铁锭。低压线圈的配方是4根铜线和1根棒产生4个LV线圈。这意味着,要制作一个铜线圈块,我需要8根铜线和2根棍子。我最初的想法是使用每个项目中的计数整数来解决这个问题,但是我不知道如何实现它,或者这是否是最好的过程。下面是LVWireCoil类的样子:

代码语言:javascript
复制
public class LVWireCoil extends Recipe {
    public LVWireCoil(int count) {
        super(4);
        this.name = "LV Wire Coil";
        this.components.add(new CopperWire(4));
        this.components.add(new Sticks(1));

    }
}

此外,我有相当多的与java学习,所以任何建设性的批评(代码约定,逻辑等)将是欢迎的!

EN

回答 1

Code Review用户

发布于 2018-08-13 18:28:35

一般推荐

  1. 更喜欢没有add()set()等的不可变类。它会导致简单的程序而没有错误。
  2. 避免继承。老实说,我觉得只有少数几个合适的案例。
  3. 使用流。它们有助于执行懒惰的遍历或许多收集操作。
  4. 我也是Kotlin语言的忠实粉丝,所以我建议大家试一试(但这与主题无关)。

这是我的原型:

代码语言:javascript
复制
public class Temp {

    public static void main(String[] args){
        Item log = new Item("log", Collections.emptyList());
        Item wood = new Item("wood", Arrays.asList(new Component(log, 1f/4f)));
        Item stick = new Item("stick", Arrays.asList(new Component(wood, 1f/4f)));
        Item stone = new Item("stone", Collections.emptyList());
        Item axe = new Item("axe", Arrays.asList(new Component(stick, 2f), new Component(stone, 3f)));

        List<Component> axeComponents = getSimpleComponentsOf(new Component(axe, 1f)).collect(Collectors.toList());

        System.out.println(StringUtils.join(axeComponents.stream().map(Component::toString).collect(Collectors.toList()), ", "));
    }


    public static Stream<Component> getSimpleComponentsOf(Component component){
        if (component.item.components.isEmpty()) return Stream.of(component);
        return component.item.components.stream()
                .map(it -> new Component(it.item, it.number * component.number))
                .flatMap(it -> getSimpleComponentsOf(it));
    }

    static class Item {
        public final String name;
        public final List<Component> components;

        Item(String name, List<Component> components) {
            this.name = name;
            this.components = components;
        }
    }

    static class Component {

        public final Item item;
        public final float number;

        Component(Item item, float number){
            this.item = item;
            this.number = number;
        }

        @Override
        public String toString() {
            return "Component(item = " + item.name + ", number = " + number + ")";
        }
    }
}

输出:

代码语言:javascript
复制
Component(item = log, number = 0.125), Component(item = stone, number = 3.0)
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/201525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档