Homework 2: Leaving Earth

Due: Tuesday September 18 2018, 9:00PM.
Late Deadline: Wednesday September 19 2018, 5:00PM.
enter image description here

After completing the homework, you will submit:

Hand in your work at this Google Form (https://goo.gl/forms/Fkr461qrsm27iZnP2).

Helpful Things

Setup

You will need a text editor to write your answer to the first question. Any text editor is fine, and your laptop should already have one (TextEdit on Mac, NotePad on Windows). You could also install a more powerful text editor (such as Atom, instructions here), but this is not required. If you are still having a hard time, come to hours or post on Piazza!

Documentation

Useful Functions

The Assignment

Binge Breaker

  1. Read this article: The Binge Breaker.

  2. Make a new text file called bingebreaker.txt (see setup).

  3. Write a reflection on this article in bingebreaker.txt. This reflection should be 2-3 short paragraphs, but we are not looking for length. You should include a brief summary of the article (two to three sentences). Some things to write about:

    • What is your reaction to the article? Is this a real problem? To what extent or in what ways does this problem concern you (if at all)? Why?

    • How could this problem be addressed by the computer science community?

    • How could this problem be addressed in broader human/social/policy contexts?

  4. In the same file, write a short, one-paragraph comparison the standard Gmail and the “raw HTML” Gmail, as seen here:

    1. (standard gmail)
      https://mail.google.com/mail/u/0/#inbox

    2. (html gmail)
      http://mail.google.com/mail/h/

Consider these questions:

Coordinates

You’ve just arrived to the spaceship that will take you to your next destination and sat in the pilot chair! Yes! In order to get to your next destination, Mars, you need to enter in its coordinates. However, if you don’t follow the ship’s rules about how to enter the coordinates, you will be locked out of the system and be stuck on Earth.

The coordinate string must:

Write a function coordinate-check in a file called coordinates.arr that takes in a coordinate String and returns true if the coordinates follow the rules, and false if they do not.

Examples:

Clarification, added monday 9/17: The input sequence will never contain more than 1 comma in it. So your code need not be able to handle, for example, <bc,v,d>

Warning: being able to look in language documentation for useful operations is an important skill, which is why we aren’t telling you exactly which string operations to use. We want you to look at the string documentation to find useful operations for solving this problem. However, limit yourself to operations with input and output types that we have used this semester (Number, String, Boolean). Don’t use operations that return List, as we haven’t covered that yet.

Complete the circle

When you finally land on Mars, you encounter a Martian named Eli. He likes your camera, and especially your photo of some blurry stars in a circle. However, he rips up the picture in his excitement! Now you have to figure out how to recreate the image using just the upper left quadrant.

Make a new file called circle.arr and copy this code in.

include image

fun get-quadrant() -> Image:
  doc: 'load upper left quadrant of the circle'
  # do not edit this function
  image-url('https://cs.brown.edu/courses/csci0111/data/ul.png')
end

Write a function complete-circle which uses get-quadrant and other image operations to produce a circle of blurry stars. Keep repeated code to a minimum by using constants where appropriate.

CLARIFICATION (added Sun 9/16): You do not need to eliminate the background around the circle. All Pyret images are actually rectangular, but the background pixels are often transparent so you don’t see the rectangular boundary. Your code just needs to produce an image with a complete circle; it is fine if the rectangular background still shows.

Battle plans

As a gesture of good will after the camera incident, Eli shares some Martian code with you. He tells you that the fighter pilots of Mars use the code to train for battle.

Copy this code into a new Pyret file called battleplans.arr; run the function draw-game-screen with a few different numerical inputs to see how it works.

include image

mainurl = 'https://cs.brown.edu/courses/csci0111/data/spaceship1.png'
enemyurl = 'https://cs.brown.edu/courses/csci0111/data/spaceship2.png'

s1 = image-url(mainurl)
s2 = image-url(enemyurl)

fun draw-game-screen(position):
  underlay-xy(
    underlay-xy(rectangle(300, 600, 'solid', 'black'), position, 500, s1),
    30, 20,
    above(
      beside(
        flip-vertical(s2),
        beside(
          rectangle(100, 1, 'solid', 'transparent'),
          flip-vertical(s2))),
      above(
        rectangle(1, 50, 'solid', 'transparent'),
        flip-vertical(s2))))
end

However, this Martian script does not follow your CS111 style guide!

  1. Add a type annotation to the function.

  2. Add a docstring to the function. Good documentation includes:

    • A description of the input(s).

    • A description of the output.

    • Any restrictions on the input(s).

  3. Clean up this function; keep the same function name, draw-game-screen.
    Note: the output image does not have to be exactly the same, but it should be similar.
    Tip: Sometimes it can be very hard to read code. It can help to try changing some numbers around and seeing what effect that has.

    • Numbers used for positioning should not be hard-coded, but should be given a useful name. For example, CIRCLE-RADIUS + PADDING is clearer than 30 + 5.

    • Numbers that are based on their context should be computed based on their context. For example, the circle in Greenland’s flag would be moved horizontally based on the width of the entire flag (maybe 1/31/3 the flag’s width), not using a seemingly arbitrary number like 100.
      The image-width and image-height functions may be helpful for this.

    • Remove repeated computation using either helper functions or constants.

    • Rename constants if they are unclearly named.

    • Add short comments for lines that may not be intuitive.

    • When you are making constants, think about whether to put them inside or outside of the function. In general, you should only have constants inside of the function if they depend on the inputs.

      pi = 3.14
      fun area(R):
      	pi * num-sqr(R)
      end
      

      is cleaner than

      fun area(R):
      	pi = 3.14
      	pi * num-sqr(R)
      end
      

      There is no need to redefine pi every single time area is used; pi never changes.


Brown University CSCI 0111 (Fall 2018)
Do you have feedback? Fill out this form.