Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-advanced-reader.ss" "lang")((modname |32.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;A solitaire board (board) is a (vectorof (vectorof booleans)), where true represents a filled square, false an empty square. In particular, for any given board, the n-th element is a vector containing n booleans. We call a triangular board with n-pegs to a side a size-n triangle. For example, the representation of a size-4 equilaterial triangular board:
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;(vector (vector false)
7 12687dd9 2023-08-04 jrmu ; (vector true true)
8 12687dd9 2023-08-04 jrmu ; (vector true true true)
9 12687dd9 2023-08-04 jrmu ; (vector true true true true))
10 12687dd9 2023-08-04 jrmu
11 12687dd9 2023-08-04 jrmu ;enabled? : posn board -> boolean
12 12687dd9 2023-08-04 jrmu ;Determines whether or not the peg specified by aposn is enabled in aboard (enabled means that the peg is capable of jumping). The top position of the board has the index (make-posn 1 1). The vertical axis is denoted by y, the horizontal by x.
13 12687dd9 2023-08-04 jrmu
14 12687dd9 2023-08-04 jrmu ;next-peg-x represents the x-index where the peg will jump to if it jumps to the right
15 12687dd9 2023-08-04 jrmu ;next-peg-y represents the y-index where the peg will jump to if it jumps down
16 12687dd9 2023-08-04 jrmu ;prev-peg-x represents the x-index ... if the peg jumps to the left
17 12687dd9 2023-08-04 jrmu ;prev-peg-x represents the y-index ... if the peg jumps up
18 12687dd9 2023-08-04 jrmu ;need-length-x represents the necessary horizontal length of the board to jump to the right
19 12687dd9 2023-08-04 jrmu ;need-length-y represents the necessary vertical length of the board to jump down
20 12687dd9 2023-08-04 jrmu ;next-peg-x-removes represents the x-index of the peg that will be removed if the peg is jumped, etc.
21 12687dd9 2023-08-04 jrmu
22 12687dd9 2023-08-04 jrmu (define (enabled? aposn aboard)
23 12687dd9 2023-08-04 jrmu (local ((define peg-x (sub1 (posn-x aposn)))
24 12687dd9 2023-08-04 jrmu (define peg-y (sub1 (posn-y aposn)))
25 12687dd9 2023-08-04 jrmu (define next-peg-x-removes (+ peg-x 1))
26 12687dd9 2023-08-04 jrmu (define next-peg-y-removes (+ peg-y 1))
27 12687dd9 2023-08-04 jrmu (define prev-peg-x-removes (- peg-x 1))
28 12687dd9 2023-08-04 jrmu (define prev-peg-y-removes (- peg-y 1))
29 12687dd9 2023-08-04 jrmu (define next-peg-x (+ peg-x 2))
30 12687dd9 2023-08-04 jrmu (define next-peg-y (+ peg-y 2))
31 12687dd9 2023-08-04 jrmu (define prev-peg-x (- peg-x 2))
32 12687dd9 2023-08-04 jrmu (define prev-peg-y (- peg-y 2))
33 12687dd9 2023-08-04 jrmu (define need-length-x (+ peg-x 3))
34 12687dd9 2023-08-04 jrmu (define need-length-y (+ peg-y 3)))
35 12687dd9 2023-08-04 jrmu (cond
36 12687dd9 2023-08-04 jrmu ;;is there even a peg?
37 12687dd9 2023-08-04 jrmu [(not (vector-ref (vector-ref aboard peg-y) peg-x)) false]
38 12687dd9 2023-08-04 jrmu ;;jumping down
39 12687dd9 2023-08-04 jrmu [(and (>= (vector-length aboard) need-length-y)
40 12687dd9 2023-08-04 jrmu (not (vector-ref (vector-ref aboard next-peg-y) peg-x))
41 12687dd9 2023-08-04 jrmu (vector-ref (vector-ref aboard next-peg-y-removes) peg-x)) true]
42 12687dd9 2023-08-04 jrmu ;;jumping right
43 12687dd9 2023-08-04 jrmu [(and (>= (vector-length (vector-ref aboard peg-y)) need-length-x)
44 12687dd9 2023-08-04 jrmu (not (vector-ref (vector-ref aboard peg-y) next-peg-x))
45 12687dd9 2023-08-04 jrmu (vector-ref (vector-ref aboard peg-y) next-peg-x-removes)) true]
46 12687dd9 2023-08-04 jrmu ;;jumping up
47 12687dd9 2023-08-04 jrmu [(and (not (negative? prev-peg-y))
48 12687dd9 2023-08-04 jrmu (not (vector-ref (vector-ref aboard prev-peg-y) peg-x))
49 12687dd9 2023-08-04 jrmu (vector-ref (vector-ref aboard prev-peg-y-removes) peg-x)) true]
50 12687dd9 2023-08-04 jrmu ;;jumping left
51 12687dd9 2023-08-04 jrmu [(and (not (negative? prev-peg-x))
52 12687dd9 2023-08-04 jrmu (not (vector-ref (vector-ref aboard peg-y) prev-peg-x))
53 12687dd9 2023-08-04 jrmu (vector-ref (vector-ref aboard peg-y) prev-peg-x-removes)) true]
54 12687dd9 2023-08-04 jrmu ;;jumping diagonal (up)
55 12687dd9 2023-08-04 jrmu [(and (not (negative? prev-peg-x))
56 12687dd9 2023-08-04 jrmu (not (negative? prev-peg-y))
57 12687dd9 2023-08-04 jrmu (not (vector-ref (vector-ref aboard prev-peg-y) prev-peg-x))
58 12687dd9 2023-08-04 jrmu (vector-ref (vector-ref aboard prev-peg-y-removes) prev-peg-x-removes)) true]
59 12687dd9 2023-08-04 jrmu ;;jumping diagonal (down)
60 12687dd9 2023-08-04 jrmu [(and (>= (vector-length aboard) need-length-y)
61 12687dd9 2023-08-04 jrmu (>= (vector-length (vector-ref aboard next-peg-y)) need-length-x)
62 12687dd9 2023-08-04 jrmu (not (vector-ref (vector-ref aboard next-peg-y) next-peg-x))
63 12687dd9 2023-08-04 jrmu (vector-ref (vector-ref aboard next-peg-y-removes) next-peg-x-removes)) true]
64 12687dd9 2023-08-04 jrmu [else false])))