Scheme: Mutating elements in an array for chess -


i have been working on simple chess program in scheme, , 1 of helper functions have defined consumes piece, , coordinates (current-location) , places @ specified coordinates (move-here) on chess board, switching out piece may located @ move-here coordinates. function working had hoped, yet whatever reason no longer functioning properly. have no idea causing this, , have been tracing , re-tracing code while trying find bug. hoping may able shed light on situation.

here code piece-switching function, along structures used within code:

(define-struct place (row column piece)) ;; place structure (make-place r c p) r rank of piece ;; symbol of 'pawn, 'rook, 'knight, 'bishop, 'king or 'queen ;; , column , place. c , p give placement coordinates, ;; column 1 symbol '(a b c d e f g h) , row number  ;; 1 - 8 inclusive.  (define-struct piece (rank color)) ;; piece structure (make-piece r col) rank described place   structure,  ;; , colour symbo either 'black or 'white.  (define move-counter 1) ; keeps track of current number of mves made. ;; odd indicates white move, else black move.  ;; swap-in: '(symbol symbol nat) '(symbol nat) -> '(-2- void) ;; conditions: ;;   pre: swap-in '(rank column row) '(column row) ;;   post: produces list of lists containing -2- , void. void represents ;;         changed values. ;; purpose: swap-piece helper x-move function dictates legal ;;          moves of 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)) 

here 2 examples; first of outputs, , second should output (in example, there small modification swap-piece 1 of parameters board, not use entire array have chess board).

(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 4) '(a 7) example-board) (shared ((-2- void)) (list (list -2- (void)) (list (void) -2-))) 

so call example board updated 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)))) 

however, output expect is:

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

sorry, long post, cannot figure out causing happen. said, sure code working few hours ago.

edit: should add list, board, map function acting on in swap-piece function chessboard.

i realised causing incorrect piece placing/swapping. had swap part backwards, surely because labelled parameters incorrectly. code said: "if coordinates of square i'm looking @ equal coordinates of piece placing, don't change square", defeats purpose of moving piece, obviously. interested, proper code is:

(define (swap-piece swap-in from)   (map (λ (x)          (map (λ (piece)                 (cond                    ; comparing coordinates of current square swap-in                   [(and (= (third swap-in) (place-row piece))                          (symbol=? (second swap-in) (place-column piece)))                    ; if equal place, remove swap-in square                    (set-place-piece! piece (make-piece empty empty))]                    ; if coordinates of square equal coordinates of                    ; place 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)) 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -