2022 Review 4
-
(Knight Moves.) Given the position of a knight, return a list of all of the legal positions the knight can occupy. Number the board coordinates 0 through 7 for both x and y.
;; knight: x(number) y(number) -> (Listof Posn)
-
(Pawn Push.) In a simplified version of chess, a pawn can move forward unless there is a piece directly in front of it. When they move, “white” pawns increase y by 1 and “black” pawns decrease y by 1.
(define-struct pawn (team pos))
You are given a list of posns containing the occupied squares on a chess board, and a list of Pawns. You produce a new list by advancing every pawn that you can on a given team. The occupied squares list is just supposed to make the coding easier.
;; pawn-push: (Listof Posn) Team (Listof Pawn) ;; -> (Listof Pawn) (define WHITE 1) (define BLACK -1) (check-expect (pawn-push (list (make-posn 3 2) (make-posn 3 5)) BLACK (list (make-pawn BLACK (make-posn 3 5)) (make-pawn WHITE (make-posn 3 2)))) (list (make-pawn BLACK (make-posn 3 4)) (make-pawn WHITE (make-posn 3 2))))
-
(Checkers King Jump.) Given a list of pieces, one piece to move, and ending coordinates (x,y), return true if the move is legal for a king in checkers, false otherwise. A king can move in any direction. Do not consider multiple jumps. Team is either 1 or -1 (representing red or black). Helper functions are encouraged.
(define-struct piece (team pos)) ;; team is a number 1 or -1 ;; x and y are numbers 0 through 7 ;; checkers-move? : (Listof Piece) Piece Posn ;; -> boolean ;; Here the posn (x,y) is the destination.
For a move to be legal:
- Destination square is empty.
- Destination square is on the board.
- Destination square is not occupied by a piece.
- Either:
- Destination square is 1 diagonal step from the original; or
- Destination square is 2 diagonal steps from the original, and there is a piece from the opposing team in between.
Tests
(require picturing-programs)
(define-struct piece (team pos))
(define RED 1)
(define BLACK -1)
(define BOARD
(list (make-piece BLACK (make-posn 2 3))
(make-piece RED (make-posn 3 4))
(make-piece BLACK (make-posn 4 3))
(make-piece RED (make-posn 5 4))
(make-piece RED (make-posn 6 3))
(make-piece BLACK (make-posn 7 2))
(make-piece BLACK (make-posn 5 2))
(make-piece BLACK (make-posn 4 1))))
(define the-p (make-piece RED (make-posn 3 4)))
(check-expect (checkers-move? BOARD the-p
(make-posn 2 5))
true)
(check-expect (checkers-move? BOARD the-p
(make-posn 2 3))
false) ;; occupied
(check-expect (checkers-move? BOARD the-p
(make-posn 1 2))
true) ;; jump
(check-expect (checkers-move? BOARD the-p
(make-posn 0 1))
false) ;; too far
(check-expect (checkers-move? BOARD the-p
(make-posn 5 6))
false) ;; again, too far
(define (checkers-move? board piece dest)
false)