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

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