home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xl21hos2.zip / AKALAH.LSP < prev    next >
Lisp/Scheme  |  1995-12-27  |  11KB  |  348 lines

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