CS262, Fall 2011 Program assignment 3 (PA3) Instructor: Jyh-Ming Lien, jmlien@cs.gmu.edu Due date: sections 005 and 002: Nov 28, 2011 by midnight, 11:59pm The deadline is hard. There will be no extensions. Description: Goal: Practicing pointers, dynamic memory allocation, file io signal handling, struct and ability to write a medium size program and ability to use unix-like shell terminal. PA3 Part 1: Before you start, let me warn you this assignment uses ANSI code thus most IDE, including eclipse, will not correctly display the output. You will need to use terminal (console) to see your result. Thus, make sure that you use cygwin/linux/darwin terminals for this assignment. This is a great opportunity to learn/sharpen your ability to work with the command lines. In PA3, your goal is to write a program that can read a vector field from a file and the starting position of the particle from the command line and output the trajectory of the particle. Details are provided below. (1) Your program will take 3 arguments in the following order: - filename (of the vector field) - x (integer, the x coordinate of the particle) - y (integer, the y coordinate of the particle) -ex: pa3 ex01 0 0 (read from file ex01 and the particle starts at 0,0) (2) Input: The input file (vector field) is stored in a file, here is the format of the vector field: - width (integer) - height (integer) - width*height of the following characters (char) * U (up) * D (down) * L (left) * R (right) * O (obstacle) - ex: 2 2 RDUL (this defines a loop in a 2*2 vector field) - the origin (0,0) of the vector field is at the upper-left corner - the definition of the vector field is in row-major order - If there is anything wrong (e.g. not enough characters or unknown characters), you program should terminate and report errors - if the initial position of the particle (x,y) is outside the game world, then you program should terminate and report errors (3) Output: Use the provided ANSI display library to animate the particle moving in the vector field and draw the trajectory of the particle. The particle will start from the given starting point and iteratively move to the next position by following the vector field. Use the default frame rate in the given code (about one update per second) to animate your particle. Your animation should stop when the particle dies. The particle p will die if - p is trapped - p hits an obstacle - p hits the boundary - p hits its own trajectory and if the starting position is inside an obstacle pixel, then the particle dies immediately (4) Details about the display code and example code (pa3.tar.gz). The given code "pa3.tar.gz" contains an example (main.c) that uses the provided ANSI-based terminal display code. It (main.c) generates an environment at random. The particle also moves in random directions at every frame update (about 1 frame per second). You can use main.c as your starting point for this assignment. To compile the code, type "make" (if you are using cygwin, please make sure that "make" is installed.) The result of the compilation is a program called pa3 (or pa3.exe). To run pa3, type "pa3 width height". For example, "pa3 10 10" will create a random environment with size 10*10. You may use eclipse to compile the code but as mentioned earlier the program output from eclipse will not make sense. You need to run pa3 from command line. Your goal is to modify main.c (don't change other files) so that the particle will "flow" in the vector field until it dies. For example, if the vector field is "2 2 RDUL", then your program will show RD UL and after 4 updates (~4 seconds), the particle will die since it hits its own trajectory. (5) Make sure that you follow the input/output requirements strictly to avoid any penalties. PA3 Part 2 (optional): (1) Write a control code that allows user to change the particle's movement against the vector field at most "max(width, height)" times, where "width, height" is the width and height of the vector field. (10% extra points) (2) Design a method to automatically control the particle so that the particle can move against the vector field at most "max(width, height)" times while maximize the length of the particle's trajectory. Again, "width, height" is the width and height of the vector field. (30% extra points) (3) Design 1 large (40*40 or larger) interesting environment. An interesting environment contains at least three (3) long trajectories in the vector field. These three trajectories cannot overlap but can intersect. A trajectory is long if its length is longer than "2*max(width, height)" pixels (twice the longer side of the vector field), where "width, height" is the width and height of the vector field. (10% extra points) To turn in: (1) Submit a copy of your (well commented) source code. Make sure that you code is well documented. Make sure that your documented code includes your name and the assignment number. (2) Submit a sample terminal session Showing several runs of your program. Show your example input clearly in your sample terminal session. You can use the example input above and create two more make-up examples. (3) Submit a README file. The README file should have brief description on - What you have done (for both the required part and the bonus part - Limitation/know bugs - (optional) A description of your algorithm for automatic control - (optional) A description of your design of the interesting environment (4) Your test environments used in your terminal session a (5) An interesting (optional) vector field named "fun-field.txt" * Zip all the above documents to pa3-(Your G#).zip * Send your documents to cs262.2011fall.lien@gmail.com with subject line pa3-(Your G#)