1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #reader(lib "" "lang")((modname |32.3|) (read-case-sensitive #t) (teachpacks ((lib "" "teachpack" "htdp") (lib "" "teachpack" "htdp") (lib "" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "" "teachpack" "htdp") (lib "" "teachpack" "htdp") (lib "" "teachpack" "htdp")))))
4 ;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:
6 ;(vector (vector false)
7 ; (vector true true)
8 ; (vector true true true)
9 ; (vector true true true true))
11 ;enabled? : posn board -> boolean
12 ;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.
14 ;next-peg-x represents the x-index where the peg will jump to if it jumps to the right
15 ;next-peg-y represents the y-index where the peg will jump to if it jumps down
16 ;prev-peg-x represents the x-index ... if the peg jumps to the left
17 ;prev-peg-x represents the y-index ... if the peg jumps up
18 ;need-length-x represents the necessary horizontal length of the board to jump to the right
19 ;need-length-y represents the necessary vertical length of the board to jump down
20 ;next-peg-x-removes represents the x-index of the peg that will be removed if the peg is jumped, etc.
22 ;the parameters for the position of the new peg as well as the peg to be removed are passed to jump?.
24 (define (enabled? aposn aboard)
25 (local ((define peg-x (sub1 (posn-x aposn)))
26 (define peg-y (sub1 (posn-y aposn)))
27 (define next-peg-x-removes (+ peg-x 1))
28 (define next-peg-y-removes (+ peg-y 1))
29 (define prev-peg-x-removes (- peg-x 1))
30 (define prev-peg-y-removes (- peg-y 1))
31 (define next-peg-x (+ peg-x 2))
32 (define next-peg-y (+ peg-y 2))
33 (define prev-peg-x (- peg-x 2))
34 (define prev-peg-y (- peg-y 2)))
35 (cond
36 ;;first, is there even a peg?
37 ;;if so, test jump down, jump right, jump up, jump left, jump diagonal (up), jump diagonal (down), in that order
38 [(vector-ref (vector-ref aboard peg-y) peg-x)
39 (or (jump? peg-x next-peg-y peg-x next-peg-y-removes aboard)
40 (jump? next-peg-x peg-y next-peg-x-removes peg-y aboard)
41 (jump? peg-x prev-peg-y peg-x prev-peg-y-removes aboard)
42 (jump? prev-peg-x peg-y prev-peg-x-removes peg-y aboard)
43 (jump? prev-peg-x prev-peg-y prev-peg-x-removes prev-peg-y-removes aboard)
44 (jump? next-peg-x next-peg-y next-peg-x-removes next-peg-y-removes aboard))]
45 [else false])))
47 ;jump? : N N N N board-> boolean
48 ;Given new-peg-x, new-peg-y, remove-peg-x, remove-peg-y, and aboard, determine if it is possible to make a jump. All coordinates are in terms of vector indices. E.g, 0,0 represents the first element in the first vector.
50 (define (jump? new-peg-x new-peg-y remove-peg-x remove-peg-y aboard)
51 (local ((define need-length-x (add1 new-peg-x))
52 (define need-length-y (add1 new-peg-y)))
53 (and (not (negative? new-peg-x))
54 (not (negative? new-peg-y))
55 (>= (vector-length aboard) need-length-y)
56 (>= (vector-length (vector-ref aboard new-peg-y)) need-length-x)
57 (not (vector-ref (vector-ref aboard new-peg-y) new-peg-x))
58 (vector-ref (vector-ref aboard remove-peg-y) remove-peg-x))))
60 ;next-board : board posn -> (listof boards) or false
61 ;Given aboard and aposn, return all possible board configurations if the peg specified by aposn is jumped. (since there may be more than one possible jump, we must list the results as a listof boards)
63 (define (next-board aboard aposn)
64 (cond
65 [(enabled? aposn aboard)
66 (append (jump aboard aposn) ;down
67 (jump aboard aposn) ;right
68 (jump aboard aposn) ;up
69 (jump-left aboard aposn) ;left
70 (jump-diagonal-up aboard aposn) ;diagonal-up
71 (jump-diagonal-down aboard aposn))] ;diagonal-down
72 [else false]))
74 jump : board posn posn -> board
75 Using aboard, return a new board with old-peg replaced with false and new-peg replaced with true. The intermediate peg is removed.
77 (define (jump aboard new-peg old-peg)
78 (cond))