// // DeathStarAttack.cpp // #include #include #include #include "Constants.h" #include "PrintFormattedPercentage.h" #include "DeathStarAttack.h" using namespace std; DeathStarAttackResults :: DeathStarAttackResults () : m_death_star_destroyed(0), m_death_star_survived(0), m_total(0) { assert(invariant(*this)); } bool DeathStarAttackResults :: invariant (const DeathStarAttackResults& results) { if(results.m_death_star_destroyed + results.m_death_star_survived != results.m_total) return false; return true; } DeathStarAttackResults calculateDeathStarAttackResults ( unsigned int aaa_attacker_frequencies[POSSIBLE_VALUE_COUNT][POSSIBLE_VALUE_COUNT][POSSIBLE_VALUE_COUNT], unsigned int a_attacker_value_count[3], unsigned int total_required) { assert(aaa_attacker_frequencies != NULL); assert(a_attacker_value_count != NULL); DeathStarAttackResults results; for(unsigned int az = 0; az < a_attacker_value_count[2]; az++) for(unsigned int ay = 0; ay < a_attacker_value_count[1]; ay++) for(unsigned int ax = 0; ax < a_attacker_value_count[0]; ax++) { unsigned int weight = aaa_attacker_frequencies[az][ay][ax]; // dice rolls are start at 1, but array indexes start at 0 unsigned int total = ax + ay + az + 3; if(total >= total_required) results.m_death_star_destroyed += weight; else results.m_death_star_survived += weight; results.m_total += weight; } assert(DeathStarAttackResults::invariant(results)); return results; } void printDeathStarAttackBlockHeader () { cout << endl; cout << endl; cout << endl; cout << "========================================================================" << endl; cout << " Attacks on the Death Star" << endl; cout << "========================================================================" << endl; } void printDeathStarAttackTableHeader (unsigned int total_required) { cout << endl; cout << "+--------------------+-----------------------------------+" << endl; cout << "| Roll Required:" << setw(3) << total_required; cout << " | Result for Death Star |" << endl; cout << "+--------------------+-----------------+-----------------+" << endl; cout << "| Attacker Ships | Destroyed | Survived |" << endl; cout << "+------+------+------+--------+--------+--------+--------+" << endl; cout << "| F | B | C | Value | Percent| Value | Percent|" << endl; cout << "+------+------+------+--------+--------+--------+--------+" << endl; } // // printDeathStarAttackTableRow // // Purpose: Prints a data row for a table with results for // attacking the death star. // Parameter(s): // <1> fighter_count: The number of fighters // <2> bomber_count: The number of bombers // <3> capital_count: The number of capital ships // <4> results: The probabilty distribution for the attack // results // Precondition(s): N/A // Returns: N/A // Side Effect: A row for thw table is printed showing the // probabilities described by results. This row // is for a force with fighter_count fighters, // bomber_count bombers, and capital_count capital // ships attacking the death star. // void printDeathStarAttackTableRow (unsigned int fighter_count, unsigned int bomber_count, unsigned int capital_count, const DeathStarAttackResults& results) { double destroyed_fraction = (double)(results.m_death_star_destroyed) / (double)(results.m_total); double survived_fraction = (double)(results.m_death_star_survived) / (double)(results.m_total); // print the support ships cout << "| " << setw(2) << fighter_count << " | " << setw(2) << bomber_count << " | " << setw(2) << capital_count; // print the results cout << " |" << setw(7) << results.m_death_star_destroyed << " |"; printAsFormattedPercentage(destroyed_fraction); cout << " |" << setw(7) << results.m_death_star_survived << " |"; printAsFormattedPercentage(survived_fraction); cout << " |" << endl; } void printDeathStarAttackTableEnd () { cout << "+------+------+------+--------+--------+--------+--------+" << endl; }