Advertisement
Guest User

Untitled

a guest
May 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. record Adventure =
  2. inventory : List-Set Item,
  3. current-place-name : String,
  4. places : List-Dict String Place,
  5. combinations : List Combination,
  6.  
  7. func adventure : String -> List (Pair String Place) -> List Combination -> Adventure =
  8. \current-place-name \places \combinations
  9. Adventure
  10. (list-set (==) [])
  11. current-place-name
  12. (list-dict (==) invalid-place places)
  13. combinations
  14.  
  15. func current-place : Adventure -> Place =
  16. \adv
  17. (at (current-place-name adv) . places) adv
  18.  
  19. func current-place : (Place -> Place) -> Adventure -> Adventure =
  20. \f \adv
  21. (places . at (current-place-name adv)) f adv
  22.  
  23. func show-inventory : Adventure -> String =
  24. join " " . map name . values . inventory
  25.  
  26. record Place =
  27. description : String,
  28. items : List-Set Item,
  29. objects : List-Set Object,
  30. directions : List-Dict Direction String,
  31.  
  32. func place : String -> List Item -> List Object -> List (Pair Direction String) -> Place =
  33. \description \items \objects \directions
  34. Place
  35. description
  36. (list-set (==) items)
  37. (list-set (==) objects)
  38. (list-dict (==) "" directions)
  39.  
  40. func invalid-place : Place = place "Invalid place. Reaching this place is a bug." [] [] []
  41.  
  42. record Direction =
  43. name : String,
  44. description : String,
  45.  
  46. func dir : String -> String -> Description = Direction
  47.  
  48. func == : Direction -> Direction -> Bool =
  49. \dir1 \dir2
  50. name dir1 == name dir2
  51.  
  52. func != : Direction -> Direction -> Bool =
  53. not (==)
  54.  
  55. func full-description : Place -> String =
  56. \place
  57. yield-all (description place);
  58. for (values; items place)
  59. (yield-all . (" " ++) . short-description);
  60. for (values; objects place)
  61. (yield-all . (" " ++) . short-description);
  62. for (keys; directions place)
  63. (yield-all . (" " ++) . description);
  64. empty
  65.  
  66. record Item =
  67. name : String,
  68. short-description : String,
  69. long-description : String,
  70.  
  71. func item : String -> String -> String -> Item = Item
  72.  
  73. func == : Item -> Item -> Bool =
  74. \item1 \item2
  75. name item1 == name item2
  76.  
  77. func != : Item -> Item -> Bool =
  78. not (==)
  79.  
  80. record Object =
  81. name : String,
  82. short-description : String,
  83. long-description : String,
  84. actions : List Action,
  85.  
  86. func object : String -> String -> String -> List Action -> Object = Object
  87.  
  88. func == : Object -> Object -> Bool =
  89. \object1 \object2
  90. name object1 == name object2
  91.  
  92. func != : Object -> Object -> Bool =
  93. not (==)
  94.  
  95. record Action =
  96. name : String,
  97. description : String,
  98. items : List-Set Item,
  99. outcome : Maybe (Adventure -> Adventure),
  100.  
  101. func action : String -> String -> List Item -> Maybe (Adventure -> Adventure) -> Action =
  102. \name \description \items \outcome
  103. Action
  104. name
  105. description
  106. (list-set (==) items)
  107. outcome
  108.  
  109. record Combination =
  110. description : String,
  111. use-items : List-Set Item,
  112. new-items : List-Set Item,
  113.  
  114. func combination : String -> List Item -> List Item -> Combination =
  115. \description \use-items \new-items
  116. Combination
  117. description
  118. (list-set (==) use-items)
  119. (list-set (==) new-items)
  120.  
  121. func pick : Item -> Adventure -> Adventure =
  122. \item \adv
  123. start-with adv;
  124. (current-place . items) <- remove item;
  125. inventory <- add item;
  126. return self
  127.  
  128. func use : Object -> Action -> Adventure -> Adventure =
  129. \object \action \adv
  130. start-with adv;
  131. inventory <- - items action;
  132. if-none (return self);
  133. let-some (outcome action) \outcome
  134. (current-place . objects) <- remove object;
  135. self <- outcome;
  136. return self
  137.  
  138. func combine : Combination -> Adventure -> Adventure =
  139. \combo \adv
  140. start-with adv;
  141. inventory <- - use-items combo;
  142. inventory <- + new-items combo;
  143. return self
  144.  
  145. func go-to : String -> Adventure -> Adventure =
  146. \new-place-name \adv
  147. start-with adv;
  148. current-place-name := new-place-name;
  149. return self
  150.  
  151. func special-words : Adventure -> List-Set String =
  152. \adv
  153. list-set (==);
  154. yield-all ["end", "inventory", "look", "pick"];
  155. for (values; inventory adv)
  156. (yield . name);
  157. for (values; items; current-place adv)
  158. (yield . name);
  159. for (values; objects; current-place adv)
  160. (yield . name);
  161. for (values; objects; current-place adv)
  162. (yield-all . map name . actions);
  163. for (map name; keys; directions; current-place adv)
  164. yield;
  165. empty
  166.  
  167. func play : Adventure -> IO =
  168. \adv
  169. println "Welcome to the adventure! Are you ready?";
  170. println "If not, type 'end' to quit the game.";
  171. println (description; decode-command adv; parse-words adv "look around");
  172. adv |> recur \loop \adv
  173. print "> ";
  174. scanln \input
  175. let (parse-words adv input) \words
  176. if (empty? words)
  177. (loop adv);
  178. if (words == list-set (==) ["end"])
  179. quit;
  180. let (decode-command adv words) \command
  181. println (description command);
  182. loop (change command adv)
  183.  
  184. record Command =
  185. description : String,
  186. change : Adventure -> Adventure,
  187.  
  188. func parse-words : Adventure -> String -> List-Set String =
  189. \adv \input
  190. special-words adv & list-set (==) (split-no-empty whitespace? input)
  191.  
  192. func decode-command : Adventure -> List-Set String -> Command =
  193. \adv \words
  194.  
  195. if (words == list-set (==) ["inventory"])
  196. (Command (show-inventory adv) self);
  197.  
  198. if (words == list-set (==) ["look"])
  199. (Command (full-description; current-place adv) self);
  200.  
  201. for (values; inventory adv) (
  202. \item \next
  203. if (words == list-set (==) ["look", name item])
  204. (Command (long-description item) self);
  205. next
  206. );
  207.  
  208. for (values; items; current-place adv) (
  209. \item \next
  210. if (words == list-set (==) ["look", name item])
  211. (Command (long-description item) self);
  212.  
  213. let (list-set (==) ["pick", name item]) \pick-words
  214. if (words == pick-words)
  215. (Command
  216. ("You picked " ++ name item ++ ".")
  217. (pick item));
  218. next
  219. );
  220.  
  221. for (values; objects; current-place adv) (
  222. \object \next
  223. if (words == list-set (==) ["look", name object])
  224. (Command (long-description object) self);
  225.  
  226. for (actions object) (
  227. \action \next
  228. let (map name; values; items action) \required-items
  229. let (list-set (==) (name object :: name action :: required-items)) \use-words
  230. if ((words == use-words) && (items action <= inventory adv))
  231. (Command
  232. (description action)
  233. (use object action));
  234. next
  235. );
  236. next
  237. );
  238.  
  239. for-pair (entries; directions; current-place adv) (
  240. \dir \place-name \next
  241. let (list-set (==) [name dir]) \required-words
  242. if (words == required-words) (
  243. let (at place-name; places adv) \new-place
  244. Command
  245. (full-description new-place)
  246. (go-to place-name)
  247. );
  248. next
  249. );
  250.  
  251. for (combinations adv) (
  252. \combo \next
  253. let (list-set (==) (map name; values; use-items combo)) \combo-words
  254. if ((words == combo-words) && (use-items combo <= inventory adv))
  255. (Command
  256. (description combo)
  257. (combine combo));
  258. next
  259. );
  260.  
  261. Command "Can't do that." self
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement