make-decision.lisp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (defvar *initialized-p* nil)
  2. (defun ensure-positive (number)
  3. (if (< number 0)
  4. (- number)
  5. number))
  6. (defun ensure-initialized ()
  7. (when (not *initialized-p*)
  8. (setq *random-state* (make-random-state t)
  9. *initialized-p* t)
  10. (dotimes (number (ensure-positive (random 100)))
  11. (dotimes (other (ensure-positive (random (1+ number))))
  12. (dotimes (yet-another (ensure-positive (random (1+ other))))
  13. (ensure-positive (random (1+ yet-another))))))))
  14. (defun flip-coin (stream number)
  15. (ensure-initialized)
  16. (format stream "Flipping ~a coins.~&" number)
  17. (dotimes (n number)
  18. (if (= (mod (random 100) 2) 0)
  19. (format stream "Heads.~&")
  20. (format stream "Tails.~&"))))
  21. (defun roll-die (stream number type)
  22. (ensure-initialized)
  23. (format stream "Rolling a ~a-sided die ~a times.~&" type number)
  24. (dotimes (n number)
  25. (let ((die (1+ (mod (ensure-positive (random (* 100 type))) type))))
  26. (format stream "You rolled a ~a.~&" die))))
  27. (defun main ()
  28. (let ((type (if (not (null (second *posix-argv*)))
  29. (intern (string-upcase (second *posix-argv*)) "KEYWORD")))
  30. (times (if (and (not (null (second *posix-argv*)))
  31. (not (null (third *posix-argv*))))
  32. (parse-integer (third *posix-argv*) :junk-allowed t)
  33. 1)))
  34. (case type
  35. (:die
  36. (roll-die *standard-output* times (if (not (null (fourth *posix-argv*)))
  37. (parse-integer (fourth *posix-argv*) :junk-allowed t)
  38. 6)))
  39. (:coin
  40. (flip-coin *standard-output* times))
  41. (t
  42. (format *standard-output*
  43. "make-decision [ die [ times [ type ] ] | coin [ times ] ]~&")))))