チェスのゲームでポーンの有効な動きを計算する関数に取り組んでいます。機能white-pawn-move
は動作します。どちらかのプレイヤーのポーン ( ) に対して一般化しようとするとpawn-move
、不正な関数呼び出しに遭遇します。私はreplでファンコールをテストしましたが、それが問題だとは思いません。
私は何を間違っていますか?
(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 #'-))