Lab 01
Partners
At the start of lab, you should have received a partner assignment. This will be your homework partner for the first half semester. Make sure to introduce yourself and set up a regular meeting time to work on the homework. It’s important to start early…
Your lab partner will change every week so that you can get to know your fellow computer scientists. Your homework partner will probably only change once; this is so that you may develop a rapport and consistent working schedule.
If you have not received a partner assignment by now, speak to your TA immediately.
Pet Rock and The Nested Boxes
Aaron is very protective of his pet rock, "King Paimon"
. He carefully guards it by placing it in layers of boxes of various colors, each with their own size.
To represent this psychologically dubious scenario, we are going to use a list to represent the (potentially 0) layers of boxes, in the order of from the largest box to "King Paimon"
itself.
Note: in this imaginary world, larger boxes can fit inside smaller ones, and negative box size is permitted.
- Write the data definition for
PRS
(i.e. Pet Rock Storage) describing the scenario, along with other data definitions you might need. What should your base case be? - Define examples and design a template for
PRS
. - Design a function
largest-box-size
which gives the size of the largest box in aPRS
. If the pet rock is contained in no boxes,largest-box-size
should produce 0. - Design a function which determines if a box of a particular color is in a
PRS
.
Switch pair programming roles
- Design a function which takes a
PRS
and increases all of its boxes sizes by 1. - Design a function which takes a
PRS
and a number and discards all boxes bigger than that size. - Design the function
well-stored?
which ensures aPRS
is comprised of successively strictly smaller boxes.
Stuck in the middle
Switch pair programming roles
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.
- Design a data definition, called
Point
, which represents a point on a 2D-plane. - 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. - Define constant images to represent the board points, their midpoint, and the point the user chooses. These will come in handy later.
- Design a function called
place-point
, which takes aPoint
, 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. - Design a function called
draw-board
, which renders the board you designed in the previous question.
Switch pair programming roles
Next, we will work on designing the calculations necessary for playing the game.
- Design a function called
midpoint
, which computes the midpoint of twoPoints
. - Design a function called
board-midpoint
, which computes the midpoint of the points on a givenBoard
. - Define a function called
distance
, which computes the 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. - Design a function
user-error
determining the distance from the user-givenPoint
to the midpoint of theBoard
.
Switch pair programming roles
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:
- the
Board
; - the current state of user input.
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.
- Design a data definition for the state of user input, and use this data definition to complete the data definition for the
GameState
. - Design a function to check whether a
GameState
has user-input (i.e., to check if the user has already clicked) - Design a function,
draw-state
, which takes aGameState
and renders the board and a dot (different from the board dots) for the potential user input. - 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. - Design a function,
initial-board
, which takes a height and width and generates a board with randomly positionedPoint
s. How can you test on random values?check-random
is your friend. - Design a function,
initial-state
, which generates an initialGameState
.
Switch pair programming roles
- Design a function to handle when the user has clicked their guess. See the
documentation for
big-bang
’son-mouse
for the structure. - 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 shouldstop-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 tellsbig-bang
to draw the final state of the world before exiting.