assignment no #3 is the file attached to work on
1336 Programming Assignment 3 Page 1 of 1 General Statement You must write this assignment starting with Template for Functions (chapter 5+).py. You earn up to 60% by correct operation and up to 40% with good style, readability, and documentation. Refer to the Style Requirements.pdf document posted in Blackboard. You must also start with Template for Libraries (chapter 5+).py when writing the library module. The Setup This is the classic “cat and mouse” game. Imagine a grid with a cat at one location (column, row) and a mouse at another location (column, row). Each one can move along a row from column to column by adding to or subtracting from its X value, and along a column from row to row by adding to or subtracting from its Y value. The cat wants to eat the mouse and the mouse wants to escape the cat. This version of the game is more complex than the simple beginning, and therefore more interesting. The Problem This program positions the cat and mouse in random locations, then moves the cat and mouse according to some simple rules. For this assignment, please refer to the instructions for Assignment 2. This assignment will make two additions to that problem, otherwise the specifications are the same. The Additions When calculating a new row and column for each animal, pass the new values to a function that appends them to a file named animal_tracks.txt. Be sure that each time you play the game, you are not just adding moves to the previous game (i.e., clear the file before the run). Then change your program to repeatedly read the values for each move from the file and proceed from there (calculate the separation and call the display, as before) until the entire file is read. Be sure to use the file name exactly as specified – I will be running an independent program to display from the file. Special Notes: 1. Do not use lists, even if you know how to use them. 2. The purpose of this assignment is to see if you have the skills to use only the Python tools described in chapters 1-6 to solve this problem. The Final Exam will assess the other skills. 3. Using more advanced features demonstrates that you don’t know the simpler tools well enough, so using them will lower your score. 4. You earn up to 60% by correct operation and up to 40% with good style, readability, and documentation. Submit your solution (in Blackboard) as the files catAndMouse.py and catAndMouseFunctions.py. Name the files exactly as given. Do not add anything to the program names. Not your name, not your ID number, not words like “assignment 3” or “revised”, or any additional characters as part of the file name. The Python program is scored with a maximum value of 100 points. 1336 Programming Assignment 2 Page 1 of 3 General Statement You must write this assignment starting with Template for Functions (chapter 5+).py. You earn up to 60% by correct operation and up to 40% with good style, readability, and documentation. Refer to the Style Requirements.pdf document posted in Blackboard. You must also start with Template for Libraries (chapter 5+).py when writing the library module. The Setup This is the classic “cat and mouse” game. Imagine a grid with a cat at one location (column, row) and a mouse at another location (column, row). Each one can move along a row from column to column by adding to or subtracting from its X value, and along a column from row to row by adding to or subtracting from its Y value. The cat wants to eat the mouse and the mouse wants to escape the cat. This version of the game is more complex than the simple beginning, and therefore more interesting. The Problem This program positions the cat and mouse in random locations, then moves the cat and mouse according to some simple rules. The main program (function, to Python) calls a function that actually plays the game. The main program asks if the user wants to play and calls the game function as long as the user says they want to play. All other functions are stored in a file named catAndMouseFunctions.py, which is imported by the main program. The game playing function generates a random row and column value for the cat and for the mouse. Use global constants for the maximum number of rows and columns in the playing area and generate random location variables between 0 and those values. It might be necessary to keep the area very small (e.g., 10x10) or make a lot of moves for the cat to ever catch the mouse. There is also a global constant for the cat “speed” or (“step size”) and another for the mouse. Big steps also makes it easier for the mouse to escape. Finally have a global constant for the “catch distance” – the distance which means the cat has caught the mouse – Another global constant defines the maximum number of moves in the game. There is a distance function that receives 4 values, one each for the column and a row of each animal and calculates distance between them. The function the returns the calculated distance There is a display function that receives 4 location values (the same columns and rows as were sent to the distance function) and a 5th parameter for the distance; that function displays the game status. This display function returns no value. The game play function generates random values for the columns and rows, then calls the distance calculating function and the display function. As long as this distance is greater than the “capture” value and the number of moves is less than the maximum number of moves, move the animals and call the distance function again. To move the animals, there are two more functions: one receives an animal’s column and speed as parameters and adds a random step value between the speed and it’s negative value. So if the maximum step is 5, generate a random value between -5 and 5: the animal can thus move left or right. The other function receives the animals row and speed, then moves it up or down in the same manner. Note that the animal may not move at all if the value is 0 – perfectly acceptable. When the new column or row has been calculated, each move function tests the new value to see if it is greater than the maximum value for the column or row (the global values you defined above) or less than the 1336 Programming Assignment 2 Page 2 of 3 minimum value, If so, the new value is assigned the value of the boundary it would have crosses. That is, if the animal hits the edge, it goes no further. After the entire moving loop in the game play function is completed, examine the situation. If the distance is less than or equal to the “capture” distance that’s what stopped the loop, so display a message indicating that the cat ate the mouse. If the distance is greater, then they must have made more than the maximum number of moves; display a message indicating that the cat got tired and gave up. All numbers are integers. When calculating the distance you may end up with a decimal value: convert that result to an integer before displaying it. The formula for calculating distance is: distance = square root ( (cat row – mouse row)2 + (cat column – mouse column)2 ) As before, your success (read, score) depends as much on style as on getting the correct numbers. Optional Improvements: You may wish to vary the borders, the speeds, and the capture distance by setting a variable to a random number between 0 and the constant value. In this way, the size of the field and the speeds of the animals might be different each time – your choice. It goes without saying that whatever the method, an animal’s step size must be less than the size of the field, and probably very much less to make the game interesting. You might want to make the moves more complicated than described. Suggestions are: only add the random step on either the row or the column, but not both. Or add half to the row and half to the column. Another possibility is to “wrap around”: if the calculated new row or column is greater than the maximum, subtract the maximum and set the row or column to that value. If the calculated new row or column is less than 0, add the negative calculated value to the maximum and set the row or column to that value. Extra Credit [70 points]: Instead of displaying the status by printing the animals positions and the distance between them, draw them. Use command line graphics or Turtle graphics as you wish. If you choose this option, create another library module called catAndMouseDisplay.py and write the animation function there. Also move the status display function to this module. Import this module to catAndMouseFunctions.py, not to the main program. That is, the main program imports the game play library module, and the game play library imports the display library. Special Notes: 1. Do not use lists, exceptions, or files, even if you know how to do those things. 2. The purpose of this assignment is to see if you have the skills to use only the Python tools described in chapters 1-5 to solve this problem. The next assignment will assess the other skills. 3. Using more advanced features demonstrates that you don’t know the simpler tools well enough, so using them will lower your score. 4. You earn up to 60% by correct operation and up to 40% with good style, readability, and documentation. Submit your solution (in Blackboard) as the files catAndMouse.py and catAndMouseFunctions.py. Name the files exactly as given. Do not add