我正在尝试制作一个简单的幼稚文字冒险游戏(基于此页面为基础)来学习 OCaml。
该游戏是关于制作游戏引擎的,因此有关房间、物品等的所有信息都存储在一个 json 文件中。
示例 json 文件如下所示:
{
"rooms":
[
{
"id": "room1",
"description": "This is Room 1. There is an exit to the north.\nYou should drop the white hat here.",
"items": ["black hat"],
"points": 10,
"exits": [
{
"direction": "north",
"room": "room2"
}
],
"treasure": ["white hat"]
},
{
"id": "room2",
"description": "This is Room 2. There is an exit to the south.\nYou should drop the black hat here.",
"items": [],
"points": 10,
"exits": [
{
"direction": "south",
"room": "room1"
}
],
"treasure": ["black hat"]
}
],
"start_room": "room1",
"items":
[
{
"id": "black hat",
"description": "A black fedora",
"points": 100
},
{
"id": "white hat",
"description": "A white panama",
"points": 100
}
],
"start_items": ["white hat"]
}
我几乎完成了游戏,但在项目描述页面上,它说两个目标是
- 设计用户定义的数据类型,尤其是记录和变体。
- 编写在列表和树上使用模式匹配和高阶函数的代码。
但是,我制作的唯一用户定义数据类型是用于捕获游戏当前状态的记录类型,我没有使用 tree 和 variant :
type state = {
current_inventory : string list ;
current_room : string ;
current_score : int ;
current_turn : int ;
}
然后只需解析用户输入并使用模式匹配来处理不同的情况。
我一直在试图弄清楚我应该如何使用变体(或多态变体)和树在我的游戏中
任何人都可以提供一些建议吗?