Query changes...

This commit is contained in:
Ognjen Milan Robovic 2024-06-16 15:47:45 -04:00
parent 511f55fbad
commit c6d1bcc7ab

View File

@ -45,6 +45,31 @@ static void separate (void) {
print ("/0------------------------------------------------------------------------------------------/-\n"); print ("/0------------------------------------------------------------------------------------------/-\n");
} }
static int query (char * names [], int count) {
char input [1024] = "";
int index = reply_help;
requery: for (index = 0; index < 1024; ++index) {
in (& input [index], 1);
if (input [index] == '\n') {
break;
}
}
input [index] = '\0';
for (index = 0; index < count; ++index) {
if (string_compare (input, names [index]) == true) {
return (index);
}
}
print ("Incorrect /1reply/-, type '/3help/-' to list replies or look at message above.\n");
goto requery;
}
static void print_help (void) { static void print_help (void) {
int index; int index;
@ -96,54 +121,36 @@ static void print_statistics (void) {
separate (); separate ();
} }
static void build_construction (int index) { static void build_construction (void) {
int price; int index;
int reply;
for (price = 0; price < resources; ++price) { for (index = 0; index < constructions; ++index) {
if (resource [price] < construction_price [index] [price]) { print ("/0->/- build /3%s/- ()\n", construction_name [index]);
print ("You don't have enough /1%s/-, need /1%i/- more.\n", resource_name [price], construction_price [index] [price] - resource [price]); }
print ("Your architect humbly refused to build /1%s/-.\n", construction_name [index]);
reply = query (construction_name, constructions);
for (index = 0; index < resources; ++index) {
if (resource [index] < construction_price [reply] [index]) {
print ("You don't have enough /1%s/-, need /1%i/- more.\n", resource_name [index], construction_price [reply] [index] - resource [index]);
print ("Your architect humbly refused to build /1%s/-.\n", construction_name [reply]);
separate (); separate ();
return; return;
} }
} }
for (price = 0; price < resources; ++price) { for (index = 0; index < resources; ++index) {
resource [price] -= construction_price [index] [price]; resource [index] -= construction_price [reply] [index];
} }
construction [index] += 1; construction [reply] += 1;
print ("Construction of /2%s/- was completed successfully.\n", construction_name [index]); print ("Construction of /2%s/- was completed successfully.\n", construction_name [reply]);
separate (); separate ();
} }
static int query (void) {
char input [1024] = "";
int index = reply_help;
requery: for (index = 0; index < 1024; ++index) {
in (& input [index], 1);
if (input [index] == '\n') {
break;
}
}
input [index] = '\0';
for (index = 0; index < replies; ++index) {
if (string_compare (input, reply_name [index]) == true) {
return (index);
}
}
print ("Incorrect /1reply/-, type '/3help/-' to list replies.\n");
goto requery;
}
int main (void) { int main (void) {
int index; int index;
int reply; int reply;
@ -161,18 +168,18 @@ int main (void) {
construction [index] = random (1, 2); construction [index] = random (1, 2);
} }
for (reply = reply_help; reply != reply_quit; reply = query ()) { for (reply = reply_help; reply != reply_quit; reply = query (reply_name, replies)) {
separate (); separate ();
switch (reply) { switch (reply) {
case reply_help: print_help (); break; case reply_help: print_help (); break;
case reply_report: print_resources (); break; case reply_report: print_resources (); break;
case reply_status: print_statistics (); break; case reply_status: print_statistics (); break;
case reply_build: build_construction (); break;
default: break; default: break;
} }
} }
build_construction (granary);
print_constructions (); print_constructions ();
separate (); separate ();