WARMUP
The game 2048 is a single player game on a 4x4 board. Tiles of value 2 or 4 randomly appear on the board. The player shifts the tiles on the board in any of the 4 directions, and tiles of the same value merge when shifted towards each other.
The player wins if they create a 2048 tile.
Describe the parts of a program necessary to create this game.
HW Review
What did people have the most trouble figuring out? ask random
Programming
Thing big: what was this homework about? ask random
Key Research Ideas
- plan to visualize results
- “model worlds”
Key Python Ideas
dicts
as alternative to complexif-elif-else
- old: defining functions
- functions as inputs to functions
- plotting libraries
Drawing vs. Plotting
There are two main tasks in visualization, which I will refer to as drawing and plotting.
In plotting, the scale and exact line positions are precise: ideally, you can determine exact numbers by reading off the lines with reference to the scales. You can use the numpy
plotting library by importing pylab
. We will be addressing plots later, when we learn about numpy
and scipy
.
In drawing, the scale and lines are aesthetic rather than precise. It’s still important to get them “right”, but right is subjective - the goal with drawing is to illustrate, rather than exact representation. Python has a basic library for drawing called turtle
.
Advanced Version
We are going to modify your drawings to:
- make the shapes filled instead of lines
- take arguments for the fill color, but fill with black if no color is provided
- draw the figures on the same diagram, if requested
After you have modified your code, the render_shapes2.py
script should produce a series of nested shapes in different colors, but your code SHOULD CONTINUE TO WORK FOR THE ORIGINAL HOMEWORK. The one difference is that the shapes should be filled (if they weren’t before).
Project Advice
This is more capability that you need to complete one of the projects. We also covered thinking about how to extend your existing work, and how to build that work in a way that can be extended. You will need to do a bit more extension on your own for the project.
HOMEWORK
Implement the Mid-point approach to numerical integration in midpoint.py
.
Consult try_integrators.py
to understand the required method signatures. Once your integrators are complete, the following code should work:
$ ./try_integrators.py
...midpoint method, area under e^x on (0, 10), 100 points: ...
...scipy.integrate result: ...
Implement the bisection method for root finding with a stopping criteria on the value of the function (instead of the width of the interval) in bisection.py
. See try_roots.py
for the appropriate method signature. Once your code is complete, the following should work:
$ ./try_roots.py
...bisection method, root of y = (x-1)(x+10)^2 on (0, 10)
...scipy.optim result: ...