class Hammurabi
  OGH = "O Great Hammurabi"

  def initialize
    @population = 100
    @grain = 2800
    @acres = 1000
    @landValue = 19

    @starved = 0
    @plagueVictims = 0
    @immigrants = 5
    @harvest = 3
    @amountEatenByRats = 100
  end

  def intro
    puts "Congratulations! You are the newest ruler of ancient Samaria,\n" \
         " elected for a ten year term of office. Your duties are to\n" \
         " dispense food, direct farming, and buy and sell land as\n" \
         " needed to support your people. Watch out for rat infestiations\n" \
         " and the plague! Grain is the general currency, measured in\n" \
         " bushels. The following will help you in your decisions:\n" \
         "\n" \
         "   * Each person needs at least 20 bushels of grain per year to survive\n" \
         "   * Each person can farm at most 10 acres of land\n" \
         "   * It takes 2 bushels of grain to farm an acre of land\n" \
         "   * The market price for land fluctuates yearly\n" \
         "\n" \
         "Rule wisely and you will be showered with appreciation at the\n" \
         " end of your term. Rule poorly and you will be kicked out of office!\n"
  end

  def play
    for i in 1..10
      printSummary("in year #{i}")
      getCommandsFromHammurabi
      # puts "---------- It shall be done, #{OGH}.\n"
      determineResults
    end
    printSummary('at the end')
    endGame @acres
  end

  def getCommandsFromHammurabi
    acresBought = buyLand
    sellLand if acresBought == 0
    feedPeople
    plantCrops
  end

  def determineResults
    plague
    percentStarved = starvation
    endGame(-percentStarved) if percentStarved > 45
    immigration
    harvest
    rats
    realEstate
  end

  def printSummary(year)
    puts '___________________________________________________________________'
    puts "\n#{OGH}!"
    puts "You are #{year} of your ten year rule."
    puts "A horrible plague killed #@plagueVictims people." \
      unless @plagueVictims == 0
    puts "In the previous year #@starved people starved to death."
    puts "In the previous year #@immigrants people entered the kingdom."
    puts "The population is now #@population."
    puts "We harvested #@grainHarvested bushels at #@harvest bushels per acre."
    if @amountEatenByRats > 0
      puts "*** Rats destroyed #@amountEatenByRats bushels, leaving #@grain" \
           " bushels in storage."
    end
    puts "The city owns #@acres acres of land."
    puts "Land is currently worth #@landValue bushels per acre."
  end

  def buyLand
    while true
      acresToBuy = ask("How many acres of land will you buy?")
      if acresToBuy > 0
        cost = @landValue * acresToBuy
        if cost > @grain
          jest("We have but #@grain bushels of grain, not #{cost}!")
        else
          @acres += acresToBuy
          @grain -= cost
          puts "#{OGH}, you now have #@acres acres of land"
          puts "and #@grain bushels of grain."
          return acresToBuy
        end
      elsif acresToBuy == 0
        return 0
      end
    end
  end

  def sellLand
    while true
      acresToSell = ask("How many acres of land will you sell?")
      if acresToSell > @acres
        jest("We have but #@acres acres!")
      else
        @grain += @landValue * acresToSell
        @acres -= acresToSell
        puts "#{OGH}, you now have #@acres acres of land"
        puts "and #@grain bushels of grain."
        return
      end
    end
  end

  def feedPeople
    while true
      eat = ask("How much grain will you feed to the people?")
      if eat > @grain
        jest("We have but #@grain bushels!")
      else
        @fedToPeople = eat
        @grain -= eat
        puts "#{OGH}, you have #@grain bushels of grain left."
        return
      end
    end
  end

  def plantCrops
    while true
      amountToPlant = ask("How many bushels will you plant?")
      if amountToPlant > @grain
        jest("We have but #@grain bushels left!")
      elsif amountToPlant > 2 * @acres
        jest("We have but #@acres acres available for planting!")
      elsif amountToPlant > 20 * @population
        jest("We have but #@population people available to do the planting!")
      else
        @acresPlanted = amountToPlant / 2
        @grain -= amountToPlant
        puts "#{OGH}, you now have #@grain bushels of grain in storage."
        return
      end
    end
  end

  def plague
    if rand < 0.15
      puts "*** A horrible plague kills half your people! ***"
      @plagueVictims = @population / 2
      @population -= @plagueVictims
    else
      @plagueVictims = 0
    end
  end

  def starvation
    peopleFed = @fedToPeople / 20
    if peopleFed >= @population
      # puts "Your people are well fed and happy."
      return 0
    else
      @starved = @population - peopleFed
      # puts "#@starved people starved to death."
      percentStarved = 100 * @starved / @population
      @population -= @starved
      return percentStarved
    end
  end

  def immigration
    @immigrants = (20 * @acres + @grain) / (100 * @population) + 1
    @population += @immigrants
  end

  def harvest
    @harvest = 1 + rand(5)
    @grainHarvested = @harvest * @acresPlanted
    @grain += @grainHarvested
  end

  def rats
    if rand(100) < 40
      percentEatenByRats = 10 + rand(21)
      puts "*** Rats eat #{percentEatenByRats}% of your grain! ***"
      @amountEatenByRats = (percentEatenByRats * @grain) / 100
      @grain -= @amountEatenByRats
    else
      @amountEatenByRats = 0
    end
  end

  def realEstate
    @landValue = 17 + rand(7)
  end

  def endGame measure
    puts "\n"
    if measure < 0
      puts "O Once-Great Hammurabi,\n" \
           "#{-measure}% of your people starved during the last year of your\n" \
           "incompetent reign! The few who remain have stormed the palace\n" \
           "and bodily evicted you!\n" \
           "\nYour final rating: TERRIBLE."
    elsif measure < 900
      puts "Congratulations, #{OGH}! You have ruled wisely but not\n" \
           "well; you have led your people through ten difficult years, but\n" \
           "your kingdom has shrunk to a mere #{measure} acres.\n" \
           "\nYour final rating: ADEQUATE."
    elsif measure < 1100
      puts "Congratulations, #{OGH}! You have ruled wisely, and\n" \
           "shown the ancient world that a stable economy is possible.\n" \
           "\nYour final rating: GOOD."
    else
      puts "Congratulations, #{OGH}! You have ruled wisely and well, and\n" \
           "expanded your holdings while keeping your people happy.\n" \
           "Altogether, a most impressive job!"
           "\nYour final rating: SUPERB."
    end
    exit
  end

  def ask(question)
    print "\n"
    begin
      print "#{question} "
      STDOUT.flush
      return Integer(gets.chomp)
    rescue
      print "Try again:  "
      retry
    end
  end

  def jest(message)
    puts "#{OGH}, surely you jest!"
    puts message
  end

  def dump
    puts "#@population people, #@grain bushels of grain, #@acres acres, land is at #@landValue bushels/acre."
  end
end


h = Hammurabi.new
h.intro
h.play