C Program: You will develop and implement a program that simulates an archery ta
ID: 3686665 • Letter: C
Question
C Program:
You will develop and implement a program that simulates an archery target shooting game.The primary objective is to gain experience in using: function decomposition to simplify implementing
a program; and random number generation with the library functions rand, srand, and time.
Program :The goal of the simulation is to shoot an arrow at a target consisting of three concentric circles as shown below: Hitting the white region (the center) is worth 5 points; hitting the green region is worth 3 points; and hitting the red region is worth 1 point. 0 points for missing all the 3 regions.your program will use a random number generator to determine where the arrows land.After each arrow is shot (that is after a random number is generated), your program displays a message describing the result. After each round (i.e., a set of 5 shootings) is completed, the total score for that round is displayed. In addition, you should ask the user if s/he wants to continue the simulation. The program continues the simulation as long as the user wants. At each round, the player shoots 5 arrows from different distances: 10, 20, 30, 40, and 50.
Functions
We use functions to break a problem into smaller sub-problems. This reduces program complexity. you are supposed to implement 4 distinct functions.
1. Main
2. chance
3. shoot
4. play.
The main function will be the driver of the program. You should create the loop that controls the rounds in the main function. After each iteration of the loop body, it should ask the player if s/he wants to continue. Please notice that you should start every iteration by calling the predefined functions srand and time. This will ensure that your simulator generates random numbers properly. However, please notice that you will generate the random number in the function chance.
The header (i.e., function declaration) of the function chance should be as follows:
double chance();
The function chance simply returns a random value between 0.0 and 1.0. The third function you need to develop is called shoot. This function takes the distance as an argument. It returns the score of the shooting by using the random value returned by the chance function and the distance based on the following table below.
Distance
Score: 5
Score: 3
Score: 1
Score: 0
10
0.0<= chance<0.50
0.50<=chance<0.90
0.90<=chance<0.95
0.95<=chance<=1.0
20
0.0<=chance< 0.40
0.40<=chance<0.80
0.80<=chance<0.90
0.90<=chance<=1.0
30
0.0<=chance< 0.30
0.30<=chance<0.60
0.60<=chance<0.75
0.75<=chance<=1.0
40
0.0<=chance< 0.20
0.20<=chance<0.40
0.40<=chance<0.60
0.60<=chance<=1.0
50
0.0<=chance <0.10
0.10<=chance<0.20
0.20<=chance<0.45
0.45<=chance<=1.0
The header (i.e., function declaration) of the function shoot should be as follows:
double shoot(int distance);
The last function you will develop is the play function. The function play calls the function shoot, and it is responsible for calculating and displaying messages (please see the output below) for a single round (i.e., a set of 5 shootings). The header (i.e., function declaration) of the function play should be as follows
void play();
Sample Output
Please notice that your output (scores/points) will probably be different because we use random values.
Welcome to Archery Shooting Simulator
Round 1:
Distance 10: 5 points, you are awesome!
Distance 20: 1 points
Distance 30: 0 points, what a shame!
Distance 40: 3 points
Distance 50: 5 points, you are awesome!
---------------------
Total Score:14 points
Enter 0 to exit, any other value to play again: 1
Distance
Score: 5
Score: 3
Score: 1
Score: 0
10
0.0<= chance<0.50
0.50<=chance<0.90
0.90<=chance<0.95
0.95<=chance<=1.0
20
0.0<=chance< 0.40
0.40<=chance<0.80
0.80<=chance<0.90
0.90<=chance<=1.0
30
0.0<=chance< 0.30
0.30<=chance<0.60
0.60<=chance<0.75
0.75<=chance<=1.0
40
0.0<=chance< 0.20
0.20<=chance<0.40
0.40<=chance<0.60
0.60<=chance<=1.0
50
0.0<=chance <0.10
0.10<=chance<0.20
0.20<=chance<0.45
0.45<=chance<=1.0
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
int counter = 0,wannaPlay=1;
double chance (); // I am calling all
double shoot (int distance); // my functions in here
void play (); // for using them into
// my main function.
int main () {
printf("Welcome to Archery Shooting Simulator ");
while (wannaPlay != 0){ // This loop is for us to keep play or not.
srand(time(NULL)); /* We are seeding our random . This will help us to change
our random value everytime we run it.
*/
play(); // Calling play function.
printf(" Enter 0 to exit, any other value to play again: ");
scanf("%d",&wannaPlay); // Keep looping or not ?
printf(" ");
}
}
double chance () {
int myChance = (rand()%100); // assigning random number to myChance variable
return (double)myChance/100; // making our chance between 0.0 to 1.0
}
double shoot (int distance) {
int score;
double chance1 = chance(); // Calling chance function
if (distance==10){ /* We will take distances from play function and we will
initialize them in this if-else statement so that we can
state our chances in different distances.
*/
if ((0.0 <= chance1) && (chance1 < 0.5)) { return score=5; }
else if ((0.5 <= chance1) && (chance1 < 0.9)) { return score=3; }
else if ((0.9 <= chance1) && (chance1 < 0.95)) { return score=1; }
else { return score=0; }
}else if (distance==20){
if ((0.0 <= chance1) && (chance1 < 0.4)) { return score=5; }
else if ((0.4 <= chance1) && (chance1 < 0.8)) { return score=3; }
else if ((0.8 <= chance1) && (chance1 < 0.9)) { return score=1; }
else { return score=0; }
}else if (distance==30){
if ((0.0 <= chance1) && (chance1 < 0.3)) { return score=5; }
else if ((0.3 <= chance1) && (chance1 < 0.6)) { return score=3; }
else if ((0.6 <= chance1) && (chance1 < 0.75)) { return score=1; }
else { return score=0; }
}else if (distance==40){
if ((0.0 <= chance1) && (chance1 < 0.2)) { return score=5; }
else if ((0.2 <= chance1) && (chance1 < 0.4)) { return score=3; }
else if ((0.4 <= chance1) && (chance1 < 0.6)) { return score=1; }
else { return score=0; }
}else{
if ((0.0 <= chance1) && (chance1 < 0.1)) { return score=5; }
else if ((0.1 <= chance1) && (chance1 < 0.2)) { return score=3; }
else if ((0.2 <= chance1) && (chance1 < 0.45)) { return score=1; }
else { return score=0; }
}
}
void play () { // This func. is for us to simulate our shoot's result
double shoot (); // We are calling the func. shoot
counter++; // This is for counting the rounds
printf("Round %d ",counter); // Printing the round
int distance[5] = {10,20,30,40,50}; /* We are collecting all five distances
in an array for entering them into the
shoot function.
*/
int totalScore = 0; /* Everytime that we start to count the totalScore have
to be zero.
*/
for (int i=0;i<5;i++){ // For passing our distances
int score=shoot(distance[i]); /* assigning shoot function's return value
-which is 'score'- to a variable named
score.
*/
totalScore+=score; // Adding all new scores to the total.
switch (score){ // We will print what we have as score in this distance.
case 5:
printf("Distance %d: 5 points, you are awesome! ",distance[i]);
break;
case 3:
printf("Distance %d: 3 points ",distance[i]);
break;
case 1:
printf("Distance %d: 1 points ",distance[i]);
break;
default:
printf("Distance %d: 0 points, what a shame! ",distance[i]);
break;
}
}
printf("--------------------- ");
printf("Total Score:%d points ",totalScore); // Printing total score.
}
sample output
Welcome to Archery Shooting Simulator
Round 1
Distance 10: 3 points
Distance 20: 5 points, you are awesome!
Distance 30: 1 points
Distance 40: 1 points
Distance 50: 5 points, you are awesome!
---------------------
Total Score:15 points
Enter 0 to exit, any other value to play again: 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.