If you had 10 cases of upgrade, that'd be a lot of coding...
To me, each case statement looks somewhat similar, with just a few arguments being different in each.
Functions can reduce the size of code, if you decide to make more building types; good idea if multiple lines of code share nearly identical functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
switch(temp)
{
case 1: //Upgrade HQ
void buildQueue(/*building_id*/,/*nobuilding*/,/*buildingTurnsLeft*/,/*vector*/,/*HQ*/./*iron*/./*wood*/./*clay*/)
case 2: //Upgrade Timber Camp;
void buildQueue(/*building_id*/,/*nobuilding*/,/*buildingTurnsLeft*/,/*vector*/,/*Timber_Camp*/./*iron*/./*wood*/./*clay*/)
case 3:
...
case 4::
...
...
...
//Enabling many more cases with a maintainable number of lines.
//Functions are a bit slower than hard-code
//Function can cause heap allocation if using recursion
|
Hiding code doesn't improve performance and makes code actually harder to interpret/predict, but can shorten code dramatically - and sometimes improve functionality.
1 2 3 4 5 6 7 8 9 10 11 12
|
//if(hq >= 2)
//{
cout << /*vector_string[2]*/ << /*vector_building[2]*/ << ")\t\t" << "0" << "\t" << "0" << "\t" << "0" << "\t" << "0" << endl;
//}
//requires no if condition, just prints what's in the string_vector at index 2 and vector_building at index 2.
//example: cout << /*vector_string[5]*/ << /*vector_building[5]*/ << ")\t\t" << "0" << "\t" << "0" << "\t" << "0" << "\t" << "0" << endl;
//example: cout << /*vector_string[10]*/ << /*vector_building[10]*/ << ")\t\t" << "0" << "\t" << "0" << "\t" << "0" << "\t" << "0" << endl;
//Again, functions aren't as efficient as direct hard-code, but it can reduce code length. Also, in this case, it nullify the use of testing against multiple conditions.
//If you had 10 cases here, it'd take 10 comparison before being able to check the last one, just to print.
|
The above reasoning can be applied to the switch below as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
switch(buildingID) //Display the building that is in the build queue
{
case nobuilding:
cout << "No buildings currently in production.";
break;
case HQ:
cout << "Headquarters: " << hq + 1 << "\t\t" << buildingTurnsLeft;
break;
case BARRACKS:
cout << "Barracks: " << barracks + 1 << "\t\t" << buildingTurnsLeft;
break;
case STABLES:
cout << "Stables: " << stables + 1 << "\t\t" << buildingTurnsLeft;
break;
case TIMBER:
cout << "Timber Camp: " << timbercamp + 1 << "\t\t" << buildingTurnsLeft;
break;
case CLAYP:
cout << "Clay Pit: " << claypit + 1 << "\t\t" << buildingTurnsLeft;
break;
case IRONM:
cout << "Iron Mine: " << ironmine + 1 << "\t\t" << buildingTurnsLeft;
break;
case SMITHY:
cout << "Smithy: " << smithy + 1 << "\t\t" << buildingTurnsLeft;
break;
}
|
So, your overall code may look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
//displays and handles HQ
void Nation::openHQ(void)
{
bool inHQ = true;
while(inHQ)
{
cls();
int temp;
cout << "~~~~~~~~~~~~~~~VILLAGE HEADQUARTERS~~~~~~~~~~~~~~~" << endl;
cout << "\nBUILDING PROGRESS\tTurns till completion\n\n\n";
if (buildingID != nobuilding)
{
cout << /*vector_string_buildingName[buildingID]*/ << /*building_count?*/ + 1 << "\t\t" << buildingTurnsLeft;
}
else
{
cout << "No buildings currently in production.";
}
//Display the buildings you can upgrade
cout << "\n\n\n\nBUILDING(Current lvl)\tWOOD\tCLAY\tIRON\tTurns to complete";
cout << "\n\n\n1) Headquarters(" << hq << ")\t" << hqcost[hq][WOOD] << "\t" << hqcost[hq][CLAY] << "\t" << hqcost[hq][IRON] << "\t" << hqcost[hq][TTC] << endl;
cout << "2) Timber Camp(" << timbercamp << ")\t" << resourcecampcost[timbercamp][WOOD] << "\t" << resourcecampcost[timbercamp][CLAY] << "\t" << resourcecampcost[timbercamp][IRON] << "\t" << resourcecampcost[timbercamp][TTC] << endl;
cout << "3) Clay Pit(" << claypit << ")\t\t" << resourcecampcost[claypit][WOOD] << "\t" << resourcecampcost[claypit][CLAY] << "\t" << resourcecampcost[claypit][IRON] << "\t" << resourcecampcost[claypit][TTC] << endl;
cout << "4) Iron Mine(" << ironmine << ")\t\t" << resourcecampcost[ironmine][WOOD] << "\t" << resourcecampcost[ironmine][CLAY] << "\t" << resourcecampcost[timbercamp][IRON] << "\t" << resourcecampcost[ironmine][TTC] << endl;
cout << /*vector_string[2]*/ << /*vector_building[2]*/ << ")\t\t" << "0" << "\t" << "0" << "\t" << "0" << "\t" << "0" << endl;
//5,6,7 - will sometimes not print out, and still print the below - original design
cout << "8) Cancel build queue" << endl;
cout << "9) Leave Headquarters" << endl;
cout << "\nChoice: ";
cin >> temp;
switch(temp)
{
case 1: //Upgrade HQ
void buildQueue(/*building_id*/,/*nobuilding*/,/*buildingTurnsLeft*/,/*vector*/,/*HQ*/./*iron*/./*wood*/./*clay*/) //You should know what build_queue does.
case 2: //Upgrade Timber Camp;
void buildQueue(/*building_id*/,/*nobuilding*/,/*buildingTurnsLeft*/,/*vector*/,/*Timber_Camp*/./*iron*/./*wood*/./*clay*/) //You should know what build_queue does.
//case 3:
//...
//case 4::
//...
//...
//...
|
As programmers, we're always looking for patterns. Encapsulating data into an object organizes it and gives us the possibility of morphing it. Our generic would be the parent class which has the standard data and functionality.....
<Edited, removed essay>
A possible class below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//class building
//protected:
//string building name
//int building id
//bool building busy?
//public:
//standard building member functions
//class barrack derived(child) from building
//protected:
//barrack unique data
//public:
//barrack unique member functions
|
I'm not trying to change how you program in any way, just giving my perspective on possible improvements.
Code was done through the browsers text editor.
And as you mentioned:
"I am determined to actually finish a C++ project in my lifetime"
Learning something new is good, but it can be overwhelming and cause frustration. You should work with what you know, and finish your project. Maintenance and redesign can be implemented later, though this should've been thought through before the actual creation of the program. =D
Good Luck