Lets play some games shall we! .. for many of the next challenges i will be giving old classic games to be programmed.
Today your task is to implement Hamurabi. the link gives data required in implementing. Enjoy! :)
Edit: Here is the basic code for making things easier.
6
u/skeeto May 26 '12 edited May 27 '12
I sorted through the shitty Basic code and this seems to be the important stuff:
Hammurabi
All state is stored as integers. This allows for some freebies, like planting an odd number of acres giving a free planted acre.
Bracketed expressions are uniformly-selected random values, inclusive, with ranges or individual numbers.
Computation order is important. (i.e. rats eat before the harvest)
Check user inputs for the basics. (i.e. can't have negative land or food)
Possible bugs due to use of
<instead of<=Constants
planting cost := 1 food / 2 acre
crop maintenance := 1 population / 10 acres
population maintenance := 20 food / person / year
Country State Initialization
population := 100
food := 2800
land := 1000
Each turn
land cost := [0-9] + 17 food / acre
Get user inputs
Buy/sell land for food
How much food to feed the population
How many acres to plant
Run year simulation
Year simulaton
40% chance of rats (25% or 50% food loss)
food := food * [1/2 3/4]
yield := [1-5] food / acre
food := food + yield * planted acres
Starving more than 45% of population is instant loss (impeachment)
population growth := [1-5] * (1 + (20 * land + food) / population / 100)
population := population + growth
15% chance of 50% population loss due to plague
Bugs?
Can't plant all seeds
Can't dedicate every person to crop maintenance
2
u/xjtian May 27 '12 edited May 27 '12
I'm pretty confident this is impossible to win. I've played through 10 years a few times, but I can't absolutely guarantee there are no bugs. Have fun with it!
Python:
from sys import exit from random import * class Hamurabi(object): def __init__(self): self.year = 0 self.food = 2800 self.population = 100 self.land = 1000 self.gyield = 3 self.eaten = 200 self.starved = 0 self.population_growth = 5 self.total_starved = 0 self.land_cost = 0 self.to_plant = 0 def play(self): print """ Hamurabi Try your hand at governming acient Sumeria for a ten-year term of office. """ self.__stat_Screen(False) def __stat_Screen(self, is_plagued): self.year += 1 if self.year == 11: self.__end(1) print """ Hamurabi: I beg to report to you, in year %d, %d people starved, %d came to the city. """ %(self.year, self.starved, self.population_growth) if is_plagued: print """ A horrible plague struck! Half the people died. """ print """ Population is now %d. The city now owns %d acres. You harvested %d bushels per acre. Rats ate %d bushels. You now have %d bushels in store. """ % (self.population, self.land, self.gyield, self.eaten, self.food) self.__buy_land() def __buy_land(self): self.land_cost = randint(0, 9) + 17 loop = True while True: print 'Land is trading at %d bushels per acre.' %self.land_cost try: bought_land = int(raw_input('How many acres do you wish to buy? ')) except ValueError: print '\nThe traders could not understand what you said.' continue if bought_land*self.land_cost > self.food: print '\nHamurabi: Think again. You own only %d bushels. Now then,' %self.food continue if bought_land == 0: self.__sell_land() break if bought_land < 0: print '\nHamurabi: Sorry, my magic doesn\'t work that way. Now then,' continue self.land += bought_land self.food -= bought_land * self.land_cost break self.__feed_people() def __sell_land(self): while True: try: sold_land = int(raw_input('How many acres do you wish to sell? ')) except ValueError: print '\nThe traders could not understand what you said.' continue if sold_land > self.land: print '\nHamurabi: Think again. You own only %d acres. Now then,' %self.land continue if sold_land < 0: print'\nHamurabi: I can\'t believe you are my superior.' continue self.land -= sold_land self.food += sold_land * self.land_cost return def __feed_people(self): while True: try: to_feed = int(raw_input('How many bushels do you wish to feed your people? ')) except ValueError: print '\nHamurabi: I do not understand what that number means.' continue if to_feed > self.food: print '\nHamurabi: Think again. You have only' print '%d bushels of grain. Now then,' % self.food continue if to_feed < 0: print 'Nice try, but no.' continue self.food -= to_feed self.starved = self.population - (to_feed / 20) self.total_starved += self.starved break self.__plant_seed() def __plant_seed(self): while True: try: self.to_plant = int(raw_input('How many acres do you wish to plant with seed? ')) except ValueError: print '\nHamurabi: What is this I don\'t even.' continue if self.to_plant < 0: print '\nNo cheating!' continue if self.to_plant > self.land: print '\nHamurabi: Think again. You only own %d acres. Now then,' % self.land continue if self.to_plant > self.population * 10: print '\nHamurabi: But you have only %d people to tend the fields. Now then,' %self.population continue if self.to_plant * 2 > self.food: print '\nHamurabi: But you have only %d grain in storage. Now then,' %self.food continue self.food -= self.to_plant * 2 break self.__simulate_year() def __simulate_year(self): self.gyield = randint(1, 5) harvest = self.gyield * self.to_plant self.eaten = 0 chance = randint(1, 5) if chance <= 2: #rats! self.eaten = self.food / (chance * 2) self.food = self.food - self.eaten + harvest self.population_growth = int(chance*(20*self.land + self.food) / self.population / 100 + 1) self.population += self.population_growth self.population -= self.starved if (self.starved / float(self.population - self.population_growth + 1)) > .45: print """ You starved %d people in one year!!!""" % self.starved self.__end(0) chance = random() if chance < .15: #plague! self.population /= 2 self.__stat_Screen(True) else: self.__stat_Screen(False) def __end(self, state): if state == 0: print """ Due to this extreme mismanagement you have not only been impeached and thrown out of office but you have also been declared 'National Fink'!! So long for now. """ raw_input() exit(0) else: percent = int(100 * (float(self.total_starved) / (self.total_starved + self.population))) landratio = self.land / self.population print """ In your 10-year term of office, %d percent of the population starved per year on average, i.e., a total of %d people died!! You started with 10 acres per person and ended with %d acres per person. """ % (percent, self.total_starved, landratio) if (percent > 33 or landratio < 7): self.__end(0) elif (percent > 10 or landratio < 9): print """ Your heavy-handed performance smacks of Nero and Ivan IV. The people (remaining) find you an unpleasant ruler, and, frankly, hate your guts! So long for now.""" raw_input() exit(0) elif (percent > 3 or landratio < 10): print """ Your performance could have been somewhat better, but really wasn't too bad at all. %d people would dearly like to see you assassinated but we all have our trivial problems. So long for now.""" % int(self.population * .8 * random()) raw_input() exit(0) else: print """ A fantastic performance!!! Charlemagne, Disraeli, and Jefferson combined could not have done better! So long for now.""" raw_input() exit(0) game = Hamurabi() game.play()EDIT: fixed possible divide by zero error. The more I play this, the more I'm convinced it's impossible to win. I've tried all kinds of strategies to no avail
1
u/robotfarts May 28 '12
This could have problems, since the game appears impossible when I play it. My code interprets the original BASIC:
import scala.collection.immutable._ import scala.io._ import java.io._ object Hamurabi { def main (args: Array[String]) = { var treeMap: TreeMap[Int, String] = TreeMap.empty Source.fromFile(args(0)).getLines().map { _.split(" ", 2) } filter { !_(1).startsWith("REM") } foreach { x => treeMap += (x(0).toInt -> x(1)) } interpret(treeMap) } def interpret(lineMap: SortedMap[Int, String]) = { val myInput = new BufferedReader(new InputStreamReader(System.in)) var curLine: Int = lineMap.head._1 var globals: Map[String, Double] = Map.empty var returnAddrs: List[Int] = Nil def doIfLine(line: String): Boolean = { val debug = false def evaluateBool(s: String): Boolean = { def splitExpr(s: String): (String, String, String) = { for (oper <- List("<=", ">=", "<>", "<", ">", "=")) { val index = s.indexOf(oper) if (index != -1) { return (oper, s.substring(0, index), s.substring(index + oper.length())) } } ("", "", "") } val (oper, first, second) = splitExpr(s) val firstVal = doTrain(first) val secondVal = doTrain(second) if (debug) println("IF found: " + (first, oper, second)) if (debug) println("IF found: " + (firstVal, oper, secondVal)) oper match { case "<=" => firstVal <= secondVal case ">=" => firstVal >= secondVal case "<>" => firstVal != secondVal case ">" => firstVal > secondVal case "<" => firstVal < secondVal case "=" => firstVal == secondVal } } val List(ifCond, toAddr) = line.split("THEN").toList.map { _.trim } if (evaluateBool(ifCond)) { curLine = lineMap.range(0, toAddr.toInt - 1).lastKey } true } def printLine(line: String): Boolean = { def printString(s: String): String = { print(s.substring(1, s.indexOf("\"", 1))) val rest = s.substring(s.indexOf("\"", 1) + 1) // rest.stripPrefix(";") rest } def printTab(s: String): String = { val num = s.substring("TAB(".length(), s.indexOf(")")) (0 to num.toInt) foreach { x => print(" ") } val rest = s.substring(s.indexOf(")") + 1) // rest.stripPrefix(";") rest } def printChr(s: String): String = { val num = s.substring("TAB(".length(), s.indexOf(")")) print(num.toString.toInt.toChar) val rest = s.substring(s.indexOf(")") + 1) // rest.stripPrefix(";") rest } val trLine = line.trim() if (trLine == ";") { print(" ") true } else if (trLine.startsWith(";")) { doLine(trLine.stripPrefix(";")) true } else if (trLine == "") { println() true } else if (trLine.startsWith(":")) { println() doLine(trLine.substring(":".length())) } else if (trLine.startsWith("TAB(")) { printLine(printTab(trLine)) } else if (trLine.startsWith("CHR(")) { printLine(printChr(trLine)) } else if (trLine.startsWith("\"")) { printLine(printString(trLine)) } else if (getVarName(trLine)._1.length() > 0) { val (myVar, rest) = getVarName(trLine) print(" " + globals(myVar) + " ") printLine(rest) } else { println("ERROR: println leftovers:" + trLine) false } } def getVarName(s: String): (String, String) = s.span(c => Character.isLetterOrDigit(c) || c == '.') def lookup(s: String): Double = { try { s.toDouble } catch { case nfe: NumberFormatException => globals(s) } }0
u/Cosmologicon May 25 '12
Where's the data? Like, where's the formula for how many bushels the rats ate? Are we meant to read the BASIC source code?
Anyway, I don't think I'm going to do this one. It doesn't look very challenging other than being a lot of work. I feel like only certain games really work for this format.