7.9

Homework 2

home work!

Programming Language BSL

Due Date: Friday February 5, 9pm

Purpose To write simple functions; design data; design World Programs

Expectations

This will be an individual (non-pair) assignment. For this and all future assignments you must upload one .rkt file in the language specified at the top of the assignment to the Handin Server. Failure to do so invalidates your submission and will therefore, unfortunately, result in a score of 0.

Unless otherwise stated, all data and functions you design must be designed according to the data and function design recipes (DR), resp., following all four steps in each case, with the exception of functions that call big-bang, for which you need no check-expects, as justified in class. All functions used inside big-bang must have check-expects, along with the other three steps of the design recipe.

We will not walk you through the DR steps in each problem, nor request them individually and explicitly. Instead, review the data and function design recipes before starting this homework, and then have these recipes handy.

Exercise 1 Design data for a ChessPiece. There are six of them: pawn, knight, bishop, rook, queen, and king.

Exercise 2 Design a function piece-multiplicity that takes a chess piece as input and returns how many copies of each piece one player has when the game starts. (Do some research if you are not a chess player.) You can use PosInteger as the return type of your function.

Exercise 3 Different chess pieces have different (perceived) "values". One commonly agreed scale is: in the above order, they are worth 1, 3, 3, 5, 9, and 99 points, resp. The "99 points" is the value of the king (it is really infinitely valuable, since losing it loses the game).

Design a function piece-value that returns the value of a given chess piece. You can use PosInteger as the return type of your function.

Exercise 4 Your friend (the obnoxious one from HW01) objects to this value system: "A pawn is only 1 point, but I have many of them!"

Design an all-pieces comparison function called combined-start-<?, which takes two pieces as input and returns whether the first piece has a smaller combined start value than the second. The combined start value of a chess piece is defined as the product of the piece’s point value and its multiplicity on the initial chess board.

Exercise 5 Design the following data, to be used in a scheduling app.

  • Design two types of data, ClockHour and ClockMinute, that represent the hours or minutes displayed by a clock, resp. Make up your mind whether you use a 24h clock or a 12h clock. (Hint: 24h will be easier down the road.)

  • Design data Duration that represents the duration of some event, in minutes. We consider only whole minutes.

Exercise 6 Design a function ends-at-m that takes the start time of an event—in the form of an hour and a minute— and a duration, and returns the minute of the time at which the event ends.

Then design an analogous function ends-at-h that returns the hour of the time at which the event ends.

You can ignore the day here, i.e. we don’t care if an event wraps around midnight. Just return the minute and the hour it ends.

Note: it may seem silly to have two functions that return the minute and hour of a time separately. We do this because we have not yet seen compound data, which can pack separate basic data into a larger piece of data.

Exercise 7 Design a function within? that takes the start time and the duration for two events as input (the times come in the form of an hour and a minute). The function returns whether the first event happens entirely within the time frame of the second event. That is, any minute during which the first event is on, the second event is on, too. Think about what that means.

Hint: First design a helper function time<=? that takes two times as input—both in terms of hour and minute—and returns whether the first time is earlier than or equal to the second. Then use the helper function to implement within?.

Exercise 8 You will design a World program in which a planet orbits around the sun (’cause that’s what planets do). We begin by defining a bunch of constants:

  • Our background sky will be of size 500x500. Define constants for its width, height, and for the coordinates X0 and Y0 of the center of the sky. These coordinates should be calculated off the sky width and height.

  • Now define the SKY itself, as a rectangle, and the SUN and a PLANET as circles. Colors are up to you; radii 30 and 10 work well for the latter two. Also define a constant SKY-WITHIN-SUN that puts the SUN into the middle of the SKY.

  • Finally, we need a constant R-ORBIT of value 100—this will be the radius of the (assumed circular) orbit of our planet. And we need a constant END-ALPHA. You can assign 0 to it for now; we will get back to it later.

Exercise 9 As always, the first question when designing a World program is: what is the (changing) state? The answer is: the planet’s coordinates. But wait, there are two—x and y. Our world state must be a single value; we cannot have both x and y change! What do we do?

Here is a trick: we store the angle between a horizontal line and the line that connects the planet and the sun. If the angle is 0, the planet is to the right of the sun. If the angle is 90, the planet is vertically below the sun (it orbits clock-wise). Along with R-ORBIT, the angle parameter is all we need to determine the exact position of the planet! The radius and this angle measure, as a way of tracking a position, are called the position’s polar coordinates.

Design a function orbit that takes the initial angle as input—this will be a real number. The function then calls big-bang with that initial angle, and three event clauses: one to draw our current scene, one to move the planet when the clock ticks, and one to stop the world program when a certain condition becomes true. We will define the handler functions for these events in the rest of this homework. In this exercise, just set up the call to big-bang in function orbit. Follow the Design Recipe, but omit check-expects for orbit.

Exercise 10 Exercise 11 below asks for a function to draw the scene. That exercise needs a helper function, as follows. Our angle is given in degrees, e.g. 0, 90, 180. But BSL’s trigonometric functions, which we will need next, expect an angle in radians. We therefore need a function to convert the angle in degrees to radians. The formula is: radians(x) = (x*pi)/180. Write a helper function that performs the conversion.

There is one caveat: the conversion involves the irrational number pi and, therefore, inexact numbers. BSL does not permit inexact-number expressions in check-expects. Instead, it has a special test function for such cases, called check-within. Review this function in the documentation, and then give tests for your conversion function using check-within, with a delta (see documentation) of 0.1.

Exercise 11 The big-bang handler function that draws our scene places the PLANET at the correct coordinates into the sky with the sun (a predefined constant). How do we get the coordinates? We use the mathematical fact that the cosine of the current angle equals the ratio of the x coordinate to the orbit radius. We know the current angle (= the state of the World) and the orbit radius (a constant), so we can compute the x coordinate easily! The same works for the y coordinate, but you must replace the cosine by the sine. BSL has functions sin and cos.

There is one more thing to consider: the above sin/cos math to compute the planet’s position assumes the sun is at position (0,0). But it sits in the middle of the sky. The fix is to simply add X0 and Y0 to the positions you have computed using the above math, to get the final coordinates of the planet.

Use a single check-expect, where you pass in 0 as the angle. For this angle, the planet should be straight to the right of the sun. Put in the correct position for this case (without the sin/cos business). Do not use other check-expects for this function.

Exercise 12 Design a function that moves the planet, by increasing its angle by 1. Use it as the handler function for clock ticks.

Exercise 13 Design a handler function for the stop-when clause. We want the World to end when the planet has orbited the sun a full three times. Put the appropriate angle value that represents this "achievement" into the END-ALPHA constant defined earlier, and use it in your handler function.

The stop-when clause has an optional second argument: the name of a function to draw when the world stops, i.e. a final image. The final image should be the sky with the words "Stop watching movies!" in font size 40 written in the middle. Use the overlay and text functions in BSL. Check out the stop-when documentation for the signatures of the two function arguments to stop-when.

When all is in place, don’t forget to play with your (orbit ...) function!