Problem Set 4: Breakout: Objectives
Problem Set 4: Breakout: Objectives
Problem Set 4: Breakout: Objectives
Table of Contents
Objectives ....................................................................................................................... 1
diff pset4 hacker4 ........................................................................................................... 2
Academic Honesty .......................................................................................................... 2
Reasonable .............................................................................................................. 3
Not Reasonable ....................................................................................................... 3
Scores ............................................................................................................................. 4
Shorts .............................................................................................................................. 5
Backstory ........................................................................................................................ 5
Getting Started ............................................................................................................... 6
Breakout ........................................................................................................................ 18
How to Submit .............................................................................................................. 22
Step 1 of 2 ............................................................................................................ 22
Step 2 of 2 ............................................................................................................ 23
due Thu 10/10 Fri 10/11 at noon
with thanks to Eric Roberts of Stanford
No need to submit any work early (i.e., by Wed) this week in order to get an extra
day. Everyone gets an extra day this week; everyones deadline is Fri 10/11!
1
Objectives
Learn an API.
Build a game with a real GUI.
1
https://www.cs50.net/discuss
2
https://www.cs50.net/ohs
Academic Honesty
This courses philosophy on academic honesty is best stated as "be reasonable." The
course recognizes that interactions with classmates and others can facilitate mastery of
the courses material. However, there remains a line between enlisting the help of another
and submitting the work of another. This policy characterizes both sides of that line.
The essence of all work that you submit to this course must be your own. Collaboration on
problem sets is not permitted except to the extent that you may ask classmates and others
for help so long as that help does not reduce to another doing your work for you. Generally
speaking, when asking for help, you may show your code to others, but you may not view
theirs, so long as you and they respect this policys other constraints. Collaboration on
quizzes is not permitted at all. Collaboration on the courses final project is permitted to
the extent prescribed by its specification.
Below are rules of thumb that (inexhaustively) characterize acts that the course considers
reasonable and not reasonable. If in doubt as to whether some act is reasonable, do not
commit it until you solicit and receive approval in writing from the courses heads. Acts
considered not reasonable by the course are handled harshly. If the course refers some
matter to the Administrative Board and the outcome is Admonish, Probation, Requirement
to Withdraw, or Recommendation to Dismiss, the course reserves the right to impose local
2
Reasonable
Communicating with classmates about problem sets' problems in English (or some
other spoken language).
Discussing the courses material with others in order to understand it better.
Helping a classmate identify a bug in his or her code at Office Hours, elsewhere, or
even online, as by viewing, compiling, or running his or her code, even on your own
computer.
Incorporating snippets of code that you find online or elsewhere into your own code,
provided that those snippets are not themselves solutions to assigned problems and
that you cite the snippets' origins.
Reviewing past semesters' quizzes and solutions thereto.
Sending or showing code that youve written to someone, possibly a classmate, so that
he or she might help you identify and fix a bug.
Sharing snippets of your own code on CS50 Discuss or elsewhere so that others might
help you identify and fix a bug.
Turning to the web or elsewhere for instruction beyond the courses own, for references,
and for solutions to technical difficulties, but not for outright solutions to problem sets
problems or your own final project.
Whiteboarding solutions to problem sets with others using diagrams or pseudocode
but not actual code.
Working with (and even paying) a tutor to help you with the course, provided the tutor
does not do your work for you.
Not Reasonable
Accessing a solution in CS50 Vault to some problem prior to (re-)submitting your own.
Asking a classmate to see his or her solution to a problem sets problem before
(re-)submitting your own.
3
Scores
Your work on this problem set will be evaluated along four axes primarily.
Scope
To what extent does your code implement the features required by our specification?
Correctness
To what extent is your code consistent with our specifications and free of bugs?
4
Shorts
Head to https://www.cs50.net/shorts/4 and watch the shorts on pointers and strings.
Then head to https://www.cs50.net/shorts/5 and watch the short on the CS50 Library.
We may have a few questions for you when it comes time to submit this problem sets
form!
Backstory
One day in the late summer of 1975, Nolan Bushnell [founder of Atari and,
um, Chuck E. Cheeses], defying the prevailing wisdom that paddle games
were over, decided to develop a single-player version of Pong; instead of
competing against an opponent, the player would volley the ball into a wall
that lost a brick whenever it was hit. He called [Steve] Jobs into his office,
sketched it out on his little blackboard, and asked him to design it. There
would be a bonus, Bushnell told him, for every chip fewer than fifty that he
used. Bushnell knew that Jobs was not a great engineer, but he assumed,
correctly, that he would recruit [Steve] Wozniak, who was always hanging
around. "I looked at it as a two-for-one thing," Bushnell recalled. "Woz was
a better engineer."
Wozniak was thrilled when Jobs asked him to help and proposed splitting the
fee. "This was the most wonderful offer in my life, to actually design a game
that people would use," he recalled. Jobs said it had to be done in four days
and with the fewest chips possible. What he hid from Wozniak was that the
deadline was one that Jobs had imposed, because he needed to get to the
5
Getting Started
Your challenge for this problem set is to implement the same game that Steve and Steve
did, albeit in software rather than hardware. That game is Breakout.
Now, Problem Set 3 was also a game, but its graphical user interface (GUI) wasnt exactly
a GUI; it was more of a textual user interface, since we essentially simulated graphics
with printf . Lets give Breakout an actual GUI by building atop the Stanford Portable
Library (SPL), which is similar in spirit to the CS50 Library but includes an API (application
programming interface) for GUI programming and more.
Lets get you started.
As always, first open a terminal window and execute
update50
followed by
wget http://cdn.cs50.net/2013/fall/lectures/5/m/src5m.zip
your appliance probably doesnt have Internet access (even if your laptop does), in
which case you can try running connect50 or even restarting your appliance via
Menu > Log Off, after which you can try wget again.
When ready, unzip the file with
unzip src5m.zip
at which point you should find yourself with a directory called src5m in ~/Dropbox .
Navigate your way into it with
cd src5m
checkbox.c
click.c
cursor.c
label.c
Makefile
slider.c
spl
spl.jar
text.c
window.c
A window quite like the below should appear and then disappear after 5 seconds.
Neat, eh? Open up window.c with gedit . Lets now take a tour.
http://www.youtube.com/watch?v=gjirq3MzqBY
How did we know how to call newGWindow like that? Well, there arent man pages
for SPL, but you can peruse the relevant header file ( gwindow.h ). In fact, notice that
inside of src5m is a subdirectory called spl . Inside of that is another subdirectory
called include . If you take a look there, youll find gwindow.h . Open it up with
gedit and look around. (Alternatively, you can see it at http://cdn.cs50.net/2013/
to download a ZIP of this problem sets distro into your appliance. You should see a
bunch of output followed by:
'hacker4.zip' saved
your appliance probably doesnt have Internet access (even if your laptop does), in
which case you can try running connect50 or even restarting your appliance via
Menu > Log Off, after which you can try wget again.
Ultimately, confirm that youve indeed downloaded hacker4.zip by executing:
ls
Then, run
unzip hacker4.zip
https://www.cs50.net/discuss
10
followed by
ls
and you should see that the directory contains three files and one subdirectory:
Makefile , which will let make know how to compile your program;
breakout.c , which contains a skeleton for your program;
spl , a subdirectory containing SPL; and
spl.jar , a "Java archive" that contains code (written in a language called Java)
atop which SPLs C library is written.
Lets see what the distribution code does. Go ahead and execute
make breakout
to run the program as is. A window like the below should appear.
11
12
13
14
15
17
Breakout
Alright, if youre like me, odds are youll find it easiest to implement Breakout via some
baby steps, each of which will get you closer and closer to a great outcome. Rather than
try to implement the whole game at once, allow me to suggest that you proceed as follows:
1. Try out the staffs solution again (via ~cs50/pset4/breakout from within your own
~/Dropbox/pset4 directory) to remind yourself how our implementation behaves.
Yours doesnt need to be identical. In fact, all the better if you personalize yours. But
playing with our implementation should help guide you toward yours.
2. Implement initPaddle . Per the functions return value, your paddle should be
implemented as a GRect . Odds are youll first want to decide on a width and height
for your paddle, perhaps declaring them both atop breakout.c with constants. Then
calculate coordinates (x and y) for your paddle, keeping in mind that it should be initially
aligned in the bottom-middle of your games window. We leave it to you to decide
exactly where. Odds are some arithmetic involving the windows width and height and
the paddles width and height will help you center it. Keep in mind that x and y refer to a
GRects top-left corner, not its own middle. Your paddles size
and location doesnt need to match the staffs precisely, but it
should be perfectly centered, near the windows bottom. Youre
welcome to choose a color for it too, for which `setColor and
setFilled might be of interest. Finally, instantiate your paddle with newGRect .
(Take note of that functions prototype at http://cdn.cs50.net/2013/fall/psets/4/pset4/
pset4/spl/doc/gobjects.html.) Then return the GRect returned by newGRect (rather
18
you can determine whether that object is your games paddle, as with the below.
if (object == paddle)
{
// TODO
}
Once it comes time to add a GLabel to your game (for its scoreboard), you can
similarly determine if that object is GLabel , in which case it might be a collision
you want to ignore. (Unless you want your scoreboard to be something the ball can
bounce off of. Ours isnt.)
if (strcmp(getType(object), "GLabel") == 0)
{
// TODO
}
20
9. Now decide how to determine whether the ball has zoomed past the paddle and
struck the windows bottom edge, in which case the user should lose a life and
gameplay should probably pause until the user clicks the mouse button, as in the staffs
implementation. Odds are detecting this situation isnt all that different from the code
you already wrote for bouncing; you just dont want to bounce off that bottom edge
anymore!
10.Next, implement initScoreboard in such a way that the function instantiates and
positions a GLabel somewhere in your games window. Then, enhance main in
such a way that the text of that GLabel is updated with the users score anytime the
user breaks a brick. Indeed, be sure that your program keeps track of how many lives
remain and how many bricks remain, the latter of which is inversely related to how
many points you should give the user for each brick broken; our solution awards one
point per brick, but youre welcome to offer different rewards. A users game should
end (i.e., the ball should stop moving) after a user runs out of lives or after all bricks
are broken. We leave it to you to decide what to do in both cases, if anything more!
11.Lastly, amaze us by integrating at least two (2) of the following features into your game:
Implement God Mode whereby, if the program is run with ./breakout GOD , the
game ignores the users mouse movements and instead moves the paddle itself
in perfect lockstep with the ball along its (horizontal) x-axis so that the ball never
misses the paddle.
Implement a shrinking-paddle mechanism whereby the paddles width decreases
as bricks are broken; we leave it to you to decide on a formula.
Implement a variable-scoring mechanism whereby bricks higher in the games grid
are worth more points than are bricks lower in the games grid; we leave it to you
to decide on a formula.
21
How to Submit
Step 1 of 2
When ready to submit, open up a Terminal window and navigate your way to ~/
Dropbox . Create a ZIP (i.e., compressed) file containing your entire hacker4
directory by executing the below. Incidentally, -r means "recursive," which in this case
means to ZIP up everything inside of hacker4 , including any subdirectories (or even
subsubdirectories!).
zip -r hacker4.zip hacker4
If you type ls thereafter, you should see that you have a new file called
hacker4.zip in ~/Dropbox . (If you realize later that you need to make a change
to some file and re-ZIP everything, you can delete the ZIP file you already made with
rm hacker4.zip , then create a new ZIP file as before.)
Once done creating your ZIP file, open up Chrome inside of the appliance (not on your
4
own computer) and visit cs50.net/submit , logging in if prompted.
Click Submit toward the windows top-left corner.
Under pset4 on the screen that appears, click Upload New Submission.
On the screen that appears, click Add files. A window entitled Open Files should
appear.
4
https://www.cs50.net/submit
22
Step 2 of 2
Head to https://forms.cs50.net/2013/fall/psets/4/ where a short form awaits. Once you
have submitted that form (as well as your source code), you are done!
This was Problem Set 4.
https://www.cs50.net/submit
23