Advertisement
xplosiveloons

a2

Feb 25th, 2022
1,780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.10 KB | None | 0 0
  1. (* Note: You may introduce new code anywhere in this file. *)
  2. open Yojson.Basic.Util
  3. exception UnknownRoom of string
  4. exception UnknownExit of string
  5.  
  6. (* TODO: replace [unit] with a type of your own design. *)
  7. type exit = {
  8.   name: string;
  9.   room_id: string
  10. }
  11. type room = {
  12.   id: string;
  13.   description: string;
  14.   exits: exit list;
  15.   }
  16.  
  17. type t = {
  18.   rooms: Yojson.Basic.t;
  19.   start_room: string
  20.   }
  21.  
  22.  
  23. (*[from_json j] is the adventure that j represents. Requires: j is a valid JSON
  24. adventure representation.*)
  25. let from_json json =
  26.   let rooms = json |> member "rooms" in
  27.   let start_room = json |> member "start room" |> to_string in
  28.   {
  29.     rooms = rooms;
  30.     start_room = start_room;
  31.   }
  32.  
  33. (*[start_room a] is the identifier of the starting room in adventure a.
  34. *)
  35.   let start_room adv = adv.start_room
  36.  
  37.  
  38. (*[room_ids a] is a set-like list of all of the room identifiers in adventure a.
  39. *)
  40. let rec room_ids adv =
  41.   let new_lst = to_list adv.rooms in
  42.   let room_lst = [] in
  43.   let rec roomify new_list =
  44.   match new_lst with  
  45.     |[] -> []
  46.     |[h] -> (h|>member "id" |> to_string )::room_lst
  47.     |h::t -> (h|>member "id" |> to_string )::(roomify t)
  48.   in
  49.   let lst = roomify new_lst in
  50.   List.sort_uniq compare lst
  51.  
  52. (*[description a r] is the description of room r in adventure a.
  53.   Raises UnknownRoom r if r is not a room identifier in a.
  54. *)
  55. let description adv room =
  56.   let new_lst = to_list adv.rooms in
  57.   let rec search_describe new_list =
  58.   match new_lst with  
  59.   |[] -> raise (UnknownRoom room)
  60.   |h::t -> if (h|>member "id" |> to_string)=room
  61.     then (h|>member "description" |> to_string)
  62.     else search_describe t
  63.   in
  64.   search_describe new_lst  
  65.  
  66. (*[exits a r] is a set-like list of all exit names from room r in adventure a.
  67. Raises UnknownRoom r if r is not a room identifier in a.
  68. *)
  69. let exits adv room =
  70. let new_lst = to_list adv.rooms in
  71. let rec find_exits new_list =
  72. match new_lst with  
  73. |[] -> raise (UnknownRoom room)
  74. |h::t -> if (h|>member "id" |> to_string)=room
  75.   then let exits = (h|>member "exits") in
  76.   (exits|>member "name"|> to_string)
  77.   else find_exits t
  78. in
  79. find_exits new_lst    
  80.  
  81. (*[next_room a r e] is the room to which e exits from room r in adventure a.
  82. Raises UnknownRoom r if r is not a room identifier in a.
  83. Raises UnknownExit e if e is not an exit from room r in a.
  84. *)
  85. let next_room adv room ex =
  86. let new_lst = to_list adv.rooms in
  87. let rec find_room new_list =
  88. match new_list with  
  89. |[] -> raise (UnknownRoom room)
  90. |h::t -> (if (h|>member "id" |> to_string)=room
  91.   then (h|>member "room id")
  92.   else (find_room t))
  93. in
  94. let room = find_room new_lst in
  95. let exit_lst = (room|> member "exits"|>to_list) in
  96. let rec find_exit exits =
  97. match exits with
  98.   |[] -> raise (UnknownExit ex)
  99.   |h::t -> if (h|>member "name"|>to_string = ex)
  100.     then (h|>member "room id"|>to_string)
  101.     else (find_exit t)
  102. in
  103. (find_exit exit_lst);
  104.  
  105.  
  106.   (*[next_rooms a r] is a set-like list of all rooms to which there is an exit from r in
  107.   adventure a. Raises UnknownRoom r if r is not a room identifier in a.*)
  108. let next_rooms adv room =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement