Question

I have been working on a simple chess program in Scheme, and one of the helper functions that I have defined consumes a piece, and its coordinates (current-location) and places it at specified coordinates (move-here) on the chess board, switching out any piece that may be located at the Move-here coordinates. The function was working as I had hoped, yet now for whatever reason it is no longer functioning properly. I have no idea what could be causing this, and have been tracing and re-tracing the code for a while now trying to find the bug. I am hoping someone may be able to shed light on the situation.

Here is the code for the piece-switching function, along with the structures used within the code:

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

Here are two examples; the first of which is what it outputs, and the second is what it should output (in the example, there is a small modification to Swap-piece so that one of its parameters is a board, as to not use the entire array I have for my 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 a 4) '(a 7) Example-board)
(shared ((-2- void)) (list (list -2- (void)) (list (void) -2-)))

So I call Example Board to get the 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, the output I 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, for the long post, however I just cannot figure out what is causing this to happen. As I said, I am sure this code was working just a few hours ago.

EDIT: I should add that the list, board, that the map function is acting on in my Swap-piece function is the chessboard.

Was it helpful?

Solution

I realised what was causing the incorrect piece placing/swapping. I had the swap part backwards, almost surely because I labelled my parameters incorrectly. My code basically said: "If the coordinates of the square I'm looking at are equal to the coordinates of the piece I am placing, don't change the square", which defeats the purpose of moving a piece, obviously. For anyone who is interested, the proper code is:

(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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top