diff --git a/hammurabi.c b/hammurabi.c index 8d3ad70..380b455 100644 --- a/hammurabi.c +++ b/hammurabi.c @@ -6,7 +6,6 @@ */ #include #include -#include #include #define MAXYEARS 10 @@ -15,6 +14,7 @@ #define INITLAND 1000 typedef enum {FALSE, TRUE} Boolean; +typedef enum {BUY, SELL, FEED, PLANT, RESET, EXEC, CALC, QUIT} Plancmd; char MSGDIV[] = "------------------------------<<<<>>>>-----------------------------------\n"; @@ -56,7 +56,10 @@ char MSGSPLASH[] = char *HONORIFIC[] = {"Great", "Mighty", "Wise", "Benevolent", "Merciful", "Just", "Sublime", "Glorious", "Serene", "Majestic"}; +void completedterm(void); +int inputiexpr(void); Boolean planyear(void); +void printdeposed(void); void printgreeting(void); void printlandprice(void); void printstatus(void); @@ -73,9 +76,10 @@ int consctl; int foodgrain, landsale, plantland; -int harvyield, landprice, newpop, plaguedeath, popfed, ratgrain, starvedeath, starvepct, year; +int harvyield, landprice, newpop, plaguedeath, popfed, ratgrain, starvedeath, starvepct; int totstarved = 0, totstarvepct = 0; +Boolean terminated = FALSE; void main(void) @@ -92,9 +96,11 @@ main(void) setlandprice(); printlandprice(); - for(year = 1; year <= MAXYEARS; ++ year) + do { - if(planyear() == TRUE) + ++ Year; + if(planyear() == FALSE) terminated = TRUE; + else { Grain = Grain + (landsale * landprice) - (plantland / 2) - foodgrain; Land = Land - landsale; @@ -121,26 +127,149 @@ main(void) starvedeath = ((popfed < Population) ? (Population - popfed) : 0); totstarved = totstarved + starvedeath; starvepct = 100 * starvedeath / Population; - totstarvepct = totstarvepct + starvepct; - Population = Population + newpop - starvedeath; - plaguedeath = ((nrand(100) < 15) ? (Population / 2) : 0); - Population = Population - plaguedeath; + if(starvepct > 45) + { + printgreeting(); + print("You have starved %d of your subjects in one year!\n", starvedeath); + printdeposed(); + terminated = TRUE; + } + else + { + totstarvepct = totstarvepct + starvepct; + Population = Population + newpop - starvedeath; + plaguedeath = ((nrand(100) < 15) ? (Population / 2) : 0); + Population = Population - plaguedeath; + + printgreeting(); + printyearrept(); - /* impeachement check */ - - printgreeting(); - printyearrept(); + if(Year == MAXYEARS) completedterm(); + } } - else break; - } - print("The end\n"); + } while(Year < MAXYEARS && terminated == FALSE); + print(MSGDIV); exits(0); } +void +completedterm(void) +{ + int avgstarvepct = totstarvepct / MAXYEARS; + int landperperson = Land / Population; + int likeassassination = Population * 0.8 * frand(); + printgreeting(); + print("In your %d-year reign, %d people died of starvation, an average of %d people per year.\n", MAXYEARS, totstarved, avgstarvepct); + print("You started with %d acres per person and ended with %d acres per person.\n", INITLAND / INITPOP, landperperson); + if(avgstarvepct > 33 || landperperson < 7) printdeposed(); + else if(avgstarvepct > 10 || landperperson < 9) print("Your heavy-handed performance smacks of Nero and Ivan IV. Your (surviving) people find you an unpleasant ruler, and, frankly, hate your guts!"); + else if(avgstarvepct > 3 || landperperson < 10) + { + print("Your performance could have been better, but wasn't too bad. "); + if(likeassassination > 0) print("%d %s would like to see you assassinated, but we all have our trivial problems.\n", likeassassination, (likeassassination == 1 ? "person" : "people")); + } + else print("A fantastic performance! Charlemagne, Disraeli, and Jefferson combined could not have done better!"); +} + +int +inputiexpr(void) +{ + char buf[80]; + int buflen = 80; + int result = 0; + int ch, rix, wix, p, o, n[40]; + char op[39]; + + if(fgets(buf, buflen, stdin) != NULL) + { + if(buf[strlen(buf) - 1] != '\n') while((ch = getchar()) != '\n' && ch != EOF) ; + rix = wix = 0; + while(buf[rix] != '\0') + { + if(strchr("0123456789+-*/", buf[rix]) != 0) + { + if(rix > wix) buf[wix] = buf[rix]; + ++wix; + } + ++ rix; + } + buf[wix] = '\0'; + p = sscanf(buf, "%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d%c%d", + &n[0],&op[0],&n[1],&op[1],&n[2],&op[2],&n[3],&op[3],&n[4],&op[4],&n[5],&op[5],&n[6],&op[6],&n[7],&op[7],&n[8],&op[8],&n[9],&op[9], + &n[10],&op[10],&n[11],&op[11],&n[12],&op[12],&n[13],&op[13],&n[14],&op[14],&n[15],&op[15],&n[16],&op[16],&n[17],&op[17],&n[18],&op[18],&n[19],&op[19], + &n[20],&op[20],&n[21],&op[21],&n[22],&op[22],&n[23],&op[23],&n[24],&op[24],&n[25],&op[25],&n[26],&op[26],&n[27],&op[27],&n[28],&op[28],&n[29],&op[29], + &n[30],&op[30],&n[31],&op[31],&n[32],&op[32],&n[33],&op[33],&n[34],&op[34],&n[35],&op[35],&n[36],&op[36],&n[37],&op[37],&n[38],&op[38],&n[39]); + if(p > 0) + { + result = n[0]; + for(o = 0; o < (int) (p / 2); ++ o) + { + switch (op[o]) + { + case '+': + result = result + n[o+1]; + break; + case '-': + result = result - n[o+1]; + break; + case '*': + result = result * n[o+1]; + break; + case '/': + result = result / n[o+1]; + break; + } + } + } + } + return result; +} + Boolean planyear(void) { + Boolean play = TRUE; + Plancmd cmd; + foodgrain = landsale = planland = 0; + do + { + cmd = planmenu(); + switch(cmd) + { + case BUY: + + break; + case SELL: + + break; + case FEED: + + break; + case PLANT: + + break; + case RESET: + foodgrain = landsale = planland = 0; + break; + case EXEC: + + break; + case CALC: + ezintcalc(); + break; + case QUIT: + play = FALSE; + break; + } + } while(cmd != EXEC && cmd != QUIT); + return play; +} + +void +printdeposed(void) +{ + print("Due to this extreme mismanagement you have not only been deposed and executed, but you have also been declared National Fink!\n"); } void @@ -167,7 +296,7 @@ printstatus(void) void printyearrept(void) { - print("In Year %d,\n", year); + print("In Year %d,\n", Year); print("%d bushels were harvested per acre, for a total harvest of %d bushels.\n", harvyield, plantland * harvyield); if(ratgrain > 0) print("Rats ate %d bushels of grain.\n", ratgrain);