1. Random

DrRacket provides a library function for generating random numbers. The function is called random.

Randomness makes testing a little harder since our function may generate a result that is random. DrRacket also provides check-random which is a special function like check-expect but knows how to deal with calls to random.

Practise Exercises
  • 1) Design a function that consumes 2 numbers such that one number is less than the other and returns a random number that is between the the inputs. For example calling your function with input, 5 and 10 should return a random number between 5 and 10. You do not need check-random to test this function.

  • 2) Design a function that generates an image of a red, solid, circle with a random radius. The radius cannot be more than 400 pixels.

  • 3) Design a function that generates an image of a red, solid, circle with radius 100, at a random location within a canvas of size 500 by 500. Make sure that the whole circle always appears within the canvas.

2. Snake Game Extensions.

You may use the code from last lecture for the Snake Game in order to design the following extensions to the game.

Practise Exercises
  • 4) Some of the users of our Snake game are complaining that it is too hard to count the segments of the snake while playing the game. They would like you extend the game so that the total number of segments is visible on the canvas in the top right hand corner.

  • 5) A friend is asking you to extend the Snake game again. This time they would like to see a clock that will show the duration of the game while the player is playing the game. Duration should be displayed as minutes and seconds, e.g., 77:45 at the top left hand corner of the canvas. Seconds are between 0 and 59, minutes are greater or equal to 0.

  • 6) A group of players are asking you to add features to make the game a little more difficult. One idea that they have is to move the food item after 15 clock ticks, if the item has not been eaten yet. In other words, if 15 clock ticks have passed and the snake has not managed to eat the food, then we move the food to a new random location.