From 5cc25788204db6de5ce9b1f0cd557fd74b4a531c Mon Sep 17 00:00:00 2001 From: nihilazo Date: Thu, 31 Mar 2022 12:18:48 +0100 Subject: [PATCH] add forwards? check --- main-test.rkt | 9 ++++++++- main.rkt | 12 ++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/main-test.rkt b/main-test.rkt index 11bb49d..3395164 100644 --- a/main-test.rkt +++ b/main-test.rkt @@ -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)))) \ No newline at end of file + (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)))) \ No newline at end of file diff --git a/main.rkt b/main.rkt index e4a238e..92853d9 100644 --- a/main.rkt +++ b/main.rkt @@ -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)))))) \ No newline at end of file + (not (out-of-bounds? (line-to line))))))