1. Assignment Description Your assignment is to develop a manhunt simulator. The
ID: 3793090 • Letter: 1
Question
1. Assignment Description
Your assignment is to develop a manhunt simulator. The manhunt simulator will have two people. The investigator who will be demarked with an “I” and a target who will be demarked with a “T”. The target will not be directly visible on the map though.
The application should:
1. Have a starting menu that asks if the user wants to:
a.Load data from a text file
b.Ask the user for details of the investigator and the target. The information for each will include their starting point using an X and Y coordinate system.
2. Have a main menu that asks the user if they want to:
a.Search North
b.Search East
c.Search South
d.Search West
e.Exit
3. The four symbols on the map are: I = Investigator, T = Target, _ = Unsearched Area, and * = Searched Area.
4. You can only search in the four cardinal areas (north, south, east, west) there is no nw, ne, sw, se. It will indicate east/west first then north/south. For example, if the target is north-west of the target, it will tell the investigator to search west first. Then if the investigator is west enough, it will indicate north/south.
5. As part of the search, if the target is not found, it should indicate which direction should continue searching.
6. If the user finds the target, it should indicate that the target was found and exit the program.
7. The map will always have a width of 10 and a height of 10. (no magic numbers!). Additionally, the map must be stored in an array (it is up to you how many dimensions – one or more).
8. If the user tries to exit the search area, the program should indicate that search is not allowed and re-prompt the user.
9. The program exits after the target is found.
2. Requirements:
This is a list of the requirements of this application. For this project, it is up to you exactly how you want to implement it. In order for you to earn all of the points, however, you will need to meet all of the defined requirements.
Notice the trailing period is required (it says copy it into this folder).
· You must follow the coding standard as defined in the CMSC 202 coding standard (found on Blackboard under course materials). This includes comments as required.
· The project must be completed in C++. You may not use any libraries or data structures that we have not learned in class. Libraries we have learned include <iostream>, <fstream>, <iomanip>, <cmath>, <cstlib>, and <string>. You should only use namespace std.
· You must use a variety of functions including passing parameters to those functions and returning information from those functions. At least one time, an array must be passed to a function.
· Each menu must be implemented with a switch statement.
· All user input must be validated. For example, if a menu allows for 1, 2, or 3 to be entered and the user enters a 4, it will re-prompt the user. However, the user is expected to always enter the correct data type. i.e. If the user is asked to enter an integer, they will. If they are asked to enter a character, they will. You do not need to worry about checking for correct data types.
· The name of the input file is entered by the user. The input file name is not validated at all. The input file will always be in the same folder as the project file.
3. Recommendations
You are free to implement this with your own functions. While not required, these are some functions that you may want to include:
Start Menu (including validated user input) – Welcomes the user to the application and has the user choose between loading the map from a file, entering a new map, or exit. It must validate the choice to be between 1 and 3.
Main Menu (including user input) – Once the map is completed or loaded, this menu allows users to search in a particular direction or exit. It must validate the choice to be between 1 and 6.
Load Map – Uses file input to load in the text file. The file will always be named test1.txt. The text file will have exactly four inputs. One on each line. They are integers representing the X axis of the investigator, the Y axis of the investigator, the X axis of the target, and the Y axis of the target.
New Map – Allows the user to enter the X and Y coordinates of the starting investigator and the X and Y coordinates of the target. Fills the rest with underscores.
Print Map – Outputs the entire map including where the investigator currently is. Spaces that are unsearched are an underscore(_), the investigator is an “I”, and the target is demarked by a “T” but is not visible in the print map – the target is hidden!
Check Search – Checks the entire map to make sure that the direction that the user wants to search is legal and is not the target. *This is one of the trickier functions/sets of functions*
4. Sample Input and Output
For this project, input files are very simple. The input file includes an X and Y coordinate for the investigator and an X and Y coordinate for the target. For this project, the file will be named proj1.txt. You can code it as a constant. For example, proj1.txt would look like this:
5
5
1
1
In the sample output below, user input is colored blue for clarity. After compiling and running proj1, the output would look like this:
-bash-4.1$ ./proj1
Manhunt
What would you like to do?
1. Load a simulation from file
2. Start a new simulation
3. Exit
Enter 1 - 3:
If you have already created a proj1.txt file then you could choose 1. If you haven’t created a proj1.txt file, it will segfault and crash. You can create a proj1.txt file simply by using emacs proj1.txt. It simply loads the data into the application from proj1.txt.
If the user would choose 1 then the output would look like this:
Enter 1 - 3:
1
What is the name of the file?:
proj1.txt
Map loaded
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ I _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
What would you like to do?
1. Search North
2. Search East
3. Search South
4. Search West
5. See Map
6. Exit
Enter 1-6:
At this point, you don’t know which way to search because the target is hidden so you can try any direction. Ok, so if you tried to search north then it would try to move you north. Then it tells you which direction the target is in. The direction hint will direct the investigator east or west first then north or south second.
1
Try Searching West
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ I _ _ _ _
_ _ _ _ _ * _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
What would you like to do?
1. Search North
2. Search East
3. Search South
4. Search West
5. See Map
6. Exit
Enter 1-6:
If we go west a few times, it would then look like this:
4
Try Searching North
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ I * * * * _ _ _ _
_ _ _ _ _ * _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
What would you like to do?
1. Search North
2. Search East
3. Search South
4. Search West
5. See Map
6. Exit
Enter 1-6:
Notice, now it is telling us to search north. If we search north a few times, it looks like this:
1
Try Searching North
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ I _ _ _ _ _ _ _ _
_ * _ _ _ _ _ _ _ _
_ * * * * * _ _ _ _
_ _ _ _ _ * _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
What would you like to do?
1. Search North
2. Search East
3. Search South
4. Search West
5. See Map
6. Exit
Enter 1-6:
1
You found the target.
Once you have found the target, the program exits.
The second scenario is where you will enter a new map. For this scenario, you can enter the X and Y axis of the investigator and the X and Y axis of the target. Here is that code running:
-bash-4.1$ ./proj1
Manhunt
What would you like to do?
1. Load a simulation from file
2. Start a new simulation
3. Exit
Enter 1 - 3:
2
What is the X axis of the investigator?:
0
What is the Y axis of the investigator?:
4
What is the X axis of the target?:
2
What is the Y axis of the target?:
5
_ _ _ _ I _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
What would you like to do?
1. Search North
2. Search East
3. Search South
4. Search West
5. See Map
6. Exit
Enter 1-6:
From here the simulation would continue as above.
Finally, here are two examples of exiting the programs from the two menus. First, here is from the first menu:
-bash-4.1$ ./proj1
Manhunt
What would you like to do?
1. Load a simulation from file
2. Start a new simulation
3. Exit
Enter 1 - 3:
3
Giving up so soon?
Good-bye
Second, here is exit from the second menu:
-bash-4.1$ ./proj1
Manhunt
What would you like to do?
1. Load a simulation from file
2. Start a new simulation
3. Exit
Enter 1 - 3:
1
What is the name of the file?:
proj1.txt
Map loaded
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ I _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
What would you like to do?
1. Search North
2. Search East
3. Search South
4. Search West
5. See Map
6. Exit
Enter 1-6:
6
Giving up so soon?
Good-bye
Explanation / Answer
1.1 Simulation of Discrete Probabilities Probability In this chapter, we shall first consider chance experiments with a finite number of possible outcomes 1, 2, ..., n. For example, we roll a die and the possible outcomes are 1, 2, 3, 4, 5, 6 corresponding to the side that turns up. We toss a coin with possible outcomes H (heads) and T (tails). It is frequently useful to be able to refer to an outcome of an experiment. For example, we might want to write the mathematical expression which gives the sum of four rolls of a die. To do this, we could let Xi, i = 1, 2, 3, 4, represent the values of the outcomes of the four rolls, and then we could write the expression X1 + X2 + X3 + X4 for the sum of the four rolls. The Xi’s are called random variables. A random variable is simply an expression whose value is the outcome of a particular experiment. Just as in the case of other types of variables in mathematics, random variables can take on different values. Let X be the random variable which represents the roll of one die. We shall assign probabilities to the possible outcomes of this experiment. We do this by assigning to each outcome j a nonnegative number m(j ) in such a way that m(1) + m(2) + ··· + m(6)=1 . The function m(j ) is called the distribution function of the random variable X. For the case of the roll of the die we would assign equal probabilities or probabilities 1/6 to each of the outcomes. With this assignment of probabilities, one could write P(X 4) = 2 3 1 2 CHAPTER 1. DISCRETE PROBABILITY DISTRIBUTIONS to mean that the probability is 2/3 that a roll of a die will have a value which does not exceed 4. Let Y be the random variable which represents the toss of a coin. In this case, there are two possible outcomes, which we can label as H and T. Unless we have reason to suspect that the coin comes up one way more often than the other way, it is natural to assign the probability of 1/2 to each of the two outcomes. In both of the above experiments, each outcome is assigned an equal probability. This would certainly not be the case in general. For example, if a drug is found to be effective 30 percent of the time it is used, we might assign a probability .3 that the drug is effective the next time it is used and .7 that it is not effective. This last example illustrates the intuitive frequency concept of probability. That is, if we have a probability p that an experiment will result in outcome A, then if we repeat this experiment a large number of times we should expect that the fraction of times that A will occur is about p. To check intuitive ideas like this, we shall find it helpful to look at some of these problems experimentally. We could, for example, toss a coin a large number of times and see if the fraction of times heads turns up is about 1/2. We could also simulate this experiment on a computer. Simulation We want to be able to perform an experiment that corresponds to a given set of probabilities; for example, m(1)=1/2, m(2)=1/3, and m(3)=1/6. In this case, one could mark three faces of a six-sided die with an 1, two faces with an 2, and one face with an 3. In the general case we assume that m(1), m(2), . . . , m(n) are all rational numbers, with least common denominator n. If n > 2, we can imagine a long cylindrical die with a cross-section that is a regular n-gon. If m(j ) = nj/n, then we can label nj of the long faces of the cylinder with an j , and if one of the end faces comes up, we can just roll the die again. If n = 2, a coin could be used to perform the experiment. We will be particularly interested in repeating a chance experiment a large number of times. Although the cylindrical die would be a convenient way to carry out a few repetitions, it would be difficult to carry out a large number of experiments. Since the modern computer can do a large number of operations in a very short time, it is natural to turn to the computer for this task. Random Numbers We must first find a computer analog of rolling a die. This is done on the computer by means of a random number generator. Depending upon the particular software package, the computer can be asked for a real number between 0 and 1, or an integer in a given set of consecutive integers. In the first case, the real numbers are chosen in such a way that the probability that the number lies in any particular subinterval of this unit interval is equal to the length of the subinterval. In the second case, each integer has the same probability of being chosen. 1.1. SIMULATION OF DISCRETE PROBABILITIES 3 .203309 .762057 .151121 .623868 .932052 .415178 .716719 .967412 .069664 .670982 .352320 .049723 .750216 .784810 .089734 .966730 .946708 .380365 .027381 .900794 Table 1.1: Sample output of the program RandomNumbers. Let X be a random variable with distribution function m(), where is in the set {1, 2, 3}, and m(1)=1/2, m(2)=1/3, and m(3)=1/6. If our computer package can return a random integer in the set {1, 2, ..., 6}, then we simply ask it to do so, and make 1, 2, and 3 correspond to 1, 4 and 5 correspond to 2, and 6 correspond to 3. If our computer package returns a random real number r in the interval (0, 1), then the expression b6rc + 1 will be a random integer between 1 and 6. (The notation bxc means the greatest integer not exceeding x, and is read “floor of x.”) The method by which random real numbers are generated on a computer is described in the historical discussion at the end of this section. The following example gives sample output of the program RandomNumbers. Example 1.1 (Random Number Generation) The program RandomNumbers generates n random real numbers in the interval [0, 1], where n is chosen by the user. When we ran the program with n = 20, we obtained the data shown in Table 1.1. Example 1.2 (Coin Tossing) As we have noted, our intuition suggests that the probability of obtaining a head on a single toss of a coin is 1/2. To have the computer toss a coin, we can ask it to pick a random real number in the interval [0, 1] and test to see if this number is less than 1/2. If so, we shall call the outcome heads; if not we call it tails. Another way to proceed would be to ask the computer to pick a random integer from the set {0, 1}. The program CoinTosses carries out the experiment of tossing a coin n times. Running this program, with n = 20, resulted in: THTTTHTTTTHTTTTTHHTT. Note that in 20 tosses, we obtained 5 heads and 15 tails. Let us toss a coin n times, where n is much larger than 20, and see if we obtain a proportion of heads closer to our intuitive guess of 1/2. The program CoinTosses keeps track of the number of heads. When we ran this program with n = 1000, we obtained 494 heads. When we ran it with n = 10000, we obtained 5039 heads. 4 CHAPTER 1. DISCRETE PROBABILITY DISTRIBUTIONS We notice that when we tossed the coin 10,000 times, the proportion of heads was close to the “true value” .5 for obtaining a head when a coin is tossed. A mathematical model for this experiment is called Bernoulli Trials (see Chapter 3). The Law of Large Numbers, which we shall study later (see Chapter 8), will show that in the Bernoulli Trials model, the proportion of heads should be near .5, consistent with our intuitive idea of the frequency interpretation of probability. Of course, our program could be easily modified to simulate coins for which the probability of a head is p, where p is a real number between 0 and 1. In the case of coin tossing, we already knew the probability of the event occurring on each experiment. The real power of simulation comes from the ability to estimate probabilities when they are not known ahead of time. This method has been used in the recent discoveries of strategies that make the casino game of blackjack favorable to the player. We illustrate this idea in a simple situation in which we can compute the true probability and see how effective the simulation is. Example 1.3 (Dice Rolling) We consider a dice game that played an important role in the historical development of probability. The famous letters between Pascal and Fermat, which many believe started a serious study of probability, were instigated by a request for help from a French nobleman and gambler, Chevalier de M´er´e. It is said that de M´er´e had been betting that, in four rolls of a die, at least one six would turn up. He was winning consistently and, to get more people to play, he changed the game to bet that, in 24 rolls of two dice, a pair of sixes would turn up. It is claimed that de M´er´e lost with 24 and felt that 25 rolls were necessary to make the game favorable. It was un grand scandale that mathematics was wrong. We shall try to see if de M´er´e is correct by simulating his various bets. The program DeMere1 simulates a large number of experiments, seeing, in each one, if a six turns up in four rolls of a die. When we ran this program for 1000 plays, a six came up in the first four rolls 48.6 percent of the time. When we ran it for 10,000 plays this happened 51.98 percent of the time. We note that the result of the second run suggests that de M´er´e was correct in believing that his bet with one die was favorable; however, if we had based our conclusion on the first run, we would have decided that he was wrong. Accurate results by simulation require a large number of experiments. The program DeMere2 simulates de M´er´e’s second bet that a pair of sixes will occur in n rolls of a pair of dice. The previous simulation shows that it is important to know how many trials we should simulate in order to expect a certain degree of accuracy in our approximation. We shall see later that in these types of experiments, a rough rule of thumb is that, at least 95% of the time, the error does not exceed the reciprocal of the square root of the number of trials. Fortunately, for this dice game, it will be easy to compute the exact probabilities. We shall show in the next section that for the first bet the probability that de M´er´e wins is 1 (5/6)4 = .518. 1.1. SIMULATION OF DISCRETE PROBABILITIES 5 5 10 15 20 25 30 35 40 -10 -8 -6 -4 -2 2 4 6 8 10 Figure 1.1: Peter’s winnings in 40 plays of heads or tails. One can understand this calculation as follows: The probability that no 6 turns up on the first toss is (5/6). The probability that no 6 turns up on either of the first two tosses is (5/6)2. Reasoning in the same way, the probability that no 6 turns up on any of the first four tosses is (5/6)4. Thus, the probability of at least one 6 in the first four tosses is 1 (5/6)4. Similarly, for the second bet, with 24 rolls, the probability that de M´er´e wins is 1 (35/36)24 = .491, and for 25 rolls it is 1 (35/36)25 = .506. Using the rule of thumb mentioned above, it would require 27,000 rolls to have a reasonable chance to determine these probabilities with sufficient accuracy to assert that they lie on opposite sides of .5. It is interesting to ponder whether a gambler can detect such probabilities with the required accuracy from gambling experience. Some writers on the history of probability suggest that de M´er´e was, in fact, just interested in these problems as intriguing probability problems. Example 1.4 (Heads or Tails) For our next example, we consider a problem where the exact answer is difficult to obtain but for which simulation easily gives the qualitative results. Peter and Paul play a game called heads or tails. In this game, a fair coin is tossed a sequence of times—we choose 40. Each time a head comes up Peter wins 1 penny from Paul, and each time a tail comes up Peter loses 1 penny to Paul. For example, if the results of the 40 tosses are THTHHHHTTHTHHTTHHTTTTHHHTHHTHHHTHHHTTTHH. Peter’s winnings may be graphed as in Figure 1.1. Peter has won 6 pennies in this particular game. It is natural to ask for the probability that he will win j pennies; here j could be any even number from 40 to 40. It is reasonable to guess that the value of j with the highest probability is j = 0, since this occurs when the number of heads equals the number of tails. Similarly, we would guess that the values of j with the lowest probabilities are j = ±40.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.