diff --git a/main-test.rkt b/main-test.rkt index 67e5a4e..5760fea 100644 --- a/main-test.rkt +++ b/main-test.rkt @@ -2,7 +2,7 @@ (require rackunit "main.rkt") -(define grid (list +(define test-grid (grid (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 1 1) 0) (line (point 1 0) (point 1 1) 1) @@ -13,7 +13,7 @@ (line (point 3 3) (point 3 4) 0) (line (point 3 3) (point 4 3) 0) (line (point 4 3) (point 4 4) 0) - (line (point 3 4) (point 4 4) 0))) + (line (point 3 4) (point 4 4) 0)) 6 6)) (test-case "same-position?" @@ -47,14 +47,14 @@ (check-equal? (boxes-for (line (point 0 0) (point 0 1) 1)) (list (point 0 0))) (check-equal? (boxes-for (line (point 2 1) (point 2 2) 1)) (list (point 2 1) (point 1 1))) (check-equal? (boxes-for (line (point 0 0) (point 1 0) 1)) (list (point 0 0))) - (check-equal? (boxes-for (line (point 1 1) (point 2 1) 1)) (list (point 1 1) (point 1 0))) + (check-equal? (boxes-for (line (point 1 1) (point 2 1) 1)) (list (point 1 1) (point 1 0))) (check-equal? (boxes-for (line (point 0 5) (point 1 5) 1)) (list (point 0 4))) (check-equal? (boxes-for (line (point 5 0) (point 5 1) 0)) (list (point 4 0)))) (test-case "completes-boxes" - (define grid - (list + (define test-grid + (grid (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 1 1) 0) (line (point 1 0) (point 1 1) 1) @@ -72,26 +72,26 @@ (line (point 3 4) (point 4 4) 1) (line (point 4 5) (point 5 5) 1) (line (point 4 4) (point 5 4) 0) - (line (point 5 4) (point 5 5) 1))) ; all possible unfinished boxes (plus an unfinished double) - (check-equal? (completes-boxes grid (line (point 0 0) (point 1 0) 0)) 1) - (check-equal? (completes-boxes grid (line (point 1 2) (point 1 3) 0)) 1) - (check-equal? (completes-boxes grid (line (point 3 1) (point 4 1) 0)) 1) - (check-equal? (completes-boxes grid (line (point 0 5) (point 1 5) 0)) 1) - (check-equal? (completes-boxes grid (line (point 4 4) (point 4 5) 0)) 2) - (check-equal? (completes-boxes grid (line (point 1 0) (point 2 0) 1)) 0)) + (line (point 5 4) (point 5 5) 1)) 6 6)) ; all possible unfinished boxes (plus an unfinished double) + (check-equal? (completes-boxes test-grid (line (point 0 0) (point 1 0) 0)) 1) + (check-equal? (completes-boxes test-grid (line (point 1 2) (point 1 3) 0)) 1) + (check-equal? (completes-boxes test-grid (line (point 3 1) (point 4 1) 0)) 1) + (check-equal? (completes-boxes test-grid (line (point 0 5) (point 1 5) 0)) 1) + (check-equal? (completes-boxes test-grid (line (point 4 4) (point 4 5) 0)) 2) + (check-equal? (completes-boxes test-grid (line (point 1 0) (point 2 0) 1)) 0)) (test-case "valid-move?" - (define grid (list + (define test-grid (grid (list (line (point 0 0) (point 0 1) 1) (line (point 0 1) (point 0 2) 0) - (line (point 3 2) (point 3 3) 0))) - (check-false (valid-move? (line (point 0 0) (point 0 1) 1) grid)) - (check-true (valid-move? (line (point 0 2) (point 0 3) 0) grid)) ; not overwriting existing moves - (check-false (valid-move? (line (point 0 2) (point 0 1) 1) grid)) ; moving forward - (check-false (valid-move? (line (point 0 0) (point 0 2) 1) grid)) ; valid length check - (check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) grid)) - (check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) grid))) ; out of bounds + (line (point 3 2) (point 3 3) 0)) 6 6)) + (check-false (valid-move? (line (point 0 0) (point 0 1) 1) test-grid)) + (check-true (valid-move? (line (point 0 2) (point 0 3) 0) test-grid)) ; not overwriting existing moves + (check-false (valid-move? (line (point 0 2) (point 0 1) 1) test-grid)) ; moving forward + (check-false (valid-move? (line (point 0 0) (point 0 2) 1) test-grid)) ; valid length check + (check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) test-grid)) + (check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) test-grid))) ; out of bounds (test-case "out-of-bounds?" @@ -123,13 +123,16 @@ (test-case "append-move" - (check-equal? (append-move grid (line (point 2 2) (point 2 3) 0)) (append grid (list (line (point 2 2) (point 2 3) 0)))) - (check-equal? (append-move grid (line (point 0 0) (point 0 1) 0)) grid)) + (check-equal? (append-move test-grid (line (point 2 2) (point 2 3) 0)) (grid (append + (grid-lines test-grid) + (list (line (point 2 2) (point 2 3) 0))) + (grid-width test-grid) (grid-height test-grid))) + (check-equal? (append-move test-grid (line (point 0 0) (point 0 1) 0)) test-grid)) (test-case "valid-moves" - (check-equal? (valid-moves grid) (list + (check-equal? (valid-moves test-grid) (list (line (point 0 1) (point 0 2) 1) (line (point 0 2) (point 1 2) 1) (line (point 0 2) (point 0 3) 1) diff --git a/main.rkt b/main.rkt index e5e9b94..3b82a97 100644 --- a/main.rkt +++ b/main.rkt @@ -4,7 +4,10 @@ (require racket/draw racket/random) -; a Grid is a list of lines. All lines in a grid must be specified as moving left to right, top to bottom ("to" coordinate higher than "from" coordinate") +; a Grid contains: +; -- a list of lines. All lines in a grid must be specified as moving left to right, top to bottom ("to" coordinate higher than "from" coordinate") +; -- the width and height of the grid. +(struct grid (lines width height) #:transparent) ; represents a point in 2d space. (struct point (x y) #:transparent) @@ -36,12 +39,13 @@ bitmap)) ; Grid that contains all possible lines filled. Used for filtering valid moves. This is an ugly way, but i can't think of a better one right now. -(define ALL-LINES (flatten +(define ALL-LINES (grid + (flatten (for/list ([x (in-range GRID-WIDTH)]) (for/list ([y (in-range GRID-HEIGHT)]) (list (if (< x (- GRID-WIDTH 1)) (line (point x y) (point (+ x 1) y) 1) '()) - (if (< y (- GRID-HEIGHT 1)) (line (point x y) (point x (+ y 1)) 1) '())))))) + (if (< y (- GRID-HEIGHT 1)) (line (point x y) (point x (+ y 1)) 1) '()))))) GRID-WIDTH GRID-HEIGHT)) ; Line Line -> Bool ; tests if two lines are in the same position @@ -64,7 +68,7 @@ ; TODO optimise. This "filtering what is invalid out of all possible lines" method sucks. ; TODO write more tests (define (valid-moves g) - (filter (lambda (move) (valid-move? move g)) ALL-LINES)) + (filter (lambda (move) (valid-move? move (grid-lines g))) ALL-LINES)) ; Point Grid -> Number ; given a point that is the top-left corner of a square on the grid, returns the amount of edges surrounding that square. @@ -77,7 +81,7 @@ (member #t (for/list ([x (list (point 0 0) (point 0 1) (point 1 0) (point 0 0))] [y (list (point 0 1) (point 1 1) (point 1 1) (point 1 0))]) - (same-position? item (line (point+ x p) (point+ y p) 0))))) g))) ; for every position in the square, check if there's a line there + (same-position? item (line (point+ x p) (point+ y p) 0))))) (grid-lines g)))) ; for every position in the square, check if there's a line there ; Line -> Bool ; returns #t if the line is forwards (with forwards being moving right and down, higher end coord than start) @@ -106,7 +110,7 @@ ; returns #t if adding the given move to the grid is valid. (define (valid-move? line grid) (and - (empty? (filter (lambda (l) (same-position? l line)) grid)) ; line doesn't already exist on board + (empty? (filter (lambda (l) (same-position? l line)) (grid-lines grid))) ; line doesn't already exist on board (valid-length? line) (forwards? line) (and @@ -125,7 +129,7 @@ ; Grid Line -> Grid ; adds line to grid, if it is a valid move. Otherwise skips the move. (define (append-move g l) - (if (valid-move? l g) (append g (list l)) g)) + (if (valid-move? l g) (grid (append (grid-lines g) (list l)) (grid-width g) (grid-height g)) g)) ; Line -> List of Points ; returns the positions of boxes the given line affects, assuming a valid line. @@ -187,7 +191,7 @@ ; TODO color in squares (define (render-grid grid dc) (define passed '()) - (for ([l grid]) ; reversed for fill-drawing malarkey. + (for ([l (grid-lines grid)]) ; reversed for fill-drawing malarkey. (if (empty? passed) (set! passed (list l)) (set! passed (append passed (list l)))) (cond [(= (line-player l) 0) (send dc set-pen "red" 4 'solid)] ; player 1 draws in red