2014-02-06 5 views
0

나는 Scheme에서 간단한 체스 프로그램을 작업 해왔고, 내가 정의한 도우미 함수 중 하나가 조각과 좌표 (current-location)를 소비하고 체스 보드에서 지정된 좌표 (move-here)를 사용하여 Move-here 좌표에있는 조각을 전환합니다. 이 기능은 내가 원하는 바대로 작동했지만, 이제는 더 이상 제대로 작동하지 않습니다. 나는 이것이 무엇을 일으킬 수 있는지 전혀 모르고 있으며, 버그를 찾으려고 잠시 동안 코드를 추적하고 추적하고있다. 나는 누군가가 상황을 밝힐 수 있기를 희망하고있다. 여기 Scheme : 체스를위한 배열에서 요소를 돌연변이

코드 내에서 사용되는 구조와 함께, 부분 스위칭 기능에 대한 코드는 다음

(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)) 

여기 두 가지 예이다; 첫 번째 출력은 무엇이며, 두 번째 출력은 입니다 (예에서 스왑 조각에 작은 수정이있어서 그 매개 변수 중 하나가 보드이므로 전체 배열을 사용하지 않습니다. 내 체스 보드를 가지고).

> 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)))) 

죄송합니다, 긴 게시물에 대한,하지만 난 그냥

(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-))) 

은 그래서 예 보드가 업데이트 된 보드를 얻기 위해 전화 이것이 일어나는 원인을 파악할 수 없습니다. 앞에서 말했듯이이 코드는 불과 몇 시간 전만해도 작동한다고 확신합니다.

편집 :지도 기능이 작동하는 목록 인 보드를 스왑 피스 기능이 체스 판이라고 추가해야합니다.

+0

가능한 중복 (http://stackoverflow.com/questions/21592853/scheme-function-that-stopped-working) – msandiford

+0

사실, 이것은 마이그레이션 된 x- 게시물입니다. – jozefg

+0

@jozefg 예, 그렇습니다. 나는 프로그래머들에게도 그것을 게시했다. 그것이 어디 있어야하는지 몰랐다. – Plopperzz

답변

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)) 
[작동이 중지 계획 기능]의