In: Computer Science
(C++) D.Va's Mech System
D.Va is a former professional gamer who now uses her skills to pilot a state-of-the-art mech in defense of her homeland. Her real name is Hana and you can read more about her story here (Links to an external site.).
We are writing a program for D.Va's mech to load up before combat. D.Va's mech has two main systems, the micro missile system and the damage matrix system. The micro missile system is used to give damage to the enemies while the damage matrix system is used to absorb damage from enemies. You've seen how D.Va combats on the field during class and we are writing a short program to help D.Va prepare her mech systematically. The link of the video "Shooting Star" is here (Links to an external site.) in case you missed class.
D.Va will be fighting many enemy bots and one enemy boss for each combat. Each enemy bots may have different power values. D.Va would need a damage matrix system strong enough to take all damages from enemies and enough micro missiles to destroy all enemies. D.Va's mech has default power for both the damage matrix system and the micro missile system. If the default power isn't enough, our system would need to load more power to either or both of the systems for D.Va to win the combat.
System Detail
Our system will first have some variables with initial values indicating enemy information and some default value for D.Va’s mech. It will then run three steps to analyze and prepare the mech for combat. First step is to calculate how much power D.Va needs given the number of enemies she's facing in combat. The second step is to load D.Va's mech with required power to fight the combat. Finally, the system will write the combat report into a file that D.Va can review before she goes into combat.
1. Initialization
Your system should start with variables initialization as following:
//Enemy Information const int enemy_bots = 5; int enemy_bot_power[enemy_bots] = {2, 5, 3, 7, 1}; float enemy_boss_power = 27.24; //D.Va Default Spec int micro_missiles = 10; float defense_matrix_power = 100.0;
This would give you some enemy information including the number of enemy bots, their corresponding power(int), and enemy boss's power(float). This will also give you the default power of D.Va's mech including the number of micro missiles are loaded and how much power can the defense matrix absorb by default.
2. Calculate Power Needed
Then your system needs to calculate power needed for both the defense matrix system and the micro missile system. Please write two functions here, one to calculate the power needed by the defense matrix system and another to calculate the power needed by the micro missile system.
3. Load D.Va
Now that we know how much power is needed for both D.Va's systems, let's load the power to D.Va's mech. You would want to write two functions here with the same name load_dva. Both load_dva() functions should have type void and take in two parameters. Their behaviors are slightly different. This is where we would use function overloading.
Now in your main function you would have these two lines to load D.Va's mech:
//Load D.Va load_dva(defense_matrix_power, matrix_power_needed); load_dva(micro_missiles, missile_power_needed);
These two lines would update the value of defense_matrix_power and the micro_missiles.
4. Report
Finally, let's write a summary report for D.Va to read before she heads into the combat. Write a file name "report.txt" into the current directory. The content of the report should look likes this:
D.Va's Combat Report Combat with 5 enemy bots and one enemy boss with power 27.24. Loaded mech with 10 micro missiles and the defense matrix with power 144.96. Ready for combat!
Note that the number of enemy bots, the boss power, the number of missiles, and the defense matrix power in the file are all results calculated from your program and inserted into the file. Please don't hard code the output file. I may change the value from step1 to test your code to see whether the report makes sense. Take the results from your previous calculation to write into the report file.
Please make sure you have logic to check file open in your code and close the file after you finish the file operations.
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
// Calculating Matrix Power Needed
float total_damage(int enemy_bot_power[], int number_of_bots, float
enemy_boss_power)
{
float matrix_power_needed = 4 * enemy_boss_power;
for (int i = 0; i < number_of_bots; i++)
matrix_power_needed += 2 * enemy_bot_power[i];
return matrix_power_needed;
}
// Calculating Missile Power
template <typename T>
T missile_power(T enemy_power) { return 5 * enemy_power; }
// Function to Load Defense Matrix
void load_dva(float *defense_matrix_power, float
matrix_power_needed)
{
if (*defense_matrix_power < matrix_power_needed)
*defense_matrix_power = matrix_power_needed;
}
// Fuction to Load Micro Missiles
void load_dva(int *micro_missiles, float
missile_power_needed)
{
*micro_missiles = ceil(missile_power_needed / 100);
}
int main()
{
//Enemy Information
const int enemy_bots = 5;
int enemy_bot_power[enemy_bots] = {2, 5, 3, 7, 1};
float enemy_boss_power = 27.24;
//D.Va Default Spec
int micro_missiles = 10;
float defense_matrix_power = 100.0;
// Matrix and Missile Power Needed
int number_of_bots = sizeof(enemy_bot_power) /
sizeof(enemy_bot_power[0]);
float matrix_power_needed = total_damage(enemy_bot_power,
number_of_bots, enemy_boss_power);
float missile_power_needed =
missile_power<float>(enemy_boss_power);
for (int i = 0; i < number_of_bots; i++)
missile_power_needed +=
missile_power<int>(enemy_bot_power[i]);
//Load D.Va
load_dva(&defense_matrix_power, matrix_power_needed);
load_dva(µ_missiles, missile_power_needed);
// Writing Summary report in the file
ofstream myfile;
myfile.open("report.txt");
myfile << "D. Va's Combat Report\n";
myfile << "Combat with " << enemy_bots << " enemy
bots and one enemy boss with power " << enemy_boss_power
<< ".\n";
myfile << "Loaded mech with " << micro_missiles
<< " micro missiles and the defense matrix with power "
<< defense_matrix_power << ".\n";
myfile << "Ready for combat!";
myfile.close();
return 0;
}
***************†PLEASE DON'T FORGET TO GIVE THUMBS UP..