add forwards? check

This commit is contained in:
Nico 2022-03-31 12:18:48 +01:00
parent 914d2592a3
commit 5cc2578820
2 changed files with 18 additions and 3 deletions

View File

@ -34,6 +34,7 @@
(define board (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) board))
(check-true (valid-move? (line (point 0 2) (point 0 3) 0) board)) ; not overwriting existing moves
(check-false (valid-move? (line (point 0 2) (point 0 1) 1) board)) ; moving forward
(check-false (valid-move? (line (point 0 0) (point 0 2) 1) board)) ; valid length check
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) board))
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) board))) ; out of bounds
@ -44,4 +45,10 @@
(check-false (out-of-bounds? (point 0 (- GRID-WIDTH 1))))
(check-true (out-of-bounds? (point GRID-WIDTH 0)))
(check-true (out-of-bounds? (point -2 0)))
(check-true (out-of-bounds? (point 0 -2))))
(check-true (out-of-bounds? (point 0 -2))))
(test-case
"forwards?"
(check-true (forwards? (line (point 0 0) (point 0 1) 1)))
(check-true (forwards? (line (point 0 1) (point 1 1) 1)))
(check-false (forwards? (line (point 1 1) (point 0 0) 0))))

View File

@ -2,7 +2,7 @@
(provide (all-defined-out)) ; for testing module
; a Board is a list of lines.
; a Board is a list of lines. All lines in a board must be specified as moving left to right, top to bottom ("to" coordinate higher than "from" coordinate")
; represents a point in 2d space.
(struct point (x y) #:transparent)
@ -29,6 +29,13 @@
(equal? (line-to l1) (line-from l2))
(equal? (line-from l1) (line-to l2)))))
; Line -> Bool
; returns #t if the line is forwards (with forwards being moving right and down, higher end coord than start)
(define (forwards? line)
(and
(>= (point-x (line-to line)) (point-x (line-from line)))
(>= (point-y (line-to line)) (point-y (line-from line)))))
; Line -> Bool
; returns #t if a line is valid in terms of length (only moving one point in one direction)
(define (valid-length? line)
@ -51,6 +58,7 @@
(and
(empty? (filter (lambda (l) (same-position? l line)) board)) ; line doesn't already exist on board
(valid-length? line)
(forwards? line)
(and
(not (out-of-bounds? (line-from line)))
(not (out-of-bounds? (line-to line))))))
(not (out-of-bounds? (line-to line))))))