On this page:
Partners
Pairing Demonstration:   Online Average
Stuck in the middle
8.14

2 Big Bang🔗

home work!

Purpose The purpose of this lab is to practice with big-bang.

Partners🔗

Remember, each lab you will work with a different partner. So find someone you didn’t work with last week.

Pairing Demonstration: Online Average🔗

In this, two TAs will demonstrate pair programming to solve a challenging problem, following the complete design recipe. THIS IS NOT AN EXERCISE, BUT WE DO NOT PROVIDE THE SOLUTION BECAUSE THE POINT IS TO OBSERVE HOW THE TAs INTERACT AS A PAIR TO SOLVE IT.

An "online" algorithm is one that processes data as it receives it, producing results along the way. These are important whenever the amount of data to process may exceed what you can store. For example, consider that we have a embedded computer attached to a temperature sensor in the arctic. The sensor is producing readings every few hundred milliseconds but the embedded computer doesn’t have nearly enough memory to store all of these readings for the months between visits by researchers. That’s okay, because the researchers are okay with just having the average temperature over the entire time between visits. However, we can’t simply store all the values and then compute the average at the end: there isn’t room to store all the values. Instead, we have to compute the average "online".

Our task is to design an online average function that accepts a previously computed current average, a new number, and computes a new current average. We will design a function next-average and an appropriate struct for the current-average.

Stuck in the middle🔗

We are going to write a "click the midpoint" game. It will begin by showing players two points on a screen. The player is then expected to guess their midpoint by clicking on the screen. Once the player clicks, it will show them how the actual midpoint compares to where they clicked.

First, we will start be representing the static elements of the game.

Exercise 1 Design a data definition, called Point, which represents a point on a 2D-plane.

Exercise 2 Design a data definition, called Board, which represents the 2D board on which the midpoint-game will be played. The board should include the locations of the two points.

Exercise 3 Define constant images to represent the board points, their midpoint, and the point the user chooses. These will come in handy later.

Exercise 4 Design a function called place-point, which takes a Point, an image representing that point, and a background image, and places the point’s image on top of the background image at the point’s coordinates.

Exercise 5 Design a function called draw-board, which renders the board you designed in the previous question.

Switch pair programming roles before continuing!

Next, we will work on designing the calculations necessary for playing the game.

Exercise 6 Design a function called midpoint, which computes the midpoint of two Points.

Exercise 7 Design a function called board-midpoint, which computes the midpoint of the points on a given Board.

Exercise 8 Define a function called distance, which computes the Euclidean distance between two points. (The distance between two points is the square root of the sum of the squares of the differences in their x and y coordinates.)

Exercise 9 Design a function user-error determining the distance from the user-given Point to the midpoint of the Board.

Switch pair programming roles before continuing!

Now, we would like to work on rendering the Board on the screen.

At this time, we can render every part of the game, and what’s left is to complete the main logic of the game itself. We need to first represent the state of the game. There are two things to keep track in the game state:

We have already designed the Board. How would we design the user state? The only event we care about here is a single click on the board, but also note that the user might not have immediately clicked their mouse.

Exercise 10 Design a data definition for the state of user input, and use this data definition to complete the data definition for the GameState.

Exercise 11 Design a function to check whether a GameState has user-input (i.e., to check if the user has already clicked).

Exercise 12 Design a function, draw-state, which takes a GameState and renders the board and a dot (different from the board dots) for the potential user input.

Exercise 13 Design a function, draw-state/midpoint, which renders exactly the same state as the previous function, but also includes another dot at the midpoint between the two dots on the board.

Exercise 14 Design a function, initial-board, which takes a height and width and generates a board with randomly positioned Points. How can you test on random values? check-random is your friend.

Exercise 15 Design a function, initial-state, which generates an initial GameState.

Switch pair programming roles before continuing!

Exercise 16 Design a function to handle when the user has clicked their guess. See the documentation for big-bang’s on-mouse for the structure.

Exercise 17 Design a main function that launches the game. The input should be the height and width of the desired board and the initial start state is random. The program should stop-when the player clicks.

Do you see the world change after you click? If not, give your stop-when clause a second input, the function which draws the board, the true midpoint, and where the player clicked, and try again. This tells big-bang to draw the final state of the world before exiting.