Law281

mud game

Apr 14th, 2016
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #lang racket
  2.  
  3. (require srfi/1)
  4. (require srfi/13)
  5. (require srfi/48)
  6.  
  7.  
  8. (define descriptions '((1 "You are in the castle entrance, you see the toilets to the west and kitchen to the east and castle hallway to the north")
  9. (2 "You are in the castle toilets, you see the entrance to your west. ")
  10. (3 "You are in the castle kitchen, you see the entrance to your east.")
  11. (4 "You are in the castle hallway, you see the entrance to your south and living room to your north")
  12. (5 "You are in the castle living room, you see the castle hallway to your south and dining room to your east")
  13. (6 "You are in the castle dining room, you see the living room to your west and games room to your north")
  14. (7 "You are in the games room, you see the dining room to your south and living room to your west")))
  15. (define look '(((directions) look) ((look) look) ((examine room) look)))
  16. (define quit '(((exit game) quit) ((quit game) quit) ((exit) quit) ((quit) quit)))
  17. (define actions `(,@look ,@quit))
  18.  
  19. ;Decision table actions for entering rooms, allows user to choose between which direction you can move in and back
  20. (define decisiontable `((1 ((north) 4) ((east) 2) ((west) 3) ,@actions)
  21. (2 ((west) 1) ,@actions)
  22. (3 ((east) 1),@actions)
  23. (4 ((north) 5) ((south) 1),@actions)
  24. (5 ((south) 4) ((east) 6),@actions)
  25. (6 ((west) 5) ((north) 7),@actions)
  26. (7 ((south) 6) ((west) 5),@actions)))
  27.  
  28.  
  29.  
  30. (define (slist->string l)
  31. (string-join (map symbol->string l)))
  32.  
  33. (define (get-directions id)
  34. (let ((record (assq id decisiontable)))
  35. (let* ((result (filter (lambda (n) (number? (second n))) (cdr record)))
  36. (n (length result)))
  37. (cond ((= 0 n)
  38. (printf "You appear to have entered a room with no exits.\n"))
  39. ((= 1 n)
  40. (printf "You can see an exit to the ~a.\n" (slist->string (caar result))))
  41. (else
  42. (let* ((losym (map (lambda (x) (car x)) result))
  43. (lostr (map (lambda (x) (slist->string x)) losym)))
  44. (printf "You can see exits to the ~a.\n" (string-join lostr " and "))))))))
  45.  
  46. (define (assq-ref assqlist id)
  47. (cdr (assq id assqlist)))
  48.  
  49. (define (assv-ref assqlist id)
  50. (cdr (assv id assqlist)))
  51.  
  52. (define (get-response id)
  53. (car (assq-ref descriptions id)))
  54.  
  55. (define (get-keywords id)
  56. (let ((keys (assq-ref decisiontable id)))
  57. (map (lambda (key) (car key)) keys)))
  58.  
  59.  
  60. ;; outputs a list in the form: (0 0 0 2 0 0)
  61. (define (list-of-lengths keylist tokens)
  62. (map
  63. (lambda (x)
  64. (let ((set (lset-intersection eq? tokens x)))
  65. ;; apply some weighting to the result
  66. (* (/ (length set) (length x)) (length set))))
  67. keylist))
  68.  
  69. (define (index-of-largest-number list-of-numbers)
  70. (let ((n (car (sort list-of-numbers >))))
  71. (if (zero? n)
  72. #f
  73. (list-index (lambda (x) (eq? x n)) list-of-numbers))))
  74.  
  75.  
  76. (define (lookup id tokens)
  77. (let* ((record (assv-ref decisiontable id))
  78. (keylist (get-keywords id))
  79. (index (index-of-largest-number (list-of-lengths keylist tokens))))
  80. (if index
  81. (cadr (list-ref record index))
  82. #f)))
  83.  
  84.  
  85. (define (startgame initial-id)
  86. (let loop ((id initial-id) (description #t))
  87. (if description
  88. (printf "~a\n> " (get-response id))
  89. (printf "> "))
  90. (let* ((input (read-line))
  91. (string-tokens (string-tokenize input))
  92. (tokens (map string->symbol string-tokens)))
  93. (let ((response (lookup id tokens)))
  94. (cond ((number? response)
  95. (loop response #t))
  96. ((eq? #f response)
  97. (format #t "huh? I didn't understand that!\n")
  98. (loop id #f))
  99. ((eq? response 'look)
  100. (get-directions id)
  101. (loop id #f))
  102. ((eq? response 'quit)
  103. (format #t "So Long, and Thanks for All the Fish...\n")
  104. (exit)))))))
  105.  
  106. (startgame 1)
Add Comment
Please, Sign In to add comment