CS 9E - Assignment 4.2

BASH Scripting: Cat & Mouse

Background

Overview

In this assignment, you will use the math library you created in the previous assignment to write a BASH script for a simulation.

Reading

Advanced Bash Scripting Guide: Sections 9-13, 25-26, 29-34

Cat & Mouse Primer

CatAndMouse.png

The scene is an urban park; a cat watches a mouse run around the base of a statue of the first computer. Over the course of a minute, the somewhat witless mouse moves one meter counterclockwise around the statue's base, which is circular and 2 meters in diameter. Every minute, the cat pursues the mouse as follows:

  • If the cat can see the mouse, the cat moves 1 meter toward the statue.
  • If the cat can't see the mouse, the cat circles 1.25 meters counterclockwise around the statue.

The cat plans eventually to get close enough to the mouse to make it a juicy lunch. The mouse by accident, however, may manage to keep completely out of sight of the cat, since both the cat and the mouse are moving counter-clockwise. Should the chase go on more than 30 minutes, however, the cat will get tired and wander off.

Trigonometric Information

All angles are in radians for the following information:

  • The cat sees the mouse if:

    1 <= (cat radius) * cos (cat angle - mouse angle)
    
  • When the cat circles distance around the statue, its radius does not change, and the change in its angle can be calculated from the relationship:

    angle = (distance) / (arc radius)
    
  • The cat catches the mouse when it (the cat) moves past the mouse while at the base of the statue, i.e. when the cat radius is 1.0 and the mouse angle lies between the old cat angle and the new cat angle. An angle B is between angles A and C iff both of the following criteria are met. (Note: The difference C-A is assumed to be less than 90 degrees, or pi/2 radians.):

    1) cos (B-A) > cos (C-A)
    2) cos (C-B) > cos (C-A).
    
  • Note that the cat cannot move inside the statue's base; hence if the cat is, say, at radius 1.7 and sees the mouse, it can move in only 0.7 meters, up to the base of the statue.

  • Remember that angles a, a + 2pi, a + 4pi, ... are all equal.

Why use BASH?

Indeed it is true that most experienced programmers, even those extremely versed in shell scripting, would not choose BASH to tackle a problem like this. However, the main reasons against using BASH for this type of problem in the real world present us with a unique educational opportunity to explore different aspects of shell programming. The mere fact that a simulation like this can be done using BASH and UNIX tools shows the power and versitility that shell programming has to offer.

Required Files

The file ~cs9e-1/hw/04.02-catmouse/catmouse-fw.sh contains the framework for this assignment. You are free to use it as a starting point or start from scratch if you desire.

Assignment

Part 1 - Floating Point Math

You should use the bashcalc-functions.sh file that you created in the previous assignment to provide basic math functionality in your Cat & Mouse simulator. Do this by including a line at the top of your script to source the file:

. bashcalc-functions.sh
Now, implement the following two functions which will be used later on in the simulation:
  1. angle_between (Hint: Look in the Trig Information above)
  2. does_cat_see_mouse (Hint: Look in the Trig Information above)

Bring in transcripts showing your tests for both of these functions. Make sure you test sufficient cases to be satisfied with the function's correctness.

Part 2 - Simulation Step

Now that you have the helper functions you need, it's time to write the heart of the simulation. Within the framework script, we have provided a framework for the function next_step. This function takes in the current state, step number, cat and mouse positions, and max steps and prints the state at the next step of the simulation.

Please complete the implementation of this function (you may start from scratch if you wish) and perform sufficient testing to catch all possible corner cases. Bring in a transcript of this testing for your tutor to examine.

Clarification on possible ambiguity:
  1. In each step, the cat moves first. If it moves past the mouse, it catches it.
  2. If the cat didn't catch the mouse, the mouse then moves.

Part 3 - Putting It All Together

And for the final step, you now need to make an executable script out of these functions which takes in the initial positions and a maximum number of steps to allow. Your script should print out the state (running/caught/given up) and positions at each step of the simulation.

Create a transcript of your tests of the simulator, and bring them in for your tutor to see. You should show at least one test case in which the mouse is caught without any movement (they start at the same location), one in which the cat catches the mouse on the very last step, and one in which the cat gives up but would've caught the mouse if he took one more step.

Assignment Checklist

Part 1

  • Student has correctly working code.
  • Student shows sufficient test cases for all helper functions.

Part 2

  • Student has correctly working code.
  • Student shows sufficient test cases to catch possible corner cases.

Part 3

  • Student has correctly working code.
  • Student shows sufficient test cases to catch possible corner cases:
    • catching the mouse at start
    • catching the mouse on the max step
    • catching the mouse on the max step + 1 (thus giving up)
    • etc

All

  • Student correctly uses quotes where appropriate
  • Student correctly uses integer, text, and floating point comparisons