I am trying to make a simple naive text adventure game (base one this page) to learn OCaml.
The game is about making an game engine, so all the information about rooms, items ect, is store in a json file.
Sample json file would be like this:
{
"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"]
}
I've almost done the game, but on the project description page, it says two of the objectives are
- Design user-defined data types, especially records and variants.
- Write code that uses pattern matching and higher-order functions on lists and on trees.
However, the only user-defined datatype I made is a record type used to capture the current state of the game, I did not use tree and variant :
type state = {
current_inventory : string list ;
current_room : string ;
current_score : int ;
current_turn : int ;
}
then just parse user input and use pattern matching to handle different situations.
I'm been trying to figure out how should I use variant (or polymorphic variant) and tree in my game.
Can anyone please provide some suggestions?