initial commit

This commit is contained in:
Nico 2022-03-30 20:08:45 +01:00
commit f8fad90247
2 changed files with 97 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
compiled/*
*~

95
main.rkt Normal file
View File

@ -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
)