Java 中的决策游戏

问题描述 投票:0回答:1

我需要 Java 方面的学校项目帮助。该项目包括创建一个决策游戏,玩家将面临多个困境(节点),这将影响故事和下一个困境。我知道我必须使用节点图/树来构建游戏,但我需要有关如何组织代码和游戏结构的更深入的帮助(例如,如何转到下一个节点,或者我必须为每个新节点创建一个新类?...)。我是初学者。我在 eclipse 上工作。

下面是我开始做的。

package representation;

public class Node {
private int id;
private String description;

    public Node(int id, String description) {
        this.id = id;
        this.description = description;
    }

    public void display() {
        System.out.println(description);
    }

    public Node chooseNext() {
        // This method will be redefined in subclasses such as `DecisionNode`..
        return null; // By default, returns null.
    }

    public int getId() {
        return id;
    }

}

`

java nodes project decision-tree
1个回答
0
投票

我认为你的项目需要更多细节和结构。但这只是我对这个游戏架构的看法。我会选择

public class Story
来包含故事的后续步骤。事实上,您可以更轻松地在图表或二叉树中操纵故事来进行困境和决策游戏。

你可以用任何你想要的方式实现它,但这是我制作的原型:

public class Story {
    private String title;
    private String content;

    public Story(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }
}

当然,如果游戏中有图形、声音、电影或与特定故事或时间戳相关的任何内容,您可以在这里实现。

public class DecisionNode extends Node {
    private Node yesNode;
    private Node noNode;
    private Story story;

    public DecisionNode(int id, String description, Node yesNode, Node noNode, Story story) {
        super(id, description);
        this.yesNode = yesNode;
        this.noNode = noNode;
        this.story = story;
    }

    @Override
    public Node chooseNext(boolean choice) {
        if (choice) {
            return yesNode;
        } else {
            return noNode;
        }
    }

    public Story getStory() {
        return story;
    }
}

public class OutcomeNode extends Node {
    public OutcomeNode(int id, String description) {
        super(id, description);
    }
}

我认为这就是我实现它的方式,当然,修改它以使其最适合您的游戏目的。

© www.soinside.com 2019 - 2024. All rights reserved.