Advertisement
Guest User

Lisp Game -- Ninjacop123 v1.1

a guest
Jan 3rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.30 KB | None | 0 0
  1. (defparameter kingdoms (make-hash-table :test #'equalp)) ; empty hashtable to store all the kingdoms
  2.  
  3. (defun r-walk (val len)
  4.   "increases val randomly, until len = 0"
  5.   (if (zerop len)
  6.       val
  7.       (r-walk (if (zerop (random 2))
  8.                  (+ val 3)
  9.                  (+ val (+ (random 11) 5)))
  10.              (1- len))))
  11.  
  12. (defun f-digit (val)
  13.   "Finds the first digit in val and converts
  14.   it from a float to a number."
  15.   (format t "~f / ~f = ~f~%" val (expt 10 (log val 10)) (/ val (expt 10 (log val 10))))
  16.   (rational (/ val (expt 10 (log val 10)))))
  17.  
  18. (defun init-kingdom (name)
  19.   "Initiates a kingdom as a hash table, indexed by name
  20.   and contains a pair of (workers . power)"
  21.   (setf (gethash name kingdoms) (list 1 100)))
  22.  
  23. (defun update-kingdoms (kingdom-list workers)
  24.   "Add workers to each kingdom by a random walk with the
  25.   value of workers and the length of the first digit of
  26.   a specific kingdom's power."
  27.   (loop for key being the hash-keys of kingdom-list
  28.         using (hash-value (a b))
  29.         do (setf (gethash key kingdom-list)
  30.                  (list (+ (r-walk workers
  31.                                   (f-digit a))
  32.                           a)
  33.                        (+ (r-walk workers
  34.                                   (f-digit a))
  35.                           b)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement