Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require "./snake_lib.rkt")
- ; a game is
- ;(define-struct game (snake food obstacles ticks))
- ; a direction is either
- ; - 'up
- ; - 'down
- ; - 'left
- ; - 'right
- ; If this type looks new to you, its just a symbol.
- ; That is ‘up is a symbol and “up” is a string.
- ; Symbols are like strings without spaces.
- ; a snake is
- ;(define-struct snake (heading segments))
- ; segments is either
- ; - (cons posn empty)
- ; - (cons posn segments)
- ; That is, segments is a non-empty list of posns.
- ; x-coordinates increase from 1 to board-length (inclusive) toward the right
- ; y-coordinates increase from 1 to board-length (inclusive) toward the top
- ; the default value for board-length is 50.
- ; food is either
- ; - empty
- ; - (cons posn food)
- ; That is, food is a list of posns.
- ; obstacles is either
- ; - empty
- ; - (cons posn obstacles)
- ; Obstacles is also a list of posns.
- ; add-food : game posn -> game
- ; Given a game and posn, returns a new game where food has been added
- ; at that posn.
- (define (add-food g p)
- (make-game (game-snake g)
- (cons p (game-food g))
- (game-obstacles g)
- (game-ticks g)))
- (check-expect
- (add-food (make-game (make-snake 'up (list (make-posn 1 2)))
- (list (make-posn 3 4))
- (list (make-posn 10 10)
- (make-posn 20 20))
- 5)
- (make-posn 6 7))
- (make-game (make-snake 'up (list (make-posn 1 2)))
- (list (make-posn 6 7) (make-posn 3 4))
- (list (make-posn 10 10)
- (make-posn 20 20))
- 5))
- ; change-direction : game direction -> game
- ; Given a game and direction, returns a new game where the snake
- ; is now headed in the provided direction.
- (define (change-direction g d)
- (make-game (make-snake d (snake-segments (game-snake g)))
- (game-food g)
- (game-obstacles g)
- (game-ticks g)))
- (check-expect
- (change-direction
- (make-game (make-snake 'down (list (make-posn 1 2)))
- (list (make-posn 3 4))
- empty
- 5)
- 'left)
- (make-game (make-snake 'left (list (make-posn 1 2)))
- (list (make-posn 3 4))
- empty
- 5))
- ; game-score : game -> nat
- ; Given a game, returns a score (as a number)
- (define (game-score g)
- (* 10 (round (* 10 (/ (* (length (snake-segments (game-snake g))) 100)
- (if
- (= 0 (game-ticks g))
- 1
- (game-ticks g)))))))
- ; no tests are provided for game-score because it is open-ended
- ; game-over? : game -> boolean
- ; Given a game, returns true if that snake has died and false otherwise.
- (define (game-over? g)
- (if (or (< (posn-x (first (snake-segments (game-snake g)))) 1)
- (> (posn-x (first (snake-segments (game-snake g)))) 50)
- (< (posn-y (first (snake-segments (game-snake g)))) 1)
- (> (posn-y (first (snake-segments (game-snake g)))) 50)
- (not (empty? (filter (λ (p)
- (if (and (= (posn-x (first (snake-segments (game-snake g)))) (posn-x p))
- (= (posn-y (first (snake-segments (game-snake g)))) (posn-y p)))
- #true
- #false))
- (game-obstacles g))))
- (not (empty? (filter (λ (p)
- (if (and (= (posn-x (first (snake-segments (game-snake g)))) (posn-x p))
- (= (posn-y (first (snake-segments (game-snake g)))) (posn-y p)))
- #true
- #false))
- (rest (snake-segments (game-snake g)))))))
- #true
- #false))
- (check-expect
- (game-over? (make-game (make-snake 'up (list (make-posn 1 1))) empty empty 5))
- false)
- (check-expect
- (game-over? (make-game (make-snake 'up (list (make-posn -1 1))) empty empty 5))
- true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement