Oladon

Role-playing Character Personality Peer Quiz

Aug 5th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.10 KB | None | 0 0
  1. (defparameter *names* (list 'character1 'character2 'character3))
  2.  
  3. (defparameter *strings* (list "~A 1 question" "~A 2 question" "~A 3 question" "~A 4 question" "~A 5 question" "~A 6 question" "~A 7 question"))
  4.  
  5. (defun replace-all (string part replacement &key (test #'char=))
  6. "Returns a new string in which all the occurences of the part
  7. is replaced with replacement."
  8.     (with-output-to-string (out)
  9.       (loop with part-length = (length part)
  10.             for old-pos = 0 then (+ pos part-length)
  11.             for pos = (search part string
  12.                               :start2 old-pos
  13.                               :test test)
  14.             do (write-string string out
  15.                              :start old-pos
  16.                              :end (or pos (length string)))
  17.             when pos do (write-string replacement out)
  18.             while pos)))
  19.  
  20. (defun tweak-text (lst caps lit)
  21.   (when lst (let ((item (car lst))
  22.                   (rest (cdr lst)))
  23.               (cond ((eq item #\space) (cons item (tweak-text rest caps lit)))
  24.                     ((member item '(#\! #\? #\.)) (cons item (tweak-text rest t lit)))
  25.                     ((eq item #\") (tweak-text rest caps (not lit)))
  26.                    (lit (cons item (tweak-text rest nil lit)))
  27.                    ((or caps lit) (cons (char-upcase item) (tweak-text rest nil lit)))
  28.                    (t (cons (char-downcase item) (tweak-text rest nil nil)))))))
  29.  
  30. (defun interp1 (name string)
  31.  (format nil (replace-all string "~A" "~:*~A")
  32.          (coerce (tweak-text (coerce (prin1-to-string name)
  33.                                      'list)
  34.                              t
  35.                              nil)
  36.                  'string)))
  37.  
  38. (defun combine-all (namelist stringlist)
  39.  (mapcar (lambda (x) (mapcar (lambda (y) (interp1 y x)) namelist)) stringlist))
  40.  
  41. (defun swap-right (lst)
  42. "Swaps the first element of a given list with a random
  43. OTHER element (not itself). This is intentional. We do
  44. not want each party member appearing in his/her own list."
  45.  (if (= (length (cdr lst)) 0)
  46.      nil
  47.    (progn
  48.      (rotatef (car lst)
  49.               (nth (1+ (random (length (cdr lst))))
  50.                    lst))
  51.             lst)))
  52.  
  53. (defun shuffle (lst)
  54. "This goes through a list and swap-rights each element."
  55.  (if (null lst)
  56.      nil
  57.    (let ((swapped (swap-right lst)))
  58.      (cons (car lst) (shuffle (cdr swapped))))))
  59.  
  60. (defun transpose (lst)
  61. "Does just what it says; transposes a list of lists,
  62. creating a number of new lists equal to the length of
  63. the longest starting sub-list."
  64.  (let ((maxsize (reduce #'max (mapcar #'length lst))))
  65.    (loop for i
  66.          from 0
  67.          to (1- maxsize)
  68.          collect (mapcar (lambda (x) (nth i x))
  69.                          lst))))
  70.  
  71. (defun make-party-alist (namelist stringlist)
  72. "Creates a list of questions for each party member
  73. about the others."
  74.  (pairlis namelist
  75.           (transpose (mapcar #'shuffle
  76.                              (combine namelist
  77.                                       stringlist)))))
  78.  
  79. (defun make-dm-list (namelist stringlist)
  80. "Creates a list of questions (intended for the DM)
  81. about the various party members."
  82.  (let* ((numnames (length namelist))
  83.        (numstrings (length stringlist))
  84.        (newnamelist (copy-list namelist))
  85.        (newstringlist (subseq stringlist 0 (- numstrings (mod numstrings numnames)))))
  86.    (alexandria:shuffle (mapcar (lambda (y)
  87.                                  (interp1
  88.                                   (car (setf newnamelist
  89.                                              (alexandria:rotate newnamelist)))
  90.                                   y))
  91.                                (alexandria:shuffle newstringlist)))))
  92.  
  93. (defun pprint-questions (qlist)
  94.  (if (null qlist)
  95.      nil
  96.    (loop for i
  97.          below (length qlist)
  98.          collect (format t
  99.                     "~d. ~A~%   ~A~%~%~%"
  100.                     (1+ i)
  101.                     (nth i qlist)
  102.                     (coerce (make-list 250
  103.                                        :initial-element #\_)
  104.                             'string)))))
Advertisement
Add Comment
Please, Sign In to add comment