Compare commits
3 Commits
914d2592a3
...
8fd5dd41b0
Author | SHA1 | Date |
---|---|---|
Nico | 8fd5dd41b0 | |
Nico | 672bfa1232 | |
Nico | 5cc2578820 |
|
@ -31,12 +31,13 @@
|
|||
|
||||
(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
|
||||
(define grid (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) grid))
|
||||
(check-true (valid-move? (line (point 0 2) (point 0 3) 0) grid)) ; not overwriting existing moves
|
||||
(check-false (valid-move? (line (point 0 2) (point 0 1) 1) grid)) ; moving forward
|
||||
(check-false (valid-move? (line (point 0 0) (point 0 2) 1) grid)) ; valid length check
|
||||
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) grid))
|
||||
(check-false (valid-move? (line (point (+ GRID-WIDTH 1) 0) (point 0 2) 1) grid))) ; out of bounds
|
||||
|
||||
(test-case
|
||||
"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))))
|
||||
|
|
55
main.rkt
55
main.rkt
|
@ -2,7 +2,9 @@
|
|||
|
||||
(provide (all-defined-out)) ; for testing module
|
||||
|
||||
; a Board is a list of lines.
|
||||
(require racket/draw)
|
||||
|
||||
; a Grid is a list of lines. All lines in a grid 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)
|
||||
|
@ -14,9 +16,18 @@
|
|||
(define GRID-WIDTH 6)
|
||||
(define GRID-HEIGHT 6)
|
||||
|
||||
; Board -> Image
|
||||
; renders the game board.
|
||||
(define (draw board) board)
|
||||
; size of one grid position in pixels
|
||||
(define GRID-SCALE 16)
|
||||
|
||||
; image representing the empty grid
|
||||
(define EMPTY-GRID
|
||||
(let* [[bitmap (make-bitmap (* GRID-WIDTH GRID-SCALE) (* GRID-HEIGHT GRID-SCALE))]
|
||||
[dc (new bitmap-dc% [bitmap bitmap])]]
|
||||
(send dc set-brush "black" 'solid)
|
||||
(for ([x (in-range GRID-WIDTH)])
|
||||
(for ([y (in-range GRID-HEIGHT)])
|
||||
(send dc draw-ellipse (* x GRID-SCALE) (* y GRID-SCALE) (/ GRID-SCALE 2) (/ GRID-SCALE 2))))
|
||||
bitmap))
|
||||
|
||||
; Line Line -> Bool
|
||||
; tests if two lines are in the same position
|
||||
|
@ -29,6 +40,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)
|
||||
|
@ -45,12 +63,31 @@
|
|||
(< (- 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)
|
||||
; Line Grid -> Bool
|
||||
; returns #t if adding the given move to the grid is valid.
|
||||
(define (valid-move? line grid)
|
||||
(and
|
||||
(empty? (filter (lambda (l) (same-position? l line)) board)) ; line doesn't already exist on board
|
||||
(empty? (filter (lambda (l) (same-position? l line)) grid)) ; 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))))))
|
||||
|
||||
; Grid -> Image
|
||||
; renders grid to an image for showing humans the game.
|
||||
(define (render-grid grid)
|
||||
(let* [[bitmap (make-bitmap (* GRID-WIDTH GRID-SCALE) (* GRID-HEIGHT GRID-SCALE))]
|
||||
[dc (new bitmap-dc% [bitmap bitmap])]]
|
||||
(for ([l grid])
|
||||
(cond
|
||||
[(= (line-player l) 0) (send dc set-pen "red" 4 'solid)] ; player 1 draws in red
|
||||
[(= (line-player l) 1) (send dc set-pen "blue" 4 'solid)]) ; player 2 draws in blue
|
||||
(send dc draw-line
|
||||
(+ 4 (* GRID-SCALE (point-x (line-from l))))
|
||||
(+ 4 (* GRID-SCALE (point-y (line-from l))))
|
||||
(+ 4 (* GRID-SCALE (point-x (line-to l))))
|
||||
(+ 4 (* GRID-SCALE (point-y (line-to l)))))) ; draw the line
|
||||
|
||||
(send dc draw-bitmap EMPTY-GRID 0 0) ; overlay dot grid
|
||||
bitmap))
|
Loading…
Reference in New Issue