Guest User

Untitled

a guest
Oct 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.36 KB | None | 0 0
  1. (defun nim-bot-play (heaps &key (misere nil))
  2.   (let ((game-sum (reduce #'(lambda (size acc) (boole boole-xor size acc)) heaps))
  3.     (num-heaps (length heaps)))
  4.     (if (zerop game-sum)
  5.     ;;If nim sum of the game is zero, then take the entire first non-empty
  6.     ;; heap
  7.     (loop
  8.        for size in heaps
  9.        for num from 1 to num-heaps
  10.        when (> size 0)
  11.        return `(:take ,size :from-heap ,num))
  12.     (let ((even-or-odd-p (if misere #'oddp #'evenp))
  13.           (heaps-bigger-than-two (loop for size in heaps count (> size 2))))
  14.       (if (zerop heaps-bigger-than-two)
  15.           ;; If there are no more heaps left with more than two elements
  16.           ;; then leave an even number of heaps of size 1 for non-misere
  17.           ;; or an odd number of heaps of size 1 for misere
  18.           (loop
  19.            for size in heaps
  20.            for num from 1 to num-heaps
  21.            when (= size 2)
  22.            return `(:take
  23.                 ,(if (funcall even-or-odd-p
  24.                   (loop for size in heaps count (= size 1)))
  25.                  size
  26.                  (1- size))
  27.                 :from-heap ,num))
  28.           ;; Otherwise, remove the number of elements equivilent to the
  29.           ;; nim sum of the game from the first heap that has greater or
  30.           ;; equal number of members to the nim sum
  31.           (loop
  32.            for size in heaps
  33.            for num from 1 to num-heaps
  34.            when (> size (boole boole-xor size game-sum))
  35.            return `(:take ,game-sum :from-heap ,num)))))))
Add Comment
Please, Sign In to add comment