CS 1044 roject 3 Fall 2009 Decision Logic: if, if else, switch, Boolean conditions and variables This programming assignment uses many of the ideas presented in sections 3 through 5 of the Dale/Weems text and the lecture notes covered through October 1, so you are advised to read those carefully. Read and follow the following program specification carefully. Your score on this project will be a weighted average of the score you receive for runtime testing and the score you receive for following the instructions in the rogramming Standards section below. The roblem: Cat and Mouse A cat and mouse are placed into a confined linear space that is divided into a sequence of N cells; for example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 N M C Each cell is labeled by an integer coordinates (a column number if you like). In the picture above, the mouse M is in cell 6 and the cat C is in cell 17. The cat and mouse will play a game. Each may move around, but may not leave the sequence of cells. If the cat and mouse reach the same cell after completing a move, then the mouse is caught and the game is over. You will write a program to track the movements of the cat and mouse, given information about the movements each makes and following the rules given in the rest of this specification. Each player (the cat or the mouse) moves among the cells in the sequence. A move is specified by giving a single signed integer, like 4 or -7, that specifies how many cells the player should move horizontally. For example, if the cat made the move -15 from its location shown above, it would wind up in cell 2. Note that the mouse would not be caught in this case, even though the cat appears to have passed through cell 6; think of it as if the cat and mouse simply jump to their next locations without passing through the intervening cells. Neither player is allowed to move to a location outside the sequence of cells. If such a move is attempted, the player will simply wrap around to the opposite end of the sequence. That s because the sequence of cells is really wrapped around into a circle. So, if the mouse made the move -7 from its position in cell 6, it will wind up in cell N-1. Given the initial positions shown above, the distance between the cat and mouse is 11. Distances are never negative. We can also measure the distance a player has moved since the beginning of the game. If the cat made the moves 3, -5 and 10, from its initial position shown above, it would have traveled a total distance of 18. Note that the initial position is not counted as a move. Input file description and sample: Your program must read its input from a file named ChaseData.txt use of another input file name will result in a score of zero. The first line of the input file specifies the number of columns in the grid used for this game. This value will be a positive integer and never more than 10000. Columns are numbered starting at 1. Each remaining line of the input file will fit exactly one of the following formats: M <integer> initial position or movement for the mouse C <integer> initial position or movement for the cat print current data, as specified below The values on each line will be separated by whitespace. You may assume that all the input values will be logically correct. For instance: 1
CS 1044 roject 3 Fall 2009 26 C 24 M 14 M -4 M 10 M -9 C -10 M 10 C -10 M -12 C 12 M 6 Note that you must not make any assumptions about the number of lines of data in the input file. Your program must be written so that it will detect when it's out of input and terminate correctly, just as in roject 2. What to calculate: You will write a program to read the input file and use the given values to: track the current location of the cat and the mouse track the distance currently separating them detect when (if) the cat catches the mouse compute the total distance traveled by each, as described above You must also watch for moves that would take the cat or mouse across one of the boundaries of the region. If such a move is attempted then the cat or mouse should wrap around the boundary as described on the first page of this specification. If the cat catches the mouse, then stop processing the input file immediately and generate the rest of the specified output. Output description and sample: The first line of the output file should include the title Cat and Mouse only; the second line should be blank; the third line should display the column labels shown below; the fourth line should consist of some delimiting symbol to separate the column labels from the body of the table. Next your output file will contain a table, with one line of output for each print command processed from the input file. Each line of the table should list the location of the cat and mouse at the time the print command was read, and the distance between them at that time (provided both are "in play"). If the cat or mouse is not "in play" yet, print "??" instead of a numeric value. There should be a line of delimiters immediately after the last line of the table body. The next two lines of the output file should be blank. The next line should contain the indicated labels, and the line after that should contain the total distances traveled by the cat and mouse. 2
CS 1044 roject 3 Fall 2009 Following that, there should be one blank line. If the cat caught the mouse in this game, then print the message Mouse caught at: followed by the location at which the capture occurred. If the cat didn t catch the mouse, then print the message Mouse evaded Cat. Your program must write output data to a file named ursuitlog.txt use of any other output file name will result in a score of zero. The sample output file shown below corresponds to the input data given above: Cat and Mouse Cat Mouse Distance -------------------- 24?? 24 10 14 24 11 13 4 21 17 4 9 5 16 9 7 16 15 1 -------------------- Distance traveled: Mouse Cat 51 32 Mouse evaded Cat You are not required to use this exact horizontal spacing, but your output must satisfy the following requirements: You must use variables of type int for all the locations, distances, etc. If you use decimal values, your answers may be incorrect due to numerical roundoff. You must use the specified header and column labels as shown. You must arrange your output in neatly aligned columns, with a label identifying the contents of each column. Note that while the Curator doesn t deduct points for horizontal alignment, the TA who grades your source code for programming standards may do so. You must use the same ordering of the columns as shown here. You must print a newline at the end of each line, including the last line. rogramming Standards: You'll be expected to observe good programming/documentation standards. All the discussions in class about formatting, structure, and commenting your code will be enforced. See the rogramming Standards page on the course website. Some specific requirements follow. Documentation: You must include header comments specifying the compiler and operating system used and the date completed. Your header comment must describe what your program does; don't just plagiarize language from this spec. You must include a comment explaining the purpose of every variable or named constant you use in your program. You must use meaningful identifier names that suggest the meaning or purpose of the constant, variable, function, etc. recede every major block of your code with a comment explaining its purpose. You don't have to describe how it works unless you do something so sneaky it deserves special recognition. If you write user-defined functions, each must have a valid C++ prototype and the definition of each must be preceded by a block comment, explaining in one sentence what the function does, and listing each parameter to the function and explaining its purpose. You must use indentation and blank lines to make control structures like loops and if-else statements more readable. The code from roject 1 is a good guide for how to handle some of the file I/O issues. 3
CS 1044 roject 3 Fall 2009 Implementation: Use named constants instead of variables where appropriate. Use at least one bool variable. Use at least one if statement, at least one if else statement, and at least one switch statement. Choose your control structures appropriately. Your submission that receives the highest score will be graded for adherence to these requirements, whether it is your last submission or not. If two or more of your submissions are tied for highest, the latest of those will be graded. Incremental Development: You ll find that it s easier and faster to produce a working program by practicing incremental development. In other words, don t try to solve the entire problem at once. First, develop your design. When the time comes to implement your design, do it piece by piece. Here s a suggested implementation strategy for this project. As usual, verify and correct as necessary after each addition to your implementation. First, write the code necessary to read the entire input file. This should apply the input-failure pattern discussed in class. To test your work, include code to write what you re reading (and nothing else) to the output file. Second, add the code to process the moves and print the locations after each move. Third, add the code to determine if the cat has caught the mouse and react accordingly. Fourth, write the code to compute the total distance traveled by each player and print that along with the locations. Finally, implement processing of print commands and print locations only when specified. Now you have a substantially complete program. At this point, you should clean up your code, eliminating any unnecessary instructions and fine-tuning the documentation you already wrote. Check your implementation and output again to be sure that you ve followed all the specifications given for this project, especially those in the rogramming Standards section above. At this point, you re ready to submit your solution to the Curator. Testing: At minimum, you should be certain that your program produces the output given above when you use the given input file. However, verifying that your program produces correct results on a single test case does not constitute a satisfactory testing regimen. Additional input/output file examples will be posted on the website. You may use those for additional testing. You should make up and try additional input files as well; of course, you'll have to determine by hand what the correct output would be. Submitting your program: You will submit this assignment to the Curator System (read the Student Guide), and it will be graded automatically. Instructions for submitting, and a description of how the grading is done, are contained in the Student Guide. You will be allowed up to ten submissions for this assignment. Use them wisely. Test your program thoroughly before submitting it. Make sure that your program produces correct results for every sample input file posted on the course website. If you do not get a perfect score, analyze the problem carefully and test your fix with the input file returned as part of the Curator e-mail message, before submitting again. The highest score you achieve will be counted. 4
CS 1044 roject 3 Fall 2009 The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: Honor Code: http:www.cs.vt.edu/curator/ In addition to the limits set in the posted course contract, students are expressly forbidden from providing the following information to the CS 1044 Forum, or by discussion or private e-mail to other students in this course: any explanation of how to calculate the position of a player any explanation of how to calculate the total distance traveled by a player It is permissible to post an input file and the corresponding output file to the listserv. Be advised that such postings may be incorrect. ledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the header comment for your program: On my honor: - I have not discussed the C++ language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C++ language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C++ language code or documentation used in my program was obtained from another source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. <Student Name> Failure to include this pledge in a submission is a violation of the Honor Code. 5