home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / xlisp / xl21freq.zip / AKALAH.LSP < prev    next >
Lisp/Scheme  |  1993-12-17  |  10KB  |  322 lines

  1. ; To play against the computer:
  2. ; (meplay #pits-per-side #pebbles-per-pit #search-depth #computer-plays-first)
  3. ;
  4. ; To have the computer play against itself:
  5. ; (play #pits-per-side #pebbles-per-pit #search-depth)
  6.  
  7.  
  8. ; The playing arena:
  9. ; 13    12 11 10 9  8  7
  10. ;    0  1  2  3  4  5     6 
  11.  
  12. ; Pick up the pile of stones in a pit and seed them counterclockwise,
  13. ; except for the oponents kalah hole.
  14. ; If the last pebble lands in your own empty hole, pick up that pebble
  15. ;  and the one oposite of the opponents hole, and put them in ones kalah hole
  16. ; If the last pebble lands in your own kalah hole, play again
  17. ; If as a result of your move, you have no more pebbles, then your oponent
  18. ; takes all of his pebbles.
  19. ; The first player to get at greater than 50% of the pebbles wins.
  20.  
  21. (defun makelist (length contents)
  22.     (if    (zerop length)
  23.         'nil
  24.         (cons contents (makelist (1- length) contents))))
  25.  
  26. (defconstant *maxvalue* 1000)
  27. (defconstant *minvalue* (- *maxvalue*))
  28. (defconstant *firsta* 0)
  29. (defvar *enda*)
  30. (defvar *endb*)
  31. (defvar *moves*)
  32. (defvar *firstb*)
  33. (defvar *halfall*)
  34. (defvar *board*)
  35. (defvar *lasta*)
  36. (defvar *lastb*)
  37.  
  38. (defmacro copy (list) `(append ,list 'nil))
  39.  
  40. (defmacro empty (position hole)    ; empty out the given hole
  41.     `(setf (nth ,hole ,position) 0))
  42.  
  43. (defmacro kalaholefn (whoseturn)   ; the scoring hole for the given player
  44.     `(if ,whoseturn *endb* *enda*))
  45.  
  46. (defmacro opposholefn (hole)    ; calculate the opposing hole
  47.      `(- *lastb* ,hole))
  48.  
  49. (defmacro ownsidep (hole whoseturn)
  50.     `(if ,whoseturn (> ,hole *enda*) (< ,hole *firstb*)))
  51.  
  52. (defmacro prinb (x) `(dotimes (i ,x) (princ #\space)))
  53.  
  54. (defun search (startpos depth whoseturn)
  55.    (if (zerop depth)
  56.        (list startpos (evaluate startpos whoseturn))
  57.        (let (bestval nextval bestpos succlist)
  58.         (setq succlist (successorsfn startpos whoseturn))
  59.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  60.         (setq 
  61.             beta    *maxvalue*
  62.             bestval *minvalue*
  63.         bestpos (car succlist))
  64.         (dolist (this succlist)
  65.             (when (wincheck this whoseturn)
  66.             (return-from search (list this *maxvalue*)))
  67.         (setq nextval (- (alphabeta this
  68.                         (- beta)
  69.                         (- bestval)
  70.                         (1- depth)
  71.                         (not whoseturn))))
  72.         (when (> nextval bestval)
  73.               (setq bestval nextval)
  74.               (setq bestpos this)))
  75.         (list bestpos bestval))))    ; return value
  76.  
  77. (defun alphabeta (position alpha beta depth whoseturn)
  78.     (if (zerop depth)
  79.         (evaluate position whoseturn)
  80.     (let (bestval nextval succlist)
  81.         (setq succlist (successorsfn position whoseturn))
  82.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  83.         (setq bestval alpha)
  84.         (dolist (this succlist)
  85.             (when (wincheck this whoseturn)
  86.               (return-from alphabeta *maxvalue*))
  87.         (setq nextval (- (alphabeta this
  88.                         (- beta)
  89.                         (- bestval)
  90.                         (1- depth)
  91.                         (not whoseturn))))
  92.         (when (> nextval bestval) (setq bestval nextval))
  93.         (when (<= beta bestval) (return-from alphabeta bestval)))
  94.         bestval)))
  95.  
  96. (defun successorsfn (position whoseturn 
  97.             &aux 
  98.             (picuphole (1- (if whoseturn *firstb* *firsta*)))
  99.             succlist 
  100.             succ 
  101.             stones 
  102.             disthole 
  103.             lasthole)
  104.    (dotimes (dummy *enda*)
  105.         (when (not (zerop (nth (setq picuphole (1+ picuphole)) position)))
  106.           (setq    succ (copy position))
  107.           (setf (nth *moves* succ) 
  108.             (cons (1+ dummy) (nth *moves* succ)))
  109.           (setq stones (nth picuphole succ)) ; stones in this pit
  110.           (empty succ picuphole)
  111.           (setq disthole picuphole)
  112.           (dotimes (dummy2 stones) ; drop in successive holes except
  113.                          ; opponents kalah hole
  114.                  (setq disthole (nextdistholefn disthole whoseturn))
  115.                (dropin succ disthole 1))
  116.           (setq lasthole disthole)
  117.           (cond ((allownzerok succ whoseturn) ; all played out
  118.                (opptakesallfn succ whoseturn))
  119.             ((eq lasthole (kalaholefn whoseturn)) ; last in kalah
  120.              (setq succ (successorsfn succ whoseturn)))
  121.             ((and (eq (nth lasthole succ) 1) ; last into own empty
  122.                   (> (nth (opposholefn lasthole) succ) 0)
  123.                   (ownsidep lasthole whoseturn))
  124.              (dropin succ 
  125.                   (kalaholefn whoseturn)
  126.                  (1+ (nth (opposholefn lasthole) succ)))
  127.              (empty succ lasthole)
  128.              (empty succ (opposholefn lasthole))
  129.              (when (allownzerok succ whoseturn)
  130.                    (opptakesallfn succ whoseturn))))
  131.           (setq succlist (nconc (preparelisfn succ) succlist))))
  132.     (if (null succlist)
  133.         (progn    (setq succ (copy position))
  134.         (opptakesallfn succ whoseturn)
  135.         (list succ))
  136.     succlist))
  137.  
  138. (defun dropin (position hole number) 
  139.     (setf (nth hole position)
  140.           (+ number
  141.          (nth hole position))))
  142.  
  143. (defun nextdistholefn (disthole whoseturn)
  144.     (cond    ((and whoseturn (eql disthole *lasta*)) *firstb*) ; skip own pile
  145.         ((and (not whoseturn) (eql disthole *lastb*)) *firsta*)
  146.         ((< disthole *endb*) (1+ disthole))
  147.         (t *firsta*)))
  148.  
  149. (defun preparelisfn (x)
  150.     (if (atom (car x))
  151.         (list x)
  152.         (unimbedfn x)))
  153.  
  154. (defun reorder (poslist whoseturn)
  155.        (mapcar #'car (sort (mapcar #'(lambda (x) 
  156.                          (cons x
  157.                            (evaluate x whoseturn)))
  158.                    poslist)
  159.                #'(lambda (x y) (>= (cdr x) (cdr y))))))
  160.                 
  161.  
  162. (defun wincheck (position whoseturn)
  163.     (> (nth (kalaholefn whoseturn) position) *halfall*))
  164.  
  165. (defun evaluate (position whoseturn) ; assign the value to the position
  166.                      ; could obviously use work
  167.     (let ((ownkala (nth (kalaholefn whoseturn) position))
  168.           (oppkala (nth (kalaholefn (not whoseturn)) position)))
  169.          (cond ((> ownkala *halfall*) *maxvalue*)
  170.                 ((> oppkala *halfall*) *minvalue*)
  171.            (t (- ownkala oppkala)))))
  172.  
  173. (defun printins ()
  174.        (terpri)
  175.        (format t "Select Hole:~%    ")
  176.        (dotimes (i *enda*)
  177.         (let ((n (- *enda* i)))
  178.              (prin1 n)
  179.              (prinb (if (> 10 n) 2 1))
  180.              )))
  181.  
  182. (defun listmoves (position &aux (l (nth *moves* position)))
  183.        (setf (nth *moves* position) nil)
  184.        (terpri)
  185.        (format t "Moves: ")
  186.        (mapc #'(lambda (x) (format t "~s " x)) (reverse l)))
  187.  
  188. (defun printpos (position)
  189.     (let ((minprint (reverse (subseq position 0 *firstb*)))
  190.           (maxprint (subseq position *firstb* *moves*)))
  191.          (terpri)
  192.          (prin1 (car minprint))
  193.          (prinb (if (> 10 (car minprint)) 3 2))
  194.          (setq minprint (cdr minprint))
  195.          (do ((list minprint (cdr list)))
  196.              ((null list))
  197.          (prin1 (car list))
  198.          (prinb (if (> 10 (car list)) 2 1)))
  199.          (terpri)
  200.          (prinb 4)
  201.          (do ((list maxprint (cdr list)))
  202.              ((null (cdr list)))
  203.          (prin1 (car list))
  204.          (prinb (if (> 10 (car list)) 2 1)))
  205.          (prinb 2)
  206.          (prin1 (car (last maxprint)))
  207.          (terpri)
  208.          (terpri)))
  209.  
  210. (defun allownzerok (position whoseturn)
  211.     (zerop (apply #'+ (if whoseturn 
  212.                   (subseq position *firstb* *endb*)
  213.                   (subseq position *firsta* *enda*)))))
  214.  
  215.  
  216. (defun opptakesallfn (position whoseturn)
  217.     (dropin position
  218.         (kalaholefn (not whoseturn))
  219.         (apply #'+ (if whoseturn 
  220.                    (subseq position *firsta* *enda*)
  221.                    (subseq position *firstb* *endb*))))
  222.     (do    ((count *enda* (1- count))
  223.          (hole (if whoseturn *firsta* *firstb*) (1+ hole)))
  224.         ((zerop count))
  225.         (empty position hole)))
  226.  
  227. (defun nextmove (depth whoseturn)
  228.        (terpri)
  229.        (setq *board* (search (car *board*) depth whoseturn))
  230.        (listmoves (car *board*))
  231.        (printpos (car *board*))
  232.        (print (cdr *board*))
  233.        (terpri))
  234.  
  235. (defun unimbedfn (poslist)
  236.     (do    ((list poslist (cdr list))
  237.          (result nil (if (atom (caar list))
  238.                   (cons (car list) result)
  239.                  (append (unimbedfn (car list)) result))))
  240.             ((null list) result)))
  241.  
  242. (defun initialize (holes pebbles 
  243.              &aux (temp (makelist holes pebbles)))
  244.         ; initialize the playing area
  245.     (setq *enda* holes
  246.           *endb* (+ holes holes 1)
  247.           *lasta* (1- *enda* )
  248.           *firstb* (1+ *enda*)
  249.           *lastb* (1- *endb*)
  250.           *moves* (1+ *endb*)
  251.           *halfall* (* holes pebbles)
  252.           *board* (list (append temp
  253.                     (list 0)
  254.                     temp
  255.                     (list 0)
  256.                     '(nil))
  257.                 0)))
  258.  
  259. (defun play (holes pebbles depth) ; play the game
  260.     (initialize holes pebbles)
  261.     (do ((whoseturn nil (not whoseturn))
  262.          (scorea 0 (nth *enda* (car *board*)))
  263.          (scoreb 0 (nth *endb* (car *board*))))
  264.         ((or (> scorea *halfall*)
  265.          (> scoreb *halfall*)
  266.          (= scorea scoreb *halfall*)))
  267.         (nextmove depth whoseturn)))
  268.  
  269. (defun meplay (holes pebbles depth computer-first) 
  270.   (prog (picuphole)
  271.     (initialize holes pebbles)
  272.     (when computer-first (setq succ (copy (car *board*)))
  273.           (go y))
  274. n    (setq succ (copy (car *board*)))
  275.     (printins)
  276.     (printpos succ)
  277.     (when (> (nth *enda* succ) *halfall*) 
  278.           (format t "You win!!~%")
  279.           (return t))    ; win for side a
  280.     (when (> (nth *endb* succ) *halfall*) 
  281.           (format t "I win!!!~%")
  282.           (return nil)) ; win for side b
  283.     (when (= (nth *enda* succ) (nth *endb* succ) *halfall*)
  284.           (format t "We tie???~%")
  285.           (return nil))
  286. x    (princ "Hole? ") (setq picuphole (read))
  287.     (when (or (not (numberp picuphole))
  288.           (>= picuphole *firstb*)
  289.           (> 1 picuphole)
  290.           (zerop (setq stones 
  291.                    (nth (setq picuphole (1- picuphole)) succ))))
  292.         (go x))
  293.     (empty succ picuphole)
  294.     (setq disthole picuphole)
  295.     (dotimes (dummy stones)
  296.              (dropin succ 
  297.              (setq disthole (nextdistholefn disthole nil)) 1))
  298.     (setq lasthole disthole)
  299.     (cond    ((allownzerok succ nil)
  300.          (opptakesallfn succ nil)
  301.          (setq *board* (list succ 0))
  302.          (go n))
  303.         ((eql lasthole *enda*)
  304.          (setq *board* (list succ 0))
  305.          (go n))
  306.         ((and (eql (nth lasthole succ) 1)
  307.               (> (nth (opposholefn lasthole) succ) 0)
  308.               (> *enda* lasthole))
  309.          (dropin succ *enda* (1+ (nth (opposholefn lasthole) succ)))
  310.          (empty succ lasthole)
  311.          (empty succ (opposholefn lasthole))
  312.          (when (allownzerok succ nil)
  313.                (opptakesallfn succ nil)
  314.                (setq *board* (list succ 0))
  315.                (go n))))
  316.     (printpos succ)
  317. y    (format t "(Computer is thinking...)~%")
  318.     (setq *board* (search succ depth t))
  319.     (listmoves (car *board*))
  320.     (go n)))
  321.  
  322.