EECS 111 Project
Language: Intermediate Student with lambda
Due: 11:59 PM Friday, Mar. 18
Your goal is to implement a variation of the game Snake.
In this game, you control a snake that wanders around a square board,
eating food. Each time the snake eats a morsel of food, it gets a little
bit longer. If the snake ever runs into itself or runs into the edge of
the board, the game is over. The snake never stops moving; the player can
control only the direction that the snake moves.
To begin, save the files snake-lib.rkt and
provide.rkt in the folder where you will save the
the code you write. Start your code with the line
For this project, we will use the following data definitions.
; a game is |
; (make-game snake food nat) |
; (define-struct game (snake food ticks)) |
; a direction is either |
; - 'up |
; - 'down |
; - 'left |
; - 'right |
; a snake is |
; (make-snake direction body) |
; (define-struct snake (heading segments)) |
; a body is either |
; - (cons posn empty) |
; - (cons posn body) |
; x-coordinates increase from 1 to 50 (inclusive) toward the right |
; y-coordinates increase from 1 to 50 (inclusive) toward the top |
; a food is either |
; - empty |
; - (cons posn food) |
Do not uncomment the define-struct lines; those structure definitions
are included via the require line (they’re listed above just for reference).
This require line also provides two other definitions:
board-length, the length of one side of the board (measured in terms of snake body segments), and
play-game, a function described below.
Develop the following functions (and any necessary helper functions), which will serve as argument to the play-game function:
- ; add-food : game posn -> gameThis function adds a morsel of food at the specified board position.
- ; change-direction : game direction -> gameThis function changes the direction in which the snake is traveling.
- ; game-score : game -> natThis function computes the player’s score, based on the snake’s length and the time (ticks) taken to reach that length. One possiblty is to multiply the snake’s length by 25 and subtract the elapsed ticks, but you’re welcome to experiment with other definitions.
- ; game-over? : game -> booleanThe game ends when the snake runs into itself or a wall. This function detects when such a collision has occurs.
- ; advance-game : game -> gameThis function moves the game forward one step. One step increments the game’s tick component and moves the snake, possibly causing it to eat and grow. Moving the snake means that the snake both gains and loses a segment (unless it eats). The new segment’s coordinates are determined by the segment that was previously at the front of the snake and the direction the snake is heading. The snake loses the oldest segment, namely the one that was previously at the end of the snake.For example, the following shows the snake before and after a single step when the snake is moving upwards without eating.If that same step were to lead the snake to a morsel of food, then the game would progress as follows.
This function does not replace eaten food; play-game (below) handles that task. You do need to handle removing the eaten food, however.
After you have written and thoroughly tested these functions, define an
initial game value game-start and make the following call to
play the game.
(play-game game-start |
advance-game |
add-food |
change-direction |
game-score |
game-over?) |