commit f8fad902471361958668d7f16dc4e535b6d54e17 Author: nihilazo Date: Wed Mar 30 20:08:45 2022 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e0e3a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +compiled/* +*~ diff --git a/main.rkt b/main.rkt new file mode 100644 index 0000000..efd4105 --- /dev/null +++ b/main.rkt @@ -0,0 +1,95 @@ +#lang racket + +(require rackunit) + +; a Board is a list of lines. + +; represents a point in 2d space. +(struct point (x y) #:transparent) + +; represents a line drawn from one point to another and which player drew it. +(struct line (from to player) #:transparent) + +; width and height of the playing grid. +(define GRID-WIDTH 6) +(define GRID-HEIGHT 6) + +; Board -> Image +; renders the game board. +(define (draw board) board) + +; Line Line -> Bool +; tests if two lines are in the same position +(define (same-position? l1 l2) + (or + (and + (equal? (line-from l1) (line-from l2)) + (equal? (line-to l1) (line-to l2))) + (and + (equal? (line-to l1) (line-from l2)) + (equal? (line-from l1) (line-to l2))))) + +(test-case + "same-position?" + (check-true (same-position? (line (point 0 0) (point 0 1) 1) + (line (point 0 0) (point 0 1) 0))) + (check-true (same-position? (line (point 0 0) (point 0 1) 1) + (line (point 0 1) (point 0 0) 0))) + (check-true (same-position? (line (point 0 2) (point 0 1) 0) + (line (point 0 2) (point 0 1) 0))) + (check-false (same-position? (line (point 0 0) (point 0 1) 1) + (line (point 2 4) (point 0 3) 0)))) + + + +; Line -> Bool +; returns #t if a line is valid in terms of length (only moving one point in one direction) +(define (valid-length? line) + (= 1 + (+ (abs (- (point-x (line-from line)) (point-x (line-to line)))) + (abs (- (point-y (line-from line)) (point-y (line-to line))))))) + +(test-case + "valid-length?" + (check-true (valid-length? (line (point 0 0) (point 0 1) 1))) + (check-true (valid-length? (line (point 0 1) (point 0 0) 1))) + (check-false (valid-length? (line (point 0 0) (point 1 1) 0))) + (check-false (valid-length? (line (point 0 0) (point 0 2) 1))) + (check-false (valid-length? (line (point 0 0) (point 0 2) 1)))) + +; Point -> Bool +; returns #t if a point is out of bounds (off the edge of the grid) +(define (out-of-bounds? p) + (or + (> 0 (point-x p)) + (> 0 (point-y p)) + (< (- GRID-WIDTH 1) (point-x p)) + (< (- GRID-WIDTH 1) (point-y p)))) + +(test-case + "out-of-bounds?" + (check-false (out-of-bounds? (point 0 0))) + (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)))) + +; Line Board -> Bool +; returns #t if adding the given move to the board is valid. +(define (valid-move? line board) + (and + (empty? (filter (lambda (l) (same-position? l line)) board)) ; line doesn't already exist on board + (valid-length? line) + (and + (not (out-of-bounds? (line-from line))) + (not (out-of-bounds? (line-to line)))))) + +(test-case + "valid-move?" + (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 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 + ) \ No newline at end of file