Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. (format t "LISP Nim~%~%")
  2. (defvar L '())
  3. ;; (setq tok 8)
  4. ;; (format t "We have ~D tokens initially.~%" tok)
  5.  
  6. (defun random123 () ;; this returns a number 1,2,or 3
  7. (+ 1 (random 3))
  8. )
  9.  
  10.  
  11.  
  12. (defun pickPile(L)
  13. (setq x 0)
  14. (loop for i from 0 to (length L)
  15. do (
  16. if (= (nth x L) 0) (setq x (+ x 1))
  17. )
  18. )
  19. (format t "X is : ~D" x)
  20. x
  21. )
  22.  
  23. ;;this to0k so long im so dumb
  24. (defun makePiles (N L X)
  25. (make-list X :initial-element N)
  26. )
  27.  
  28. (defun take123 (tokens)
  29. ;; given # of tokens, return a number 1,2,3 or tokens
  30. (if (>= tokens (setq x (+ 1 (random 3))))
  31. x
  32. tokens
  33. )
  34. )
  35.  
  36.  
  37.  
  38. (defun cturn (L playerNum)
  39. (setq pileNum (pickPile L))
  40. (setq curTokens (nth pileNum L))
  41. (setq take (take123 curTokens))
  42. (setf (nth pileNum L) (- (curTokens) take))
  43. (format t "(P~D ~D ~D)~S~%" playerNum (+ pileNum 1) take L)
  44. L
  45. )
  46.  
  47. (defun printRemaining (remaining)
  48. (format t "~D tokens remaining~%~%" remaining))
  49.  
  50. (defun checkWins (L)
  51. (loop for x in L
  52. do (if (= x 0)
  53. (setq nonZeroes nil)
  54. (setq nonZeroes t)
  55.  
  56. )
  57.  
  58. )
  59. (if (nonZeroes)
  60. (return nil)
  61. (return t))
  62. )
  63.  
  64. (defun p2nim1 (numTokens numPiles numPlayers)
  65. (setq playerNum 1)
  66. (setq tok N)
  67. (setq L (makePiles(numTokens numPiles)))
  68. (loop
  69.  
  70. (setq L (cturn L playerNum))
  71. (if (checkWins)
  72. (return (format t "~%**P~D wins**~%" playerNum))
  73. )
  74.  
  75. (if (= playerNum 3) (setq playerNum 1) (+ 1 playerNum))
  76. )
  77.  
  78. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement