Question

In: Computer Science

Upsetfowl inC++ knock­off version of the Angry Birds game. The starter program is a working first...

Upsetfowl inC++

knock­off version of the Angry Birds game. The starter program is a working first draft of the game.

1. Correct the first FIXME by moving the intro text to a function named PrintIntro. Development suggestion: Verify the program has the same behavior before continuing.

2. Correct the second FIXME. Notice that the function GetUsrInpt will need to return two values: fowlAngle and fowlVel.

3. Correct the third FIXME. Notice that the function LaunchFowl only needs to return the value fowlLandingX, but needs the parameters fowlAngle and fowlVel.

4. Correct the fourth FIXME. Notice that the function DtrmnIfHit only needs to return the value didHitSwine, but needs the parameters fowlLandingX and swineX. The main should now look like the following code and the program should behave the same as the first draft: intmain(){ doublefowlAngle=0.0;

//angleoflaunchoffowl(rad) doublefowlVel=0.0;//velocityoffowl(m/s) doubleswineX=0.0;//distancetoswine(m) doublefowlLandingX=0.0;

//fowl’shoriz.dist.fromstart(m) booldidHitSwine=false;

//didhittheswine? srand(time(0)); swineX=(rand()%201)+50; PrintIntro(); GetUsrInpt(swineX,fowlAngle,fowlVel); fowlLandingX=LaunchFowl(fowlAngle,fowlVel); didHitSwine=DtrmnIfHit(fowlLandingX,swineX); return0; }

5. Modify the program to continue playing the game until the swine is hit. Add a loop in main that contains the functions GetUsrInpt, LaunchFowl, and DtrmnIfHit.

6. Modify the program to give the user at most 4 tries to hit the swine. If the swine is hit, then stop the loop. Here is an example program execution (user input is highlighted here for clarity):

WelcometoUpsetFowl! TheobjectiveistohittheMeanSwine. TheMeanSwineis84metersaway. Enterlaunchangle(deg):45 Enterlaunchvelocity(m/s):30 Time 1 x= 0 y= 0 Time 2 x= 21 y= 16 Time 3 x= 42 y= 23 Time 4 x= 64 y= 20 Time 5 x= 85 y= 6 Time 6 x=106 y=-16 Missed'em...

TheMeanSwineis84metersaway. Enterlaunchangle(deg):45 Enterlaunchvelocity(m/s):25 Time 1 x= 0 y= 0 Time 2 x= 18 y= 13 Time 3 x= 35 y= 16 Time 4 x= 53 y= 9 Time 5 x= 71 y= -8 Hit'em!!!

Solutions

Expert Solution

Solution:

Givendata:

Source Code for main.cpp:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

//Global Constants
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)


// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y)
{
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}


// convert degree value to radians
double DegToRad(double deg)
{
return ((deg * pi) / 180.0);
}


// print time, x, and y values
void PrintUpdate(double t, double x, double y)
{
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}

// Prints game's intro message
void PrintIntro()
{
cout << "Welcome to Upset Fowl!\n";
cout << "The objective is to hit the Mean Swine.\n";
return;
}

// Given swine's current horiz. position
// Get user's desired launch angle and velocity for fowl
void GetUsrInpt(double swineX, double &fowlAngle, double &fowlVel)
{
//code that gets from the user the fowl's launch angle and velocity
cout << "\nThe Mean Swine is " << swineX << " meters away.\n";

cout << "Enter launch angle (deg): ";
cin >> fowlAngle;
cout << endl;
fowlAngle = DegToRad(fowlAngle); // convert to radians

cout << "Enter launch velocity (m/s): ";
cin >> fowlVel;
cout << endl;
  
return;
}


// Given fowl launch angle and velocity
// Return horiz. landing position of fowl
double LaunchFowl(double fowlAngle, double fowlVel)
{
//code that calculates and returns the horiz. landing position of fowl
double fowlLandingX = 0.0;
double fowlX = 0.0;
double fowlY = 0.0;
double t = 1.0;

do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t=t+1.0;
} while ( fowlY > 0.0 ); // while above ground
PrintUpdate(t, fowlX, fowlY);


fowlLandingX = fowlX;

return fowlLandingX;
}


// Given fowl's horiz. landing position and swine's horiz. position
// Return whether fowl hit swine or not
bool DtrmnIfHit(double fowlLandingX, double swineX)
{
// FIXME Add code that returns true if fowl hit swine or false if not

double beforeSwineX = 0;
bool didHitSwine = false;

beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX))
{
cout << "Hit'em!!!" << endl;
didHitSwine = true;
return didHitSwine;
}
else
{
cout << "Missed'em..." << endl;
didHitSwine = false;
return didHitSwine;
}


}

int main()
{

// double t = 1.0; // time (s)
//double fowlY = 0.0; // object's height above ground (m)
double fowlAngle = 0.0; // angle of launch (rad)
double fowlVel = 0.0; // velocity (m/s)
// double fowlX = 0.0;
double fowlLandingX = 0.0; // object's horiz. dist. from start (m)
double swineX = 40.0; // distance to swine (m)
//double beforeSwineX = 0.0; // distance before swine that is acceptable as a hit (m)
//bool didHitSwine = false; // did hit the swine?
int tries = 4;
int didHit = 0;

srand(20); //to ensure the correct output for grading
swineX = (rand() % 201) + 50;

  
//call PrintIntro here
PrintIntro();
  
for(int i = 0; i < tries; i++)
{
//call GetUsrInpt here
GetUsrInpt(swineX, fowlAngle, fowlVel);
  
//call LaunchFowl here
fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
  
//call DtermnIfHit here
didHit = DtrmnIfHit(fowlLandingX, swineX);
  
if (didHit == 1)
{
return 0;
}
  

}

return 0;
}

Sample Output Screenshots:

PLEASE GIVEME THUMBUP........


Related Solutions

Angry? Birds, Inc. designs and builds retaining walls for individual customers. On August? 1, 2 jobs...
Angry? Birds, Inc. designs and builds retaining walls for individual customers. On August? 1, 2 jobs were in?process: Level 1 with a beginning balance of? $12,000 and Level 2 with a beginning balance of? $8,600. During? August, Level 3 and Level 4 were started. Levels 1 and 2 were sold at cost plus 40?% during August. All other jobs remained in process at month end. The company applies manufacturing overhead at a rate of ?$99 per direct labor hour. Direct...
The following is a C program that is a video game version of Connect 4. The...
The following is a C program that is a video game version of Connect 4. The code works fine as is now but I want to change the way you input which column you drop a disk into. Currently, you type in 1 and it goes into column 1. If you type in 6, it goes into column 6 and so on. But I want to make it so you input A or a, then it goes into column 1...
Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
C program simple version of blackjack following this design. 1. The basic rules of game A...
C program simple version of blackjack following this design. 1. The basic rules of game A deck of poker cards are used. For simplicity, we have unlimited number of cards, so we can generate a random card without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher total of points than the dealer’s, but without going over 21. The...
The program will first prompt the user for a range of numbers. Then the game will...
The program will first prompt the user for a range of numbers. Then the game will generate a random number in that range. The program will then allow the user to guess 3 times. Each time the person takes a guess, if it is not the number then the game should tell them to guess higher or lower. They only get 3 guesses. If they have not guessed the number in three tries then the game should tell them the...
The program will first prompt the user for a range of numbers. Then the game will...
The program will first prompt the user for a range of numbers. Then the game will generate a random number in that range. The program will then allow the user to guess 3 times. Each time the person takes a guess, if it is not the number then the game should tell them to guess higher or lower. They only get 3 guesses. If they have not guessed the number in three tries then the game should tell them the...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
Using steps.py in this repository for a starter file, write a Python program to calculate average...
Using steps.py in this repository for a starter file, write a Python program to calculate average number of steps per month for a year. The input file (which you will read from the command line, see the sample program on how to read command line arguments in this repository) contains the number of steps (integer) a person took each day for 1 year, starting January 1. Each line of input contains a single number. Assume this is NOT a leap...
You are playing a version of the roulette game, where the pockets are from 0 to...
You are playing a version of the roulette game, where the pockets are from 0 to 10 and even numbers are red and odd numbers are black (0 is green). You spin 3 times and add up the values you see. What is the probability that you get a total of 15 given on the first spin you spin a 2? What about a 3? Solve by simulation and analytically.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT