Cop and robber game

This is a matlab exercise to simulate the following game, in which a thief tries to steal a treasure that is located in a room and a number of cops try and capture him before he does so. Neither the thief nor the cops know exactly where the treasure is located. The treasure is invisible because it has been cloaked, but the thief has an anti-cloaking device that allows him to see the treasure, if he is within a certain distance of the treasure.
The room is square, with the size of each side being an even integer. The centre of the room has the co-ordinates (0,0). There is a circle with origin at the centre of the room, the circumference of which provides random points for people to enter the room. The position on the circumference is determined by generating a uniform random angle between 0 and 2*pi. People enter the room at random times determined by generating a uniform random integer in the range [0..max_entry_time]. This time is the elapsed time with respect to the entry of the previous person. Time is counted in the form of discrete time steps.
A thief enters the room at a random time and a random point on the cirumference of the entry circle, as specified above, after the start of the game. A given number of cops enter in the same way, one after the other, at random times and points, again as specified above. The room is divided into a square grid, each cell of which is a square with sides of size 1cm. A cop/thief can move one grid point at a time, in one of four directions (north, east, south, or west). Once they reach a wall, they can only move along the wall, or back into the room. They can choose not to move as well.
Each person, cop or thief, has a sensor range, a circle of a specified radius, within which he can sense the presence of another person (thief or cop) or of the wall. A cop will not move into a position that is within the sensor range of another cop. If a cop knows the position of the thief, then he can't pass on this information to another cop, unless he is within the sensor range of that cop.
The thief moves randomly until he locates the treasure. To do so, the treasure has to be in the range of his anti-cloaking device. He steals the treasure if he moves to the same position as the treasure. The thief is captured if a cop moves into the same position as the thief. The time limit for the game, that is, the maximum number of time steps for which the game can run, is specified. The game ends when the thief steals the treasure or is captured or the time limit is exceeded..
You can assume any strategy you like for the movement of the thief and cops. The thief should try and avoid capture and the cops should try to capture the thief.
The initial configuration is to be read from a file, which has the information in the following order (note that the origin (0,0) is located at the centre of the room):
  • Size of side of square room (even integer), in m
  • Radius of entry circle, in m
  • Position of treasure (x and y coords), in m
  • Range of anti-cloaking device, in cm
  • Time limit for simulation, integer
  • Time limit for each person's entry (max time steps), integer
  • Thief sensor range (radius), in cms
  • Number of cops, k, an integer
  • Cop 1 sensor range (radius), in cms
  • Cop 2 sensor range (radius), in cms
  • ...
  • Cop k sensor range (radius), in cms
A line starting with a '#' should be treated as a comment line and ignored. You should ask the user for the file name of the configuration file. You should read this file and extract the required parameters from the file. Include error checking, so that you report an error if you don't get a sufficient number of input parameters, or you are given incorrect input (eg, negative numbers, where it should be positive). A sample file is provided.
You need to, in effect, do an animation of the game. Draw the position of the thief, the cops and the treasure, at each time step. You can use the drawnow function command for this purpose. You can use the pause command to force a pause between plots. Remember to erase the previous positions when you draw the new positions. Explore how this can be done (you can use a number of different approaches and is quite easy to do. For example, create a new plot each time, although this is inefficient).
Use filled circles to indicate the different items on the plot - red for the thief, blue for the cops and green for the treasure. Once the thief has grabbed the treasure, change his color to brown; the treasure circle should then disappear. Show the boundary of the room as a thick black line.
Design your program keeping in mind functionality, the potential for reuse and efficiency of code. Your program should be in a file called crgame.m. You may use as many functions as required. Functions that you think can be reused must be located in separate files. Document your code as appropriate and write a short report on your approach to solving the problem and on the design of your program in this file, in the form of comments.

1 Comment

the cyclist
the cyclist on 19 Sep 2011
This looks like a really fun game! Thanks for sharing your assignment with us! Good luck with it. Also, as you work on it, if you have some specific MATLAB questions, and you can post some code that you worked on yourself, we'd be happy to help.

Sign in to comment.

Answers (1)

The program you have commanded us to do ("You are to..." is a command, not a request for assistance), has at some flaws in its formulation. You write,
A cop will not move into a position that is within the sensor range of another cop. If a cop knows the position of the thief, then he can't pass on this information to another cop, unless he is within the sensor range of that cop.
Question 1: if cop A is not currently within sensor range of cop B, then how is cop A to know that its move will not take it to within sensor range of cop B -- unless, that is, each cop already knows exactly where each other cop is?
Question 2: since each cop has a different sensor range, how is cop A to know whether any particular movement would be to within sensor range of cop B, unless each cop can tell at a distance (from outside sensor range) what the other cop's sensor range is?
Question 3: "a cop will not move to within sensor range of another cop" -- does that apply to source or destination? That is, if cop A's range is 3 and copy B's range is 5, then if cop A moves to 4 units from B, B would not be within the sensor range of A, but A would be within the sensor range of B. Which of the two is not allowed?
Question 4: "If a cop knows the position of the thief, then he can't pass on this information to another cop, unless he is within the sensor range of that cop." -- but it sounds like from the paragraph before, a cop is not allowed to come in to within sensor range of another cop, so how could that apply? If there is an exemption about "I can see you but you can't see me" or that sort of thing as explored above, then does the transmission have to be one-way?

Categories

Find more on MATLAB Mobile in Help Center and File Exchange

Asked:

on 19 Sep 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!