56 lines
1.6 KiB
Racket
56 lines
1.6 KiB
Racket
#lang racket
|
|
|
|
(provide (all-defined-out)) ; for testing module
|
|
|
|
; 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)))))
|
|
|
|
; 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)))))))
|
|
|
|
; 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))))
|
|
|
|
; 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)))))) |