CS10: The Beauty and Joy of Computing - Su22
Agenda
Announcements
Addressing Feedback
Feedback
A Bit of a Break
Agenda for Today
The hope is that you keep the 2nd and 3rd items in mind while writing code for Homework 3.
Introducing Homework 3: 2048
2048 The Game - link to play
We Want to Code This Game
Some Important Parameters
Need a Representation
Solution: A List of Lists!
List
Solution: A List of Lists!
List
List containing other lists
List of Lists Board Representation
Now I can
1
2
3
4
1 2 3 4
Row: 2
Column: 2
List of Lists Board Representation
Now I can
1
2
3
4
1 2 3 4
Managing Complexity Through Decomposition
To build a complex system like 2048, it is absolutely necessary to decompose its complex functionality into simpler pieces.
Managing Complexity Through Decomposition
We’ve already decomposed some of the ideas into 5 blocks you’re required to implement.
Block 1: [medium difficulty]
Example:
Managing Complexity Through Decomposition
We’ve already decomposed some of the ideas into 5 blocks you’re required to implement.
Block 2: [medium difficulty]
Example:
Managing Complexity Through Decomposition
We’ve already decomposed some of the ideas into 5 blocks you’re required to implement.
Block 3: [very challenging!!]
Example:
Managing Complexity Through Decomposition
We’ve already decomposed some of the ideas into 5 blocks you’re required to implement.
Block 4: [very easy]
Example:
Managing Complexity Through Decomposition
We’ve already decomposed some of the ideas into 5 blocks you’re required to implement.
Block 5: [medium difficulty]
Example:
False
True
Best Coding Practices
In the real world…
Variable names (out of scope)
Two main camps (in other languages, we cannot have spaces in our variable names):
When do we capitalize the first letter of variable names? NEVER… with one exception.
If a variable will never be changed (i.e. a gravitational constant) capitalize its name. Example: GRAVITY
How can we write **clean** code?
How can we write **clean** code?
How can we write **clean** code?
“Code is meant to be read and is executed incidentally.”
~ Hilfinger, Former 61B Prof
How can we write **clean** code?
Abstract away smaller procedures!
How can we write **clean** code?
Abstract away smaller procedures!
How can we write **clean** code?
Abstract away smaller procedures!
Test Driven Development
(TDD)
Test Driven Development
The basic idea: write tests BEFORE your write code
This way we can
Test Driven Development
You DON’T need to know this
Unit Testing with the Test Block: www.yellkey.com/practice
Test block idea: Simply hard-code a known good input/output pair.
What goes in the ? ? ? ?
A. 0 0 0 0
B. 4 32 16 4
C. 4 0 0 0
D. 4 0 16 16
Example of Unit Testing with the Test Block
Test block idea: Simply hard-code a known good input/output pair.
What goes in the ? ? ? ?
A. 0 0 0 0
B. 4 32 16 4
C. 4 0 0 0
D. 4 0 16 16
Example of Unit Testing with the Test Block
If we run our test now, what will it return, and why?
Example of Unit Testing with the Test Block
If we run our test now, what will it return, and why?