Code Correctness
In CS 3, we expect you to pay careful attention to the quality of your code (not just if it “works”). Each week, we will expect you to apply all the items we’ve been looking for in every previous week in addition to the new ones.
So, far, we’ve asked you to pay attention to:
- good variable and function names
- procedural decomposition
This week, we will focus on all of the previous items as well as the following:
- “overcommenting” (e.g., the comments do not explain things that are already clear from the code itself)
- usage of whitespace
- overmodularization (e.g., modularizing into functions that actually make the code less clear)
Goals and Deliverables
In this project, you will begin writing the codebase you will be working with over the next several weeks; it is entirely possible that you will still be using some of this code in week 10 of the term. This week, you will build a basic data structure.
- The two most primitive things to represent are two-dimensional vectors and polygons. You will implement the base of both of these this week.
- The main data structure we will need is an
ArrayList
. This week, you will write a simple wrapper for an array, and you will extend it to be more useful in future weeks.
vector_t
In vector.h
, we’ve provided function prototypes which you should implement in vector.c
. See the provided documentation in vector.h
for what the functionality should be. Your vector.c file should (1) include the vector.h header and (2) define the functions specified in vector.h. You’ll be writing an extensive set of functions that act directly on vector_t
s that are
not pointers. Your vector_t
will eventually represent position, velocity, acceleration, and other vector quantities in your physics engine.
Task 0.
Using the documentation in vector.h
, fill in vector.c
with the implementations of the functions outlined in the header files.
vec_list_t
In CS 2, you may remember that we created an ArrayList
in Java from scratch, making it auto-resizable and generic. We will eventually get to the same place in C, but, because C
doesn’t provide as nice abstractions as Java, it’s going to take us a few weeks. This week, since we only need to represent a single polygon, all we need is a fixed-size
ArrayList
of vector_t *
s. Notice that we are using pointers, not values here! This will be important later.
Task 1.
Using the documentation in vec_list.h
as a reference, implement the functionality of vec_list.c
. Your code, as always, should not have any runtime errors or memory leaks.
Polygons
A polygon can be represented as a fixed-size vec_list_t
. We will take the convention that a polygon’s vertices are stored in a counter-clockwise order.
Task 2.
Using the documentation in polygon.h
as a reference, implement the functionality of polygon.c
.
Running and Testing your Code: make
and the Makefile
In project 0, you used clang
to compile your one-file C programs. Going forward, our project will consist of many C files. It is important to organize projects carefully
to help reduce the complexity that comes with larger programs. Your physics library will have the following directory structure:
- include: header files go here
-
library: source files for your libraries (e.g.,
vec_list
,polygon
, etc.) - demo: source files for the demos go here
- tests: test source files go here
- out: built object files will be generated here
- ref: reference object files for the demo and game are provided here
- bin: built binaries will be generated here
We have provided you with a Makefile
which is set up to build the project. It allows you to run the following commands:
-
make demo
: compile the demo and runs the web server -
make game
: compile the game and runs the web server -
make test
: compile all libraries and tests and run all the tests -
make NO_ASAN=true demo
compiles the demo without asan - this is recommended for running demos -
make server
: Run the web server with the current binaries -
make clean
: remove all generated binaries
To run all the tests, all you need to type is make test
. To run compile and run an individual test suite, run the following commands (replacing vector
with the test suite you want to run)
:
make bin/test_suite_vector
./bin/test_suite_vector
Task 1. When you are done, your group should meet and complete the questions here.