SHARE
TWEET

Untitled

a guest Mar 19th, 2019 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (require "./snake_lib.rkt")
  2.  
  3. ; a game is
  4. ;(define-struct game (snake food obstacles ticks))
  5.  
  6. ; a direction is either
  7. ; - 'up
  8. ; - 'down
  9. ; - 'left
  10. ; - 'right
  11. ; If this type looks new to you, its just a symbol.
  12. ; That is ‘up is a symbol and “up” is a string.
  13. ; Symbols are like strings without spaces.
  14.  
  15.  
  16. ; a snake is
  17. ;(define-struct snake (heading segments))
  18.  
  19. ; segments is either
  20. ; - (cons posn empty)
  21. ; - (cons posn segments)
  22. ; That is, segments is a non-empty list of posns.
  23. ; x-coordinates increase from 1 to board-length (inclusive) toward the right
  24. ; y-coordinates increase from 1 to board-length (inclusive) toward the top
  25. ; the default value for board-length is 50.
  26.  
  27. ; food is either
  28. ; - empty
  29. ; - (cons posn food)
  30. ; That is, food is a list of posns.
  31.  
  32. ; obstacles is either
  33. ; - empty
  34. ; - (cons posn obstacles)
  35. ; Obstacles is also a list of posns.
  36.  
  37. ; add-food : game posn -> game
  38. ; Given a game and posn, returns a new game where food has been added
  39. ;   at that posn.
  40. (define (add-food g p)
  41.   (make-game (game-snake g)
  42.              (cons p (game-food g))
  43.              (game-obstacles g)
  44.              (game-ticks g)))
  45.  
  46. (check-expect
  47.  (add-food (make-game (make-snake 'up (list (make-posn 1 2)))
  48.                       (list (make-posn 3 4))
  49.                       (list (make-posn 10 10)
  50.                             (make-posn 20 20))
  51.                       5)
  52.            (make-posn 6 7))
  53.  (make-game (make-snake 'up (list (make-posn 1 2)))
  54.             (list (make-posn 6 7) (make-posn 3 4))
  55.             (list (make-posn 10 10)
  56.                   (make-posn 20 20))
  57.             5))
  58.  
  59. ; change-direction : game direction -> game
  60. ; Given a game and direction, returns a new game where the snake
  61. ;   is now headed in the provided direction.
  62. (define (change-direction g d)
  63.   (make-game (make-snake d (snake-segments (game-snake g)))
  64.              (game-food g)
  65.              (game-obstacles g)
  66.              (game-ticks g)))
  67.  
  68. (check-expect
  69.  (change-direction
  70.   (make-game (make-snake 'down (list (make-posn 1 2)))
  71.              (list (make-posn 3 4))
  72.              empty
  73.              5)
  74.   'left)
  75.  (make-game (make-snake 'left (list (make-posn 1 2)))
  76.             (list (make-posn 3 4))
  77.             empty
  78.             5))
  79.  
  80. ; game-score : game -> nat
  81. ; Given a game, returns a score (as a number)
  82. (define (game-score g)
  83.   (* 10 (round (* 10 (/ (* (length (snake-segments (game-snake g))) 100)
  84.                              (if
  85.                               (= 0 (game-ticks g))
  86.                               1
  87.                               (game-ticks g)))))))
  88.  
  89.   ; no tests are provided for game-score because it is open-ended
  90.  
  91.   ; game-over? : game -> boolean
  92.   ; Given a game, returns true if that snake has died and false otherwise.
  93.   (define (game-over? g)
  94.     (if (or (< (posn-x (first (snake-segments (game-snake g)))) 1)
  95.             (> (posn-x (first (snake-segments (game-snake g)))) 50)
  96.             (< (posn-y (first (snake-segments (game-snake g)))) 1)
  97.             (> (posn-y (first (snake-segments (game-snake g)))) 50)
  98.             (not (empty? (filter (λ (p)
  99.                                    (if (and (= (posn-x (first (snake-segments (game-snake g)))) (posn-x p))
  100.                                             (= (posn-y (first (snake-segments (game-snake g)))) (posn-y p)))
  101.                                        #true
  102.                                        #false))
  103.                                  (game-obstacles g))))
  104.             (not (empty? (filter (λ (p)
  105.                                    (if (and (= (posn-x (first (snake-segments (game-snake g)))) (posn-x p))
  106.                                             (= (posn-y (first (snake-segments (game-snake g)))) (posn-y p)))
  107.                                        #true
  108.                                        #false))
  109.                                  (rest (snake-segments (game-snake g)))))))
  110.         #true
  111.         #false))
  112.  
  113.   (check-expect
  114.    (game-over? (make-game (make-snake 'up (list (make-posn 1 1))) empty empty 5))
  115.    false)
  116.   (check-expect
  117.    (game-over? (make-game (make-snake 'up (list (make-posn -1 1))) empty empty 5))
  118.    true)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top