Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Note: You may introduce new code anywhere in this file. *)
- open Yojson.Basic.Util
- exception UnknownRoom of string
- exception UnknownExit of string
- (* TODO: replace [unit] with a type of your own design. *)
- type exit = {
- name: string;
- room_id: string
- }
- type room = {
- id: string;
- description: string;
- exits: exit list;
- }
- type t = {
- rooms: Yojson.Basic.t;
- start_room: string
- }
- (*[from_json j] is the adventure that j represents. Requires: j is a valid JSON
- adventure representation.*)
- let from_json json =
- let rooms = json |> member "rooms" in
- let start_room = json |> member "start room" |> to_string in
- {
- rooms = rooms;
- start_room = start_room;
- }
- (*[start_room a] is the identifier of the starting room in adventure a.
- *)
- let start_room adv = adv.start_room
- (*[room_ids a] is a set-like list of all of the room identifiers in adventure a.
- *)
- let rec room_ids adv =
- let new_lst = to_list adv.rooms in
- let room_lst = [] in
- let rec roomify new_list =
- match new_lst with
- |[] -> []
- |[h] -> (h|>member "id" |> to_string )::room_lst
- |h::t -> (h|>member "id" |> to_string )::(roomify t)
- in
- let lst = roomify new_lst in
- List.sort_uniq compare lst
- (*[description a r] is the description of room r in adventure a.
- Raises UnknownRoom r if r is not a room identifier in a.
- *)
- let description adv room =
- let new_lst = to_list adv.rooms in
- let rec search_describe new_list =
- match new_lst with
- |[] -> raise (UnknownRoom room)
- |h::t -> if (h|>member "id" |> to_string)=room
- then (h|>member "description" |> to_string)
- else search_describe t
- in
- search_describe new_lst
- (*[exits a r] is a set-like list of all exit names from room r in adventure a.
- Raises UnknownRoom r if r is not a room identifier in a.
- *)
- let exits adv room =
- let new_lst = to_list adv.rooms in
- let rec find_exits new_list =
- match new_lst with
- |[] -> raise (UnknownRoom room)
- |h::t -> if (h|>member "id" |> to_string)=room
- then let exits = (h|>member "exits") in
- (exits|>member "name"|> to_string)
- else find_exits t
- in
- find_exits new_lst
- (*[next_room a r e] is the room to which e exits from room r in adventure a.
- Raises UnknownRoom r if r is not a room identifier in a.
- Raises UnknownExit e if e is not an exit from room r in a.
- *)
- let next_room adv room ex =
- let new_lst = to_list adv.rooms in
- let rec find_room new_list =
- match new_list with
- |[] -> raise (UnknownRoom room)
- |h::t -> (if (h|>member "id" |> to_string)=room
- then (h|>member "room id")
- else (find_room t))
- in
- let room = find_room new_lst in
- let exit_lst = (room|> member "exits"|>to_list) in
- let rec find_exit exits =
- match exits with
- |[] -> raise (UnknownExit ex)
- |h::t -> if (h|>member "name"|>to_string = ex)
- then (h|>member "room id"|>to_string)
- else (find_exit t)
- in
- (find_exit exit_lst);
- (*[next_rooms a r] is a set-like list of all rooms to which there is an exit from r in
- adventure a. Raises UnknownRoom r if r is not a room identifier in a.*)
- let next_rooms adv room =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement