1

我正在研究一个函数来计算国际象棋游戏中棋子的有效移动。该功能white-pawn-move有效。当我试图将它概括为任一玩家的棋子 ( pawn-move) 时,我遇到了一个非法的函数调用。我已经在 repl 中测试了 funcall,但我认为这不是问题。

我究竟做错了什么?

http://pastebin.com/fEiQTwi5

(defun white-pawn-move (file rank)       
  (let ((movelist '()))
    (if (and (within-boardp file (+ rank 1))
             (eql #\s (aref *board* (+ rank 1) file)))
        (push (cons file (+ rank 1)) movelist))
    (if (= rank 1) 
        (push (cons file (+ rank 2)) movelist))
    (if (and (within-boardp (- file 1) (+ rank 1))
             (belongs-to-opponent (aref *board*  (+ rank 1) (- file 1))))
        (push (cons (- file 1) (+ rank 1)) movelist))
    (if (and (within-boardp (+ file 1) (+ rank 1))
             (belongs-to-opponent (aref *board* (+ rank 1) (+ file 1))))
        (push (cons (+ file 1) (+ rank 1)) movelist))
    movelist))      

;refactor:
;file / rank numeric      
(defun pawn-move (direction)
  (let ((startrank (if (eql direction #'+)
                       1
                       6)))
    (lambda (file rank)
      (let ((movelist '()))
        (if (and (within-boardp file (funcall direction rank 1))
                 (eql #\s (aref *board* (funcall direction rank 1) file)))
            (push (cons file (funcall direction rank 1)) movelist))
        (if (= rank startrank) 
            (push (cons file (funcall direction rank 2)) movelist))
        (if (and (within-boardp (- file 1) (funcall direction rank 1))
                 (belongs-to-opponent (aref *board* 
                                            (funcall direction rank 1)
                                            (- file 1))))
            (push (cons (- file 1) (funcall direction rank 1)) movelist))
        (if (and (within-boardp (+ file 1) (funcall direction rank 1))
                 (belongs-to-opponent (aref *board*
                                            (funcall direction rank 1)
                                            (+ file 1))))
            (push (cons (+ file 1) (funcall direction rank 1)) movelist))
        movelist))))
;desired usage
(setf (gethash #\P *move-table*) (pawn-move #'+))    
(setf (gethash #\p *move-table*) (pawn-move #'-))
4

2 回答 2

0

white-pawn-move返回一个移动列表,而您pawn-move返回一个可以返回移动列表的函数。我猜你试图调用(pawn-move #'+)你以前调用的地方(white-pawn-move),但你必须调用(funcall (pawn-move #'+))

于 2013-08-20T23:37:03.943 回答
0

你能显示你得到的错误吗?

我刚刚使用 Emacs Lisp 尝试了您的代码(将字符表示更改为 Emacs-Lisp),并且我没有收到 sexps(pawn-move #'+)(pawn-move #'-). 他们是什么给你带来了错误吗?我得到这个(pawn-move #'+),例如:

     (lambda (文件等级)
       (让 ((movelist 'nil))
         (if (and (within-boardp)
                   文件
                   (功能方向排名1))
                  (当量 115
                       (参考*板*
                             (功能方向排名1)
                             文件)))
             (push (cons file (funcall direction rank 1))
                   移动列表))
         (如果(= 排名 startrank)
             (push (cons file (funcall direction rank 2))
                   移动列表))
         (if (and (within-boardp)
                   (- 文件 1)
                   (功能方向排名1))
                  (属于对手
                   (aref *board* (funcall direction rank 1)
                         (- 文件 1))))
             (push (cons (-file 1) (funcall direction rank 1))
                   移动列表))
         (if (and (within-boardp)
                   (+ 文件 1)
                   (功能方向排名1))
                  (属于对手
                   (参考*板*
                         (功能方向排名1)
                         (+ 文件 1))))
             (push (cons (+ file 1) (funcall direction rank 1))
                   移动列表))
         移动列表))


总而言之,也许就您所看到的内容提供更多细节。

于 2013-08-20T04:29:04.473 回答