Last active
July 8, 2020 17:17
-
-
Save arafin1/f0693c5ee9bceecb17a3ed8866d49199 to your computer and use it in GitHub Desktop.
Google C++ Exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include <math.h> | |
| #include <time.h> | |
| using namespace std; | |
| void StartUp(){ | |
| cout << "Welcome to Artillery."<<endl; | |
| cout <<"You are in the middle of a war and being charged by thousands of enemies."<<endl; | |
| cout <<"You have one cannon, which you can shoot at any angle."<<endl; | |
| cout <<"You only have 10 cannonballs for this target.."<<endl; | |
| cout <<"Let's begin..."<<endl; | |
| } | |
| int rand_distance(){ | |
| int random_number; | |
| // Initialize random seed. | |
| srand (time(NULL)); | |
| // Generate random number between 100 and 900 | |
| random_number = rand() % 900 + 100; | |
| return random_number; | |
| } | |
| double distance_calculation(double in_angle){ | |
| int distance; | |
| double time_in_air, Velocity, Gravity; | |
| Velocity = 200.0; | |
| Gravity = 32.2; | |
| time_in_air = (2.0 * Velocity * sin(in_angle)) / Gravity; | |
| return (int) round((Velocity * cos(in_angle)) * time_in_air); | |
| } | |
| int Fire(){ | |
| int enemy_distace = rand_distance(); | |
| double angle; | |
| double Pi = 3.1415; | |
| int distance; | |
| int counter = 0; | |
| cout<<"The enemy is "<< enemy_distace <<" feet away!!!"<<endl; | |
| do{ | |
| cout<<"What angle? "; | |
| cin>>angle; | |
| angle = (angle * Pi) / 180.0; //radian conversion | |
| distance = distance_calculation(angle); | |
| if(distance > enemy_distace){ | |
| cout<<endl; | |
| cout<<"Distance: -> "<<distance<<endl; | |
| cout<< "You over shot by "<<abs(enemy_distace - distance)<<endl; | |
| } | |
| if(distance < enemy_distace){ | |
| cout<<endl; | |
| cout<<"Distance: -> "<<distance<<endl; | |
| cout<< "You under shot by "<<abs(enemy_distace - distance)<<endl; | |
| } | |
| counter++; | |
| if(distance == enemy_distace){ | |
| cout<<"You hit him!!!"<<endl; | |
| cout<<"It took you "<<counter<<" shots."<<endl; | |
| cout<<"You have killed 1 enemy."<<endl; | |
| } | |
| if(counter > 10){ | |
| cout<<"You Fucked Up..."<<endl; | |
| return -1; | |
| } | |
| }while(distance != enemy_distace); | |
| return counter; | |
| } | |
| int main(){ | |
| StartUp(); // This displays the introductory script. | |
| int killed = 0; | |
| char done; | |
| do { | |
| killed = Fire();// Fire() contains the main loop of each round. | |
| cout << "I see another one, care to shoot again? (Y/N) " << endl; | |
| cin >> done; | |
| } while (done != 'n'); | |
| cout << "You killed " << killed << " of the enemy." << endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Welcome to Artillery. | |
| You are in the middle of a war and being charged by thousands of enemies. | |
| You have one cannon, which you can shoot at any angle. | |
| You only have 10 cannonballs for this target.. | |
| Let's begin... | |
| The enemy is 507 feet away!!! | |
| What angle? 25< | |
| You over shot by 445 | |
| What angle? 15 | |
| You over shot by 114 | |
| What angle? 10 | |
| You under shot by 82 | |
| What angle? 12 | |
| You under shot by 2 | |
| What angle? 12.01 | |
| You hit him!!! | |
| It took you 4 shots. | |
| You have killed 1 enemy. | |
| I see another one, are you ready? (Y/N) n | |
| You killed 1 of the enemy. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment