From 254a6c46c95395a1e2f4548febef3487df7462e6 Mon Sep 17 00:00:00 2001 From: xolatile Date: Sun, 16 Jun 2024 13:31:34 -0400 Subject: [PATCH] Implementing input system... --- xerbia.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/xerbia.c b/xerbia.c index ae47b08..a43afb8 100644 --- a/xerbia.c +++ b/xerbia.c @@ -2,6 +2,7 @@ enum { wheat, gold, wood, stone, resources }; enum { granary, mine, storehouse, quarry, constructions }; +enum { reply_invalid, reply_build, reply_report, reply_status, replies }; static int population = 0; static int reputation = 0; @@ -13,6 +14,7 @@ static int construction [constructions] = { 0 }; static char * resource_name [] = { "wheat", "gold", "wood", "stone" }; static char * construction_name [] = { "granary", "mine", "storehouse", "quarry" }; +static char * reply_name [] = { "--", "build", "report", "status" }; static int construction_price [constructions] [resources] = { { 0, 10, 60, 30 }, @@ -89,8 +91,36 @@ static void print_statistics (void) { separate (); } +static int query (void) { + char input [1024] = ""; + + int index; + + 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) { + print ("-- /2%s/-\n", reply_name [index]); + return (index); + } + } + + print ("/1Unknown kind of reply, type 'help' to see valid replies./-\n"); + + return (reply_invalid); +} + int main (void) { int index; + int reply; reputation = random (0, 10); monthly_immigration = random (0, 10); @@ -105,17 +135,19 @@ int main (void) { construction [index] = random (1, 2); } - separate (); + for (reply = replies; reply != reply_invalid; reply = query ()) { + separate (); - print_resources (); - print_constructions (); + print_resources (); + print_constructions (); - build_construction (granary); - build_construction (mine); - build_construction (storehouse); - build_construction (quarry); + build_construction (granary); + build_construction (mine); + build_construction (storehouse); + build_construction (quarry); - print_statistics (); + print_statistics (); + } print ("The end!\n");