User Tools

Site Tools


wiki:hamurabi

====== Hamurabi

Try your hand at ruling ancient Sumeria for 10 years. This console game runs in a Python interpreter or from the command line.

Still working out some bugs.

import random
import sys

def welcomeMessage():
    '''
    DOCSTRING: Information about the function
    INPUT: this function takes no input
    OUTPUT: this function prints a formatted welcome message
    '''
    print("\t\t\t\t\t\tHAMURABI")
    print("\t\t\t\tCREATIVE COMPUTING MORRISTOWN, NEW JERSEY")
    print("\t\t\t\tORIGINALLY WRITTEN IN 1968 BY DOUG DYMENT")
    print("\t\t\t\t     PYTHON CODE BY PAUL WALKER 2019\n\n\n")
    print("\t\t\t\tTRY YOUR HAND AT GOVERNING ANCIENT SUMERIA")
    print("\t\t\t\t    FOR A TEN-YEAR TERM OF OFFICE.\n\n\n\n")
    return


def arrive():
    incoming = random.randint(1, 20)
    return incoming

def birth():
    born = random.randint(1, 20)
    return born

def plague(population):
    thePlague = random.randint(1, population)
    if random.randint(1, 5) <= 4:
        thePlague = 0
    else:
        print(f"A HORRIBLE PLAGUE STRUCK! {thePlague} PEOPLE DIED")
    return thePlague

def checkMorale(pop, bad1, bad2):
    moraleIndicator = float(((bad1 + bad2)/pop)*100)
    if moraleIndicator >= 50:
        morale = "low"
    elif moraleIndicator >= 25:
        morale = "average"
    else:
        morale = "high"
    return morale

def census(start, add1, sub1, sub2):
    newPopulation = start + add1 - sub1 - sub2
    if newPopulation < 0:
        newPopulation = 0
    return newPopulation

def newPage():
        print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
        return

def startImpeachment(numberStarved, impeachedForStarvation = True, years = 1):
    if impeachedForStarvation:
        print(f"YOU STARVED {numberStarved} IN ONE YEAR!!!")
    else:
        print(f"YOU STARVED ON AVERAGE {int(numberStarved / years)} PER YEAR!!!")
    print("DUE TO THIS EXTREME MISMANAGEMENT YOU HAVE NOT ONLY")
    print("BEEN IMPEACHED AND THROWN OUT OF OFFICE BUT YOU HAVE")
    print("ALSO BEEN DECLARED NATIONAL FINK!!!!")
    return

def getValidIntegerInput(message):
    valid = False
    while not valid:
        enterNumber = input(message)
        try:
            number = int(enterNumber)
        except ValueError:
            print(f"That's not an integer. Try again.")
        else:
            valid = True
    return number

##########################################################33
# Initial starting information
year = 0
population = 100 #citizens eat 10 bushels per year, else they starve
totalStarved = 0  #running count of the total number of people starved during the game
totalAcresPlanted = 0 #running total of the number of acres planted
totalAcresBought = 0 #running total of the number of acres bought
totalAcresSold = 0 #running total of the number of acres sold
totalBushelsHarvested = 0 #running total of the number of acres harvested
totalBushelsRatsAte = 0

acresCityOwns = 1000 #starts out with 10 acres per person
acresPlanted = 0
bushelsRatsAte = 0
bushelsInStore = 3000 #starts out with 3000 bushels in storage
buyAcres = "HOW MANY ACRES DO YOU WISH TO BUY?   > "
sellAcres = "HOW MANY ACRES DO YOU WISH TO SELL?  > "
plantAcres = "HOW MANY ACRES DO YOU WISH TO PLANT? > "
quit = False
alreadyImpeached = False


while not quit and year <=9:
    newPage()
    welcomeMessage()
    year += 1
    marketValue = random.randint(17, 27) #this is the number of bushels that will buy an acre of land for this turn
    bushelsHarvested = random.randint(1, 10) #this is the number of bushels that will be harvested during this turn
    totalBushelsHarvested += bushelsHarvested #figure for the end of game
    bushelsInStore = bushelsInStore + bushelsHarvested
    bushelsRatsAte = random.randint(0, 9) * random.randint(0, 4)
    totalBushelsRatsAte += bushelsRatsAte
    bushelsInStore = bushelsInStore - bushelsRatsAte

    #starvation check
    if bushelsInStore < (population * 10):
        starvedToDeath = abs(bushelsInStore - (population * 10))
        bushelsInStore = 0
    else:
        starvedToDeath = 0
        bushelsInStore = bushelsInStore - (population * 10)

    totalStarved += starvedToDeath
    if starvedToDeath > .45*population:
        quit = True
        startImpeachment(starvedToDeath, year)
        alreadyImpeached = True
        break
    movedIn = arrive()

    print("HAMURABI: I BEG TO REPORT TO YOU,")
    print(f"IN YEAR {year}, {starvedToDeath} PEOPLE STARVED, {movedIn} CAME TO THE CITY,")
    diedFromPlague = plague(population)
    population = census(population, movedIn, starvedToDeath, diedFromPlague)
    print(f"POPULATION IS NOW {population}")

    print(f"THE CITY NOW OWNS {acresCityOwns} ACRES.")
    print(f"YOU HARVESTED {bushelsHarvested} BUSHELS PER ACRE.")
    if bushelsRatsAte > 0:
        print(f"RATS RUN WILD!! RATS ATE {bushelsRatsAte} BUSHELS.")
    print(f"YOU NOW HAVE {bushelsInStore} BUSHELS IN STORE.\n")
    print(f"LAND IS TRADING AT {marketValue} BUSHELS PER ACRE.")
    
    # This next part is 3 inputs ran through functions that are try/except blocks 
    
    buy = getValidIntegerInput(buyAcres)
    while bushelsInStore < (buy * marketValue):
        print("NOT ENOUGH BUSHELS TO BUY THAT MANY ACRES.  TRY AGAIN OR ENTER 0 TO BUY NOTHING.")
        buy = getValidIntegerInput(buyAcres)
    acresCityOwns = acresCityOwns + buy
    totalAcresBought += buy
    bushelsInStore = bushelsInStore - (buy * marketValue)

    sell = getValidIntegerInput(sellAcres)
    while acresCityOwns < sell:
        print("YOU CAN'T SELL MORE ACRES THAN YOU OWN.  TRY AGAIN OR ENTER 0 TO SELL NOTHING.")
        sell = getValidIntegerInput(sellAcres)
    acresCityOwns = acresCityOwns - sell
    totalAcresSold += sell
    bushelsInStore = bushelsInStore + (sell * marketValue)

    acresPlanted = getValidIntegerInput(plantAcres)
    while acresPlanted > (population*10) or acresPlanted > acresCityOwns or acresPlanted > bushelsInStore:
        if acresPlanted > (population*10):
            print("NOT ENOUGH PEOPLE TO PLANT THAT MANY ACRES. ONE CITIZEN PLANTS TEN ACRES. TRY AGAIN.")
            acresPlanted = getValidIntegerInput(plantAcres)
        elif acresPlanted > acresCityOwns:
            print("YOU CAN'T PLANT MORE ACRES THAN THE CITY OWNS.  TRY AGAIN.")
            acresPlanted = getValidIntegerInput(plantAcres)
        elif acresPlanted > bushelsInStore:
            print("NOT ENOUGH BUSHELS TO PLANT THAT MANY ACRES.  ONE BUSHEL COVERS ONE ACRE. TRY AGAIN.")
            acresPlanted = getValidIntegerInput(plantAcres)
    bushelsInStore = bushelsInStore - acresPlanted
    totalAcresPlanted += acresPlanted

    if population <= 0:
        population = 0
        quit = True
    else:
        input("Press enter for the next year: ")

# End of Game Summary, only runs if the population is greater than zero
# and the player hasn't already been impeached
if population > 0 and not alreadyImpeached:
    print("    OVERALL STATISTICS FOR YOUR TERM:")
    print(f"NUMBER OF BUSHELS PLANTED:   {totalBushelsPlanted}")
    print(f"NUMBER OF BUSHELS HARVESTED: {totalBushelsHarvested}")
    print(f"NUMBER OF BUSHELS RATS ATE:  {totalBushelsRatsAte}")
    print(f"NUMBER OF ACRES BOUGHT:      {totalAcresBought}")
    print(f"NUMBER OF ACRES SOLD:        {totalAcresSold}")
    print(f"NUMBER OF CITIZENS STARVED:  {totalStarved}")
    
    morale = checkMorale(population, starvedToDeath, diedFromPlague)
    if morale == "low":
        averageStarved = totalStarved / 10
        startImpeachment(averageStarved, False, year)
    elif morale == "average":
        print("YOUR PERFORMANCE COULD HAVE BEEN BETTER, BUT")
        print(f"REALLY WASN'T TOO BAD AT ALL. {random.randint(1, population/2)} PEOPLE")
        print("WOULD DEARLY LIKE TO SEE YOU ASSASSINATED BUT WE ALL HAVE OUR")
        print("TRIVIAL PROBLEMS.")
    else:
        print("A FANTASTIC PERFORMANCE!!! CHARLEMANGE, DISRAELI, AND")
        print("JEFFERSON COMBINED COULD NOT HAVE DONE BETTER!")

print("\nSO LONG FOR NOW.")

wiki/hamurabi.txt · Last modified: 2019/09/05 00:16 by paul