import os
import random

coins = 10000.0
corn = 1000.0
earth = 100.0
corn_price = 15
earth_price = 10
step = 0
civilians = 100
howManyCivEat = 0.1
tax = 1.3
dieHungry = 0

def cls():
    os.system("clear")

def main_menu():        
    cls()
    global dieHungry
    print("""
    ==== ОСНОВНОЕ МЕНЮ ====
        1. ЗЕРНО
        2. ЗЕМЛЯ
        
        ENTER - следующий ход
        """)
    if dieHungry > 0:
        print("От голода погибло: ", int(dieHungry))
        dieHungry = 0
    show_info()
    key = input("Чего желаете? ")
    if key == '1':
        corn_operations()
    elif key == '2':
        earth_operations()
    else:
        next_step()
        
def next_step():
    global corn, corn_price, earth_price, step, civilians, coins, dieHungry
    step = step + 1
    # сколько будет сожрано
    willBeEaten = civilians * howManyCivEat
    if willBeEaten > corn:
        diff = willBeEaten - corn
        dieHungry = diff * (howManyCivEat * 100)
        civilians = civilians - dieHungry
        willBeEaten = willBeEaten - diff
    # жрут
    corn = corn - willBeEaten
    # спят спокойно
    coins = coins + tax * civilians
    # ебутся
    if dieHungry == 0:
        civilians = civilians + random.random() * (civilians / 10)
    # перенаселение
    if civilians > earth * 2:
        civilians = earth * 2
    # изменение цен
    corn_price = corn_price + random.random() * (1 + 1) -1
    earth_price = earth_price + random.random() * (1 + 1) -1
    # поправульки
    if corn < 0:
        corn = 0
    if civilians <= 0:
        civilians = 1
    main_menu()
    
def show_info():
    print("""
        ХОД: {} ЖИТЕЛЕЙ: {} РАСХОД ЗЕРНА: {} НАЛОГ: {}
        В КАЗНЕ: ЗОЛОТО {} ЗЕРНО {} ЗЕМЛЯ {}
        ЦЕНЫ:    ЗЕРНО {} ЗЕМЛЯ {}
        """.format(step, int(civilians), int(civilians * howManyCivEat), tax,
        round(coins, 2), int(corn), int(earth), 
        round(corn_price, 2), round(earth_price, 2)))

def corn_operations():
    cls()
    global corn, coins
    print("""
    ==== ОПЕРАЦИИ С ЗЕРНОМ ====
        1. Купить
        2. Продать
        
        0. Назад
        
        """)
    show_info()
    key = input("Чего желаете? ")
    if key == '1':
        count = float(input("Сколько бы вы хотели купить зерна? "))
        price = count * corn_price
        if price <= coins:
            corn = corn + count
            coins = coins - price
            corn_operations()
        else:
            input("Недостаточно золота")
            corn_operations()
    elif key == '2':
        count = float(input("Сколько бы вы хотели купить зерна? "))
        if count <= corn:
            price = count * corn_price
            corn = corn - count
            coins = coins + price
            corn_operations()
        else:
            input("Недостаточно зерна")
            corn_operations()
    elif key == '0':
        main_menu()
        
def earth_operations():
    cls()
    global earth, coins
    print("""
    ==== ОПЕРАЦИИ С ЗЕМЛЁЙ ====
        1. Купить
        2. Продать
        
        0. Назад
        
        """)
    show_info()
    key = input("Чего желаете? ")
    if key == '1':
        count = float(input("Сколько бы вы хотели купить земли? "))
        price = count * earth_price
        if price <= coins:
            earth = earth + count
            coins = coins - price
            earth_operations()
        else:
            input("Недостаточно золота")
            earth_operations()
    elif key == '2':
        count = float(input("Сколько бы вы хотели купить земли? "))
        if count <= earth:
            price = count * earth_price
            earth = earth - count
            coins = coins + price
            earth_operations()
        else:
            input("Недостаточно земли")
            earth_operations()
    elif key == '0':
        main_menu()

main_menu()
