import random

def print_intro():
    print('''Congrats, you are the newest ruler of ancient Sumaria, elected for a ten year term of office.
Your duties are to distribute food, direct farming, and buy and sell land as needed to support your people.
Watch out for rat infestations and the resultant plague! Grain is the general currency, measured in bushels.
The following will help you in your decisions:
(a) Each person needs at least 20 bushels of grain per year to survive.
(b) Each person can farm at most 10 acres of land.
(c) It takes 2 bushels of grain to farm an acre of land.
(d) The market price for land fluctuates yearly.
Rule wisely and you will be showered with appreciation at the end of your term.
Rule poorly and you will be kicked out of office!''')

def ask_to_buy_land(bushels, cost):
    acres = int(input("How many acres will you buy? "))
    while acres * cost > bushels:
        print("O great Hammurabi, we have but", bushels, "bushels of grain!")
        acres = int(input("How many acres will you buy? "))
    return(acres)

def ask_to_sell_land(acres):
    sell_acres = int(input("How much land would you like to sell? (input a negative amount to sell)"))
    while sell_acres > 0:
        print("O great Hammurabi, to sell land you must input a negative amount")
        sell_acres = int(input("How much land would you like to sell? (input a negative amount to sell)"))
    while (sell_acres > acres):
        print("O great Hammurabi, to sell land you must input a negative amount")
        sell_acres = int(input("How much land would you like to sell? (input a negative amount to sell)"))
    return(sell_acres)

def ask_to_feed(bushels):
    bushels_feed = int(input("How many bushels would you like to use for feeding?"))
    while (bushels_feed > bushels):
        print(bushels)
        print("O great Hammurabi, excuse me when I say, with that input you will most certainly starve us all!") 
        bushels_feed = int(input("How many bushels would you like to use for feeding?"))
    return(bushels_feed)
    

def ask_to_cultivate(acres, population, bushels):
    land_for_seed = int(input("How much land would you like to plant seed in?"))
    while land_for_seed > acres:
        print("O great Hammurabi, with all do respect, we do not own enough bushels to complete your desired endeavour.")
        land_for_seed = int(input("How much land would you like to plant seed in?"))
    while (land_for_seed > (population * 10)):
        print("O great Hammurabi, with all do respect, we do not own enough bushels to complete your desired endeavour.")
        land_for_seed = int(input("How much land would you like to plant seed in?"))
    while ((land_for_seed * 2) > bushels):
        print("O great Hammurabi, we have but",bushels,"bushels to cultivate",bushels/2, "acres!")
        land_for_seed= input("How much land do you want to plant seed in?")
    return(land_for_seed)

def is_plague():
    '''Probability if there is a plague'''
    chance_plague = int(random.randint(1,100))
    if chance_plague <= 15:
        return(True)
    else:
        return(False)

def num_starving(population, bushels):
    feed_the_people = ask_to_feed(bushels)
    bushels = bushels - feed_the_people
    people_starving = population - (feed_the_people/20)
    percent_pop_starving = ((people_starving*100)/population)
    #calculates the percent of the population that is starving
    if (percent_pop_starving> 45):
        print("You've been kicked, bud :/")
        print(percent_pop_starving , "% of your people are starving")
        quit()
        #quits program if number of starving people is larger than 45%
    if (people_starving < 0):
        #runs if no one is starving...
        people_starving = 0
        return(people_starving)
    else:
        return(people_starving)

    
    
def num_immigrants(land, grain_in_storage, population, num_starving):
    if (num_starving > 0):
        num_immigrants = 0
    else:
        num_immigrants=int(((20 * land) + (grain_in_storage)) / ((100 * population) + 1))
    return(num_immigrants)
    #if people are starving, no immigrants come
    #if porple aren't starving, the equation is used


def get_harvest():
    bushels_harevested_per_plot = int(random.randint(1,8))
    return(bushels_harevested_per_plot)
    #returns the factor of which the number of bushels harvested per plot is multiplied by

def do_rats_infest():
    chance_rats_infest = int(random.randint(1, 100))
    if chance_rats_infest <= 40:
        return(True)
    else:
        return(False)
    

def percent_destroyed():
    if do_rats_infest():
        percent_ruined = int(random.randint(1, 3)/10)
    else:
        percent_ruined = 0
    return(percent_ruined)

def price_of_land():
    prices_of_land = int(random.randint(16, 22))
    return(prices_of_land)


def final_summary(acres, total_starved):
    print('''Hello Great Hamurabi! It looks like your rule was a great success. Let's see how everything
panned out:
''' )
    if (total_starved >= 10):
          print("Well, it looks like " + str(total_starved) + " people starved. You may be able to sweep it under the rug")
          print("but it's still a loss in my book.")
    if (total_starved < 10):
          print("Well, it looks like " + str(total_starved) + " people starved. Good Job Overall!")      

def Hammurabi():
    starved = 0
    immigrants = 5
    population = 100
    harvest = 3000          # total bushels harvested
    bushels_per_acre = 3    # amount harvested for each acre planted
    rats_ate = 200          # bushels destroyed by rats
    bushels_in_storage = 2800
    acres_owned = 1000
    cost_per_acre = 19      # each acre costs this many bushels
    plague_deaths = 0
    year_set = 0
    total_starved = 0

    print_intro()
    print("")
    print("")
    for i in range(year_set, 11):
        print("O great Hammurabi! :")
        print("You are in year", year_set,"of your ten year rule.")
        print("In the previous year", starved, "people starved to death.")
        print("In the previous year", immigrants,"people entered the kingdom.")
        print("The population is now: ", population)
        print("We harvested ", harvest, " bushels at ", bushels_per_acre, " bushels per acre.")
        print("Rats destroyed", rats_ate, "bushels, leaving,", bushels_in_storage, "bushels in storage.")
        print("The city owns", acres_owned, "acres of land.")
        print("Land is currently worth", cost_per_acre, "bushels per acre.")
        print("There were", plague_deaths, "deaths from the plague.")
        year_set = year_set + 1

        
        #BUYING LAND
        new_land = ask_to_buy_land(bushels_in_storage, cost_per_acre)
        acres_owned  = acres_owned + new_land
        bushels_in_storage = cost_per_acre * acres_owned
        #Adjusts the number of bushels in storage based on the cost of land and the land bought
        
        #NO LAND IS BOUGHT
        if (new_land == 0):
            sold_land = ask_to_sell_land(acres_owned)
            acres_owned = acres_owned - sold_land
            bushels_in_storage = cost_per_acre * acres_owned
        #if no land is bought, then the user is given the option to sell land which decreases
        #the acres owned and and bushels in storage
        

        #CHECKS BUSHELS IN STORAGE
        cultivated_land = ask_to_cultivate(acres_owned, population, bushels_in_storage)
        bushels_in_storage = bushels_in_storage - (cultivated_land * 2)
        #adjusts the amount of bushels in by the amount used to cultivate the land

        
        #PLAGUE
        if (is_plague()):
            plague_deaths = int(population / 2)
            population = int(population / 2)
            #the plague halves the population and also increseas the number of the deaths to the
            #plague by half of the current population
        else:
            plague_deaths = 0
            #if nobody's died from the plague, plague deaths drop to 0

        #NUM STARVING
        number_starving = num_starving(population, bushels_in_storage)
        total_starved = total_starved + number_starving
        #increases the number of people starving (if any)

        #NEW IMMIGRANTS
        number_of_immigrants = num_immigrants(acres_owned, bushels_in_storage, population, starved)
        population = population + number_of_immigrants
        #increases the population by the number of immigrants that came
            
        #HARVEST
        bushels_per_acre = get_harvest()
        #replaces current value of bushels_per_acre based on the rate bushels were harvest
        yearly_harvest = bushels_per_acre * cultivated_land
        #yeilds the total number of bushels gathered in a harvest
        bushels_in_storage = bushels_in_storage + yearly_harvest
        #increases number of bushels in storage by the amount harvested

        #RAT INFESTATION + PERCENT DESTROYED
        rats_ate = int(percent_destroyed() * bushels_in_storage)
        bushels_in_storage = bushels_in_storage - rats_ate
        #adjusts the bushels in storage based on the amount the rats ate
        
        #PRICE OF LAND
        cost_per_acre = price_of_land()
        
        
        if (year_set == 11):
            print("Game Over")
    
Hammurabi()