2022 Review 2

  1. What is the difference between for/list and for*/list? Give an example and explain in a sentence.

A traditional checkerboard has alternating red and black squares. It is oriented so the red (“light”) square is on the near right from each player’s perspective. Pieces only go on the black (“dark”) squares. The squares I use are 50x50.

  1. Use for/fold to construct the checkerboard.

You will make a model using these structures:

;; Piece is a structure containing:
;; pos: a Posn, the board coordinates of the center of the piece
;; team: a number, 0 or 1
(define-struct piece (pos team))
;; M is a structure containing:
;; pieces: a list of Piece, the current state of the board
;; selected: a Posn, the square that is selected, or (-1,-1) for no selection
(define-struct m (pieces selected))

If they print out in a way that you cannot see what is inside, like this: (make-m ...), add the #:prefab tag you have been using for messages.. You could use #:transparent instead, it prints out a little nicer.

  1. Use for/list to create a list of all of the checkers. Here is an example of a black checker at (x=2, y=1): (make-piece (make-posn 2 1) 1). See the diagram below for checker positions.

  2. Write a draw handler that takes in an m and produces an Image. As before, use for/fold.

  3. Write the move-checker function that takes in a list of pieces, a start posn, and an end posn. It should move all of the pieces located at the start posn to the end posn. Use for/list.

  4. (Coordinate conversions) Write functions that convert computer (x,y) coordinates to board (x,y) coordinates and vice-versa. Since my squares are 50x50, any mouse click with $0 \le x < 50$ and $0\le y< 50$ is considered $(0,0)$ on the board. Another example is that that $(35, 80)$ maps to board coordinates $(1,2)$. You can do this your own

    invented way, but make sure clicking off-center still works the same as clicking in the exact center of a square.

     (check-expect (c->b (make-posn 49 48)) (make-posn 0 0))
     (check-expect (c->b (make-posn 35 80)) (make-posn 0 1))
     (check-expect (b->c (make-posn 0 0)) (make-posn 25 25))
     (check-expect (b->c (make-posn 1 2)) (make-posn 75 125))
    
  5. (Mouse Handler) When the selected field is (make-posn -1 -1), the mouse handler sets the selected field to be the center of the square clicked on. When the selected field is not (-1, -1), then the mouse handler moves the piece using move-checker.

     (check-expect
       (mouse-h (make-m (list (make-piece (make-posn 1 3) 1)
                              (make-piece (make-posn 1 5) 0))
                           (make-posn -1 -1))
                  80 210
                  "button-down")
        (make-m (list (make-piece (make-posn 1 3) 1)
                      (make-piee (make-posn 1 5) 0))
                  (make-posn 1 4)))
    
    
     (check-expect
       (mouse-h (make-m (list (make-piece (make-posn 1 3) 1)
                              (make-piece (make-posn 1 5) 0))
                           (make-posn 1 4))
                  80 210
                  "button-down")
        (make-m (list (make-piece (make-posn 1 3) 1)
                      (make-piee (make-posn 1 5) 0))
                  (make-posn 1 4)))
    
  6. (Big Bang) Put together your animation and verify that you can move the pieces. You are not asked to follow any rules about what is a legal move.

Last modified August 18, 2023: 2022-2023 End State (7352e87)