我一直在使用Scheme中的一个简单的国际象棋程序,我定义的一个辅助函数消耗了一个片段,它的坐标(当前位置)并将它放在指定的坐标(此处移动到此处)上国际象棋棋盘,切换出任何位于Move-here坐标的棋子。该功能正如我所希望的那样工作,但现在无论出于何种原因,它已不再正常运行。我不知道是什么导致这种情况,并且已经跟踪并重新跟踪代码一段时间,现在试图找到错误。我希望有人能够了解情况。
以下是片段切换功能的代码,以及代码中使用的结构:
(define-struct place (row column piece))
;; Place is a structure (make-place r c p) where r is the rank of a piece
;; which is a symbol of 'Pawn, 'Rook, 'Knight, 'Bishop, 'King or 'Queen
;; and column and place. Together c and p give the placement coordinates,
;; where column is one symbol from '(a b c d e f g h) and row is a number from
;; 1 - 8 inclusive.
(define-struct piece (rank color))
;; Piece is a structure (make-piece r col) where rank is as described for a place structure,
;; and colour is a symbo either 'black or 'white.
(define move-counter 1) ; Keeps track of the current number of mves made.
;; Odd indicates white to move, else black to move.
;; Swap-in: '(Symbol Symbol Nat) '(Symbol Nat) -> '(-2- void)
;; Conditions:
;; Pre: Swap-in is '(rank column row) From is '(column row)
;; Post: produces list of lists containing -2- and void. where void represents
;; any changed values.
;; Purpose: Swap-piece is a helper for any X-Move function that dictates the legal
;; moves of a given piece.
(define (swap-piece swap-in from)
(map (λ (x)
(map (λ (piece)
(cond
[(and (= (third swap-in) (place-row piece))
(symbol=? (second swap-in) (place-column piece)))
(set-place-piece! piece
(make-piece (first swap-in) (cond
[(odd? move-counter) 'white]
[else 'black])))]
[(and (= (second from) (place-row piece))
(symbol=? (first from) (place-column piece)))
(set-place-piece! piece (make-piece empty empty))]
[else void]))
x))
board))
这是两个例子;第一个是它输出的,第二个是什么 它应该输出(在这个例子中,Swap-piece有一个小的修改,因此它的一个参数是一个板,因为不使用我的棋盘上的整个数组)。
(define Example-board (list
(list
(make-place 8 'a (make-piece 'Rook 'black))
(make-place 7 'a (make-piece 'Pawn 'black)))
(list
(make-place 4 'a (make-piece 'Queen 'white))
(make-place 6 'b (make-piece 'King 'White)))))
> (swap-piece '(Queen a 4) '(a 7) Example-board)
(shared ((-2- void)) (list (list -2- (void)) (list (void) -2-)))
所以我打电话给示例板来获取更新的电路板:
> Example-board
(list
(list
(make-place 8 'a (make-piece 'Rook 'black))
(make-place 7 'a (make-piece empty empty)))
(list
(make-place 4 'a (make-piece 'Queen 'white))
(make-place 6 'b (make-piece 'King 'White))))
但是,我期望的输出是:
> Example-board
(list
(list
(make-place 8 'a (make-piece 'Rook 'black))
(make-place 7 'a (make-piece 'Queen 'white)))
(list
(make-place 4 'a (make-piece empty empty))
(make-place 6 'b (make-piece 'King 'White))))
对不起,对于长篇文章,但我无法弄清楚导致这种情况发生的原因。正如我所说,我确信这段代码在几个小时前就已经开始工作了。
编辑:我应该补充一点,我的交换功能中地图功能正在作用的列表,棋盘是棋盘。
答案 0 :(得分:1)
我意识到导致错误放置/交换的原因是什么。我将交换部分向后移动,几乎肯定是因为我错误地标记了我的参数。我的代码基本上说:“如果我正在看的正方形的坐标等于我放置的棋子的坐标,请不要改变正方形”,这显然违背了移动棋子的目的。对于任何感兴趣的人,正确的代码是:
(define (swap-piece swap-in from)
(map (λ (x)
(map (λ (piece)
(cond
; comparing coordinates of current square to swap-in
[(and (= (third swap-in) (place-row piece))
(symbol=? (second swap-in) (place-column piece)))
; If they are equal place, remove swap-in from that square
(set-place-piece! piece (make-piece empty empty))]
; If the coordinates of the square equal the coordinates of From
; place the swap-in piece there
[(and (= (second from) (place-row piece))
(symbol=? (first from) (place-column piece)))
(set-place-piece! piece
(make-piece (first swap-in) (cond
[(odd? move-counter) 'white]
[else 'black])))]
[else void]))
x))
board))