CS 115 Program 3 Shooting Gallery Spring 2001

Design due in recitation, Friday, March 2, 2001 Due Date: Friday, March 9, 2001

You are commissioned to write an artillery training program for the military. The trainees have to learn about projectiles, and how the initial velocity and angle from the horizontal affect the distance the projectile will travel. You decide to write a game for the trainees to play.

There are two equations involved in this game, given below.
If a projectile starts with a certain velocity in feet per second, at a certain angle from the horizontal, in radians, the distance in feet that it travels is expressed as

```	distance =   velocity2* sin(2 * angle)
32.2
```
and to convert from degrees to radians,
```	radians =    degrees * 3.14159265
180.0
```

This formula does neglect the factor of air resistance.

The player will input a velocity and an angle in degrees (a more convenient unit). You will write one function that will convert from degrees to radians, and one function that will calculate the distance traveled by the projectile.

Phase I

The player is aiming at a target set at a fixed distance of 500 feet. After the player has entered their input, your program will tell them how much they missed the target by, and whether it was too short or too far. The player has made a "hit" if they come within 0.1% of the distance to the target. The player gets 5 tries to hit the target. Your program will tell the player whether they won or lost at the end.

Example interaction with the user:

```Target is 500 feet away.
You have to get within 0.50 feet

Trial # 1
Enter velocity (feet/sec) 130
Enter angle in degrees 45
524.84 feet
24.84 feet too far

Trial # 2
Enter velocity (feet/sec) 125
Enter angle in degrees 45
485.25 feet
14.75 feet not far enough

Trial # 3
Enter velocity (feet/sec) 127
Enter angle in degrees 45
500.90 feet
0.90 feet too far

Trial # 4
Enter velocity (feet/sec) 126.5
Enter angle in degrees 45
496.96 feet
3.04 feet not far enough

Trial # 5
Enter velocity (feet/sec) 126.75
Enter angle in degrees 45
498.93 feet
1.07 feet not far enough
You lose!!

```

Testing:
Try runs where the player wins, and ones where they do not win. Certainly, the angle can be varied from one shot to the next as well as the velocity. Be careful of the case where the player wins on the very last try. Write your program so that it would be easy to change the number of tries a player gets. (Hint: use a named constant)

Phase II

Once you have the problem above solved, you discover that it soon becomes easy to guess the velocity and angle that will win the game for a fixed distance. So add some variety to the game by using the random number generator. You must include a library file, stdlib.h. Then you call srand() with a non-negative integer argument. You can ask the user for this number. This is to "seed" the random number generator. You call srand only once per program run. Then call the rand() function to actually get a random number in the range of 0 to MAXINT. You can use this number to calculate a target distance, by an expression like Target = 500 + rand() % 200; which will yield numbers from 500 to 700.

Example interaction with the user:

```Please enter a seed: 55
Target is 520 feet away.
You have to get within 0.52 feet

Trial # 1
Enter velocity (feet/sec) 130
Enter angle in degrees 45
524.84 feet
4.84 feet too far

and so on
```
Testing:
You will notice that if you give the same seed for the random number, you will get the same target distance. This is a consequence of the way random numbers are generated in the computer. You should test your program with different seeds.

Phase III

Once you have Phase II working, then change your main function so that it is a function called game. It should be a boolean function, which returns true if the player won the game or false if they didn't. It does not need any parameters. Write a new main function that asks the player if they want to play a game, then calls the game function, and maintains count of how many games were played and how many the player won. This is reported when the player says they don't want to play again.

Example interaction with the user:

```Please enter a seed: 55
Do you want to play a game (y or n)? y

Target is 520 feet away.
You have to get within 0.52 feet

Trial # 1
Enter velocity (feet/sec) 130
Enter angle in degrees 45
524.84 feet
4.84 feet too far

and so on
for the first game
You won!!

Do you want to play a game (y or n)? y

Target is 592 feet away.
You have to get within 0.59 feet

and so on for the second game

You lost!!

Do you want to play a game (y or n)? n
You won 1 out of 2 games.
```

Design
You need to have a legible pseudocode design, for this program - at least Phases I and II, in one week. You will turn this in to your TA, so KEEP a COPY for your own use. The TA will not grade this, but will examine it, and make suggestions in recitation or the lecturer will talk about it in the next week.

Please read the documentation standard. As you can see from looking at the grading sheet, we will be looking to see how you meet these standards.

As described in the documentation standard, turn in the following, neatly stapled, in this order:

• Printout of the grading sheet for this assignment
• Pseudocode design of the solution for the problem
• Printout of the source program
• Captures of the test runs, labeled with what you are testing