From f6e9aff61bcd692bc2a50367b76c6d29b1e699e5 Mon Sep 17 00:00:00 2001 From: xolatile Date: Sun, 16 Jun 2024 08:13:27 -0400 Subject: [PATCH] Refactored everything to use arrays and enumerations... --- xerbia.c | 100 ++++++++++++++++++++++----------------------------------------- 1 file changed, 34 insertions(+), 66 deletions(-) diff --git a/xerbia.c b/xerbia.c index 42803a4..208616b 100644 --- a/xerbia.c +++ b/xerbia.c @@ -1,87 +1,55 @@ #include -static int wheat = 600, - gold = 600, - wood = 600, - stone = 600; +enum { wheat, gold, wood, stone, resources }; +enum { granary, mine, storehouse, quarry, constructions }; -static int granaries = 1, - mines = 1, - storehouses = 1, - quarries = 1; +static int resource [resources] = { 600, 300, 300, 300 }; +static int construction [constructions] = { 1, 1, 1, 1 }; -static int check_resource (int have, int need, char * name) { - if (have < need) { - print ("You have only /3%i %s/- and you need /3%i/-, meaning that you miss /1%i/- more.\n", have, name, need, need - have); +static char * resource_name [] = { "wheat", "gold", "wood", "stone" }; +static char * construction_name [] = { "granary", "mine", "storehouse", "quarry" }; - return (true); +static int construction_cost [constructions] [resources] = { + { 0, 10, 60, 30 }, + { 0, 120, 60, 10 }, + { 0, 30, 10, 60 }, + { 0, 60, 30, 10 } +}; + +static void build_construction (int index) { + int cost; + + for (cost = 0; cost < resources; ++cost) { + if (resource [cost] < construction_cost [index] [cost]) { + 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 ("Your architects humbly refused to build %s, while complaining about lacking the resources for that.\n", construction_name [index]); + return; + } } - return (false); -} - -static void build_granary (void) { - if (check_resource (gold, 10, "gold") || check_resource (wood, 60, "wood") || check_resource (stone, 30, "stone")) { - print ("Your architects humbly refused to build the /1granary/-, complaining about lacking the resources for that.\n"); - } else { - gold -= 10; - wood -= 60; - stone -= 30; - - print ("/2Granary/- that you ordered to be built was finished without issues, now you have /2%i/- granaries.\n", ++granaries); + for (cost = 0; cost < resources; ++cost) { + resource [cost] -= construction_cost [index] [cost]; } -} -static void build_mine (void) { - if (check_resource (gold, 120, "gold") || check_resource (wood, 60, "wood") || check_resource (stone, 30, "stone")) { - print ("Your architects humbly refused to build the /1mine/-, complaining about lacking the resources for that.\n"); - } else { - gold -= 120; - wood -= 60; - stone -= 30; + construction [index] += 1; - print ("/2Mine/- that you ordered to be built was finished without issues, now you have /2%i/- mines.\n", ++mines); - } -} - -static void build_storehouse (void) { - if (check_resource (gold, 30, "gold") || check_resource (wood, 10, "wood") || check_resource (stone, 60, "stone")) { - print ("Your architects humbly refused to build the /1storehouse/-, complaining about lacking the resources for that.\n"); - } else { - gold -= 30; - wood -= 10; - stone -= 60; - - print ("/2Storehouse/- that you ordered to be built was finished without issues, now you have /2%i/- storehouses.\n", ++storehouses); - } -} - -static void build_quarry (void) { - if (check_resource (gold, 60, "gold") || check_resource (wood, 30, "wood") || check_resource (stone, 10, "stone")) { - print ("Your architects humbly refused to build the /1quarry/-, complaining about lacking the resources for that.\n"); - } else { - gold -= 60; - wood -= 30; - stone -= 10; - - print ("/2Quarry/- that you ordered to be built was finished without issues, now you have /2%i/- quarries.\n", ++quarries); - } + print ("Construction that you ordered to be built was finished without issues.\n", construction [index]); } static void print_resources (void) { - print ("Wheat = %i\n", wheat); - print ("Gold = %i\n", gold); - print ("Wood = %i\n", wood); - print ("Stone = %i\n", stone); + print ("Wheat = %i\n", resource [wheat]); + print ("Gold = %i\n", resource [gold]); + print ("Wood = %i\n", resource [wood]); + print ("Stone = %i\n", resource [stone]); } int main (void) { print_resources (); - build_granary (); - build_mine (); - build_storehouse (); - build_quarry (); + build_construction (granary); + build_construction (mine); + build_construction (storehouse); + build_construction (quarry); print_resources ();