home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / lisp / interpre / xlispplu / lsp / akavect.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1992-01-14  |  10.3 KB  |  323 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. (if (fboundp 'generic)
  44.     (defmacro copy (array) `(generic ,array))    ; not the generic solution!
  45.     (defmacro copy (array) `(concatenate 'array ,array)) ;The generic solution
  46. )
  47.  
  48. (defmacro empty (position hole)    ; empty out the given hole
  49.     `(setf (aref ,position ,hole) 0))
  50.  
  51. (defmacro kalaholefn (whoseturn)   ; the scoring hole for the given player
  52.     `(if ,whoseturn *endb* *enda*))
  53.  
  54. (defmacro opposholefn (hole)    ; calculate the opposing hole
  55.      `(- *lastb* ,hole))
  56.  
  57. (defmacro ownsidep (hole whoseturn)
  58.     `(if ,whoseturn (> ,hole *enda*) (< ,hole *firstb*)))
  59.  
  60. (defmacro allownzerok (position whoseturn)
  61.        `(zerop (countown ,position ,whoseturn)))
  62.  
  63. (defmacro prinb (x) `(dotimes (i ,x) (princ #\space)))
  64.  
  65. (defmacro wincheck (position whoseturn)
  66.     `(> (aref ,position (kalaholefn ,whoseturn)) *halfall*))
  67.  
  68. (defun search (startpos depth whoseturn)
  69.    (if (zerop depth)
  70.        (list startpos (evaluate startpos whoseturn))
  71.        (let (bestval nextval bestpos succlist)
  72.         (setq succlist (successorsfn startpos whoseturn))
  73.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  74.         (setq 
  75.             beta    *maxvalue*
  76.             bestval *minvalue*
  77.         bestpos (car succlist))
  78.         (dolist (this succlist)
  79.             (when (wincheck this whoseturn)
  80.             (return-from search (list this *maxvalue*)))
  81.         (setq nextval (- (alphabeta this
  82.                         (- beta)
  83.                         (- bestval)
  84.                         (1- depth)
  85.                         (not whoseturn))))
  86.         (when (> nextval bestval)
  87.               (setq bestval nextval)
  88.               (setq bestpos this)))
  89.         (list bestpos bestval))))    ; return value
  90.  
  91. (defun alphabeta (position alpha beta depth whoseturn)
  92.     (if (zerop depth)
  93.         (evaluate position whoseturn)
  94.     (let (bestval nextval succlist)
  95.         (setq succlist (successorsfn position whoseturn))
  96.         (when (> depth 1) (setq succlist (reorder succlist whoseturn)))
  97.         (setq bestval alpha)
  98.         (dolist (this succlist)
  99.             (when (wincheck this whoseturn)
  100.               (return-from alphabeta *maxvalue*))
  101.         (setq nextval (- (alphabeta this
  102.                         (- beta)
  103.                         (- bestval)
  104.                         (1- depth)
  105.                         (not whoseturn))))
  106.         (when (> nextval bestval) (setq bestval nextval))
  107.         (when (<= beta bestval) (return-from alphabeta bestval)))
  108.         bestval)))
  109.  
  110. (defun successorsfn (position whoseturn 
  111.             &aux 
  112.             (picuphole (1- (if whoseturn *firstb* *firsta*)))
  113.             succlist 
  114.             succ 
  115.             stones 
  116.             disthole 
  117.             lasthole)
  118.    (dotimes (dummy *enda*)
  119.         (when (not (zerop (aref position (setq picuphole (1+ picuphole)))))
  120.           (setq    succ (copy position))
  121.           (setf (aref succ *moves*) 
  122.             (cons (1+ dummy) (aref succ *moves*)))
  123.           (setq stones (aref succ picuphole)) ; stones in this pit
  124.           (empty succ picuphole)
  125.           (setq disthole picuphole)
  126.           (dotimes (dummy2 stones) ; drop in successive holes except
  127.                          ; opponents kalah hole
  128.                  (setq disthole (nextdistholefn disthole whoseturn))
  129.                (dropin succ disthole 1))
  130.           (setq lasthole disthole)
  131.           (cond ((allownzerok succ whoseturn) ; all played out
  132.                (opptakesallfn succ whoseturn))
  133.             ((eq lasthole (kalaholefn whoseturn)) ; last in kalah
  134.              (setq succ (successorsfn succ whoseturn)))
  135.             ((and (eq (aref succ lasthole) 1) ; last into own empty
  136.                   (> (aref succ (opposholefn lasthole)) 0)
  137.                   (ownsidep lasthole whoseturn))
  138.              (dropin succ 
  139.                   (kalaholefn whoseturn)
  140.                  (1+ (aref succ (opposholefn lasthole))))
  141.              (empty succ lasthole)
  142.              (empty succ (opposholefn lasthole))
  143.              (when (allownzerok succ whoseturn)
  144.                    (opptakesallfn succ whoseturn))))
  145.           (setq succlist (nconc (preparelisfn succ) succlist))))
  146.     (if (null succlist)
  147.         (progn    (setq succ (copy position))
  148.         (opptakesallfn succ whoseturn)
  149.         (list succ))
  150.     succlist))
  151.  
  152. (defun dropin (position hole number) 
  153.     (setf (aref position hole) (+ number (aref position hole))))
  154.  
  155. (defun nextdistholefn (disthole whoseturn)
  156.     (cond    ((and whoseturn (eql disthole *lasta*)) *firstb*) ; skip own pile
  157.         ((and (not whoseturn) (eql disthole *lastb*)) *firsta*)
  158.         ((< disthole *endb*) (1+ disthole))
  159.         (t *firsta*)))
  160.  
  161. (defun preparelisfn (x)
  162.     (if (arrayp x)
  163.         (list x)
  164.         (unimbedfn x)))
  165.  
  166. (defun reorder (poslist whoseturn)
  167.        (mapcar #'car (sort (mapcar #'(lambda (x) 
  168.                          (cons x
  169.                            (evaluate x whoseturn)))
  170.                    poslist)
  171.                #'(lambda (x y) (>= (cdr x) (cdr y))))))
  172.                 
  173. (defun evaluate (position whoseturn) ; assign the value to the position
  174.                      ; could obviously use work
  175.     (let ((ownkala (aref position (kalaholefn whoseturn)))
  176.           (oppkala (aref position (kalaholefn (not whoseturn)))))
  177.          (cond ((> ownkala *halfall*) *maxvalue*)
  178.                 ((> oppkala *halfall*) *minvalue*)
  179.            (t (- ownkala oppkala)))))
  180.            
  181. (defun printins ()
  182.        (terpri)
  183.        (format t "Select Hole:~%    ")
  184.        (dotimes (i *enda*)
  185.         (let ((n (- *enda* i)))
  186.              (prin1 n)
  187.              (prinb (if (> 10 n) 2 1))
  188.              )))
  189.  
  190. (defun listmoves (position &aux (l (aref position *moves*)))
  191.        (setf (aref position *moves*) nil)
  192.        (terpri)
  193.        (format t "Moves: ")
  194.        (map nil #'(lambda (x) (format t "~s " x)) (reverse l)))
  195.  
  196. (defun printpos (position)
  197.          (terpri)
  198.          (prin1 (aref position *enda*))
  199.          (prinb (if (> 10 (aref position *enda*)) 3 2))
  200.          (dotimes (n *enda*)
  201.               (let ((val (aref position (- *enda* n 1))))
  202.              (prin1 val)
  203.             (prinb (if (> 10 val) 2 1))))
  204.          (terpri)
  205.          (prinb 4)
  206.          (dotimes (n *enda*)
  207.              (let ((val (aref position (+ *firstb* n))))
  208.              (prin1 val)
  209.             (prinb (if (> 10 val) 2 1))))
  210.          (prinb 2)
  211.          (prin1 (aref position *endb*))
  212.          (terpri)
  213.          (terpri))
  214.  
  215.  
  216. ; check if player is out of pieces (then opponent will take remainder)
  217. (defun countown (position whoseturn)
  218.        (reduce #'+ position :start (if whoseturn *firstb* *firsta*)
  219.                 :end   (if whoseturn *endb* *enda*)))
  220.  
  221. (defun opptakesallfn (position whoseturn)
  222.     (dropin position
  223.         (kalaholefn (not whoseturn))
  224.         (countown position (not whoseturn)))
  225.     (do    ((count *enda* (1- count))
  226.          (hole (if whoseturn *firsta* *firstb*) (1+ hole)))
  227.         ((zerop count))
  228.         (empty position hole)))
  229.  
  230. (defun nextmove (depth whoseturn)
  231.        (terpri)
  232.        (setq *board* (search (car *board*) depth whoseturn))
  233.        (listmoves (car *board*))
  234.        (printpos (car *board*))
  235.        (print (cdr *board*))
  236.        (terpri))
  237.  
  238. (defun unimbedfn (poslist)
  239.     (do    ((list poslist (cdr list))
  240.          (result nil (if (arrayp (car list))
  241.                   (cons (car list) result)
  242.                  (append (unimbedfn (car list)) result))))
  243.             ((null list) result)))
  244.  
  245. (defun initialize (holes pebbles 
  246.              &aux (temp (makefield (+ (* 2 holes) 3) pebbles)))
  247.        ; initialize the playing area
  248.     (setf *enda* holes
  249.           *endb* (+ holes holes 1)
  250.           *lasta* (1- *enda* )
  251.           *firstb* (1+ *enda*)
  252.           *lastb* (1- *endb*)
  253.           *halfall* (* holes pebbles)
  254.           *moves* (1+ *endb*)
  255.           (aref temp *enda*) 0
  256.           (aref temp *endb*) 0
  257.           (aref temp *moves*) nil
  258.           *board* (list temp 0)))
  259.  
  260. (defun play (holes pebbles depth) ; play the game
  261.     (initialize holes pebbles)
  262.     (do ((whoseturn nil (not whoseturn))
  263.          (scorea 0 (aref (car *board*) *enda*))
  264.          (scoreb 0 (aref (car *board*) *endb*)))
  265.         ((or (> scorea *halfall*)
  266.          (> scoreb *halfall*)
  267.          (= scorea scoreb *halfall*)))
  268.         (nextmove depth whoseturn)))
  269.  
  270. (defun meplay (holes pebbles depth computer-first) 
  271.   (prog (picuphole)
  272.     (initialize holes pebbles)
  273.     (when computer-first (setq succ (copy (car *board*)))
  274.           (go y))
  275. n    (setq succ (copy (car *board*)))
  276.     (printins)
  277.     (printpos succ)
  278.     (when (> (aref succ *enda*) *halfall*) 
  279.           (format t "You win!!~%")
  280.           (return t))    ; win for side a
  281.     (when (> (aref succ *endb*) *halfall*) 
  282.           (format t "I win!!!~%")
  283.           (return nil)) ; win for side b
  284.     (when (= (aref succ *enda*) (aref succ *endb*) *halfall*)
  285.           (format t "We tie???~%")
  286.           (return nil))
  287. x    (princ "Hole? ") (setq picuphole (read))
  288.     (if (or (not (numberp picuphole))
  289.         (>= picuphole *firstb*)
  290.         (> 1 picuphole)
  291.         (zerop (setq stones 
  292.                  (aref succ (setq picuphole (1- picuphole))))))
  293.         (go x))
  294.     (empty succ picuphole)
  295.     (setq disthole picuphole)
  296.     (dotimes (dummy stones)
  297.              (dropin succ 
  298.              (setq disthole (nextdistholefn disthole nil)) 1))
  299.     (setq lasthole disthole)
  300.     (cond    ((allownzerok succ nil)
  301.          (opptakesallfn succ nil)
  302.          (setq *board* (list succ 0))
  303.          (go n))
  304.         ((eql lasthole *enda*)
  305.          (setq *board* (list succ 0))
  306.          (go n))
  307.         ((and (eql (nth lasthole succ) 1)
  308.               (> (aref succ (opposholefn lasthole)) 0)
  309.               (> *enda* lasthole))
  310.          (dropin succ *enda* (1+ (aref succ(opposholefn lasthole))))
  311.          (empty succ lasthole)
  312.          (empty succ (opposholefn lasthole))
  313.          (when (allownzerok succ nil)
  314.                (opptakesallfn succ nil)
  315.                (setq *board* (list succ 0))
  316.                (go n))))
  317.     (printpos succ)
  318. y    (format t "(Computer is thinking...)~%")
  319.     (setq *board* (search succ depth t))
  320.     (listmoves (car *board*))
  321.     (go n)))
  322.  
  323.