Even more good refactoring...

This commit is contained in:
Ognjen Milan Robovic 2024-06-16 09:18:51 -04:00
parent f6e9aff61b
commit 7cb380a4a8

View File

@ -9,7 +9,7 @@ static int construction [constructions] = { 1, 1, 1, 1 };
static char * resource_name [] = { "wheat", "gold", "wood", "stone" }; static char * resource_name [] = { "wheat", "gold", "wood", "stone" };
static char * construction_name [] = { "granary", "mine", "storehouse", "quarry" }; static char * construction_name [] = { "granary", "mine", "storehouse", "quarry" };
static int construction_cost [constructions] [resources] = { static int construction_price [constructions] [resources] = {
{ 0, 10, 60, 30 }, { 0, 10, 60, 30 },
{ 0, 120, 60, 10 }, { 0, 120, 60, 10 },
{ 0, 30, 10, 60 }, { 0, 30, 10, 60 },
@ -17,41 +17,64 @@ static int construction_cost [constructions] [resources] = {
}; };
static void build_construction (int index) { static void build_construction (int index) {
int cost; int price;
for (cost = 0; cost < resources; ++cost) { for (price = 0; price < resources; ++price) {
if (resource [cost] < construction_cost [index] [cost]) { if (resource [price] < construction_price [index] [price]) {
print ("You have only /3%i %s/- and you need /3%i/-, meaning that you miss /1%i/- more.\n", resource [cost], resource_name [cost], construction_cost [index] [cost], construction_cost [index] [cost] - resource [cost]); print ("You don't have enough /1%s/-, need /1%i/- more.\n", resource_name [price], construction_price [index] [price] - resource [price]);
print ("Your architects humbly refused to build %s, while complaining about lacking the resources for that.\n", construction_name [index]); print ("Your architect humbly refused to build /1%s/-.\n", construction_name [index]);
return; return;
} }
} }
for (cost = 0; cost < resources; ++cost) { for (price = 0; price < resources; ++price) {
resource [cost] -= construction_cost [index] [cost]; resource [price] -= construction_price [index] [price];
} }
construction [index] += 1; construction [index] += 1;
print ("Construction that you ordered to be built was finished without issues.\n", construction [index]); print ("Construction of /2%s/- was completed successfully.\n", construction_name [index]);
} }
static void print_resources (void) { static void print_resources (void) {
print ("Wheat = %i\n", resource [wheat]); int index;
print ("Gold = %i\n", resource [gold]);
print ("Wood = %i\n", resource [wood]); for (index = 0; index < resources; ++index) {
print ("Stone = %i\n", resource [stone]); if (resource [index] == 0) {
print ("You don't have any /1%s/-.\n", resource_name [index]);
} else if (resource [index] == 1) {
print ("You have /31/- unit of /3%s/-.\n", resource_name [index]);
} else {
print ("You have /3%i/- units of type /3%s/-.\n", resource [index], resource_name [index]);
}
}
}
static void print_constructions (void) {
int index;
for (index = 0; index < constructions; ++index) {
if (construction [index] == 0) {
print ("You don't have any construction of type /1%s/-.\n", construction_name [index]);
} else if (construction [index] == 1) {
print ("You have /31/- construction of type /3%s/-.\n", construction_name [index]);
} else {
print ("You have /3%i/- constructions of type /3%s/-.\n", construction [index], construction_name [index]);
}
}
} }
int main (void) { int main (void) {
print_resources (); print_resources ();
print_constructions ();
build_construction (granary); build_construction (granary);
build_construction (mine); build_construction (mine);
build_construction (storehouse); build_construction (storehouse);
build_construction (quarry); build_construction (quarry);
print_resources (); print_resources ();
print_constructions ();
return (log_success); return (log_success);
} }