Sumeria ( download)

advertisement
COMSC 100, spring 2013
Project: Hamurabi, ruler of Sumeria
Due April 16, 2013
The Context
The object of this project is to write a simulation about running the city-state of Sumeria. The computer is
to play steward to Hamurabi, who has just been given the city-state of Sumeria for a 10-year rule. The
game player is Hamurabi, who has to make decisions about land sales and purchases, food distribution,
planting of crops, etc. This program will take a fair amount of code and a lot of variables but really
consists of one simple step after another.
Starting the rule
Initial values of acreage (1000 acres), population (100 people), food in storage (3000 bushels) should be set
and announced to Hamurabi as he is given the keys to the city. (Bushels of grain are used to feed the
people, plant next year's crop and to buy or sell land.)
The program should then run a 10-year loop, from years 1 to 10.
In each year, these events must happen:
event 1. purchase of land
The land price, in bushels ofgrain, in a given year will be, randomly, a number from 17 to 26. Find the land
price, then ask Hamurabi if he wishes to buy land. Put this request in a do … while loop so that Hamurabi
is forced to type a reasonable reply. That is, if Hamurabi tries to buy < 0 acres, say "Hamurabi, think
again" or something like that. If Hamurabi tries to buy more acres than he has bushels in the storehouse,
remind Hamurabi how much is in the storehouse. Finally, if Hamurabi does give a reasonable number (and
0 is OK), add the acres to the land, subtract the cost from the storehouse.
event 2. sale of land
At the same price, i.e., don't recalculate, ask if Hamurabi would like to sell land, often necessary when
there is not enugh food to feed the people or plant a crop. Again, check that the amount is not less thn 0
and that Hamurabi does not try to sell more land than he owns.
event 3. feed the people
Ask Hamurbai how many bushels to feed the people. If Hamurabi says less than 0, please comment; if
Hamurabi tries to feed more bushels than he has, remind him of how many he has. (Again, that means
putting the question in a loop until Hamurabi gives a proper answer.) Once Hamurabi does choose an
amount, subtract it from the store. Then, do a behind the scenes calculation to see how many people were
fed properly. It takes 20 bushels to feed one person, so compute how many people were fed and set the
value of starved to the difference between the population and the number fed. Be certain that if Hamurabi
is overly generous with food you don't compute negative people starving. Compute what is left in store.
event 4. plant some acreage with seed
With the seed left after buying and selling land and feeding the people, Hamurabi must decide how many
acres to plant. Again, check that Hamurabi doesn't try to plant more acres than he has and that he has
enough seed to plant the acreage and that there are enough people to plant the acreage. It takes 1/2 bushel
to plant an acre and one person can plant 10 acres. Compute what is left in store.
event 5. compute the harvest
The yield will be a random number, from 1 to 5, of bushels per acre. Compute a yield and multiply by the
acres planted to compute the harvest. Don't yet add the harvest amount to the store. Do that at the end of
the year just before reporting.
event 6. what have the rats eaten?
The rats are an ever present problem. To see if the rats have damaged the store, compute a random number
n from 1 to 5. If it is odd, there is no damage; if it is even, the amount the rats eat is store/n. Remember
that this year's harvest is not in the store, yet.
event 7. population growth
The population growth in any year (births and immigration) is a complex function of acreage, population,
and food store. First compute the wealth of the city as (20*acres owned + bushels in store). This should be
an integer, and you may want to use a long int. Divide this by the population (integer divide). Divide the
result by 100 (integer divide). This final quotient should be multiplied by a random number from 1 to 5
and 1 should be added to the result.
event 8. was there a plague?
There are 15 chances out of a hundred that a plague will hit the city. If it does, cut the new population in
half after reporting the new population (next item).
event 9. report to Hamurabi
At the end of the year report these events to Hamurabi:
The number of people who starved, and the number who came to the city.
IF MORE THAN 45% of the population starves, impeach Hamurabi, i.e., issue a proclamation and exit the
program. (use break)
Compute and report the new population, then check if a plague occurred. If it did cut the population in
half.
Report the presence or absence of plague and the population.
Report the acreage now owned by the city.
Report the acres planted and the yield.
Report what the rats ate.
Compute the amount in storage, i.e., what was there minus what the rats ate plus the harvest.
At the end of the 10 years, evaluate Hamurabi.
Each year, compute the percent of people who starved, and at the end of 10 years, compute the
average of these amounts. At the end of 10 years, also compute the average land per person owned by the
city.
Then print something like:
Hamurabi, in your 10-year term of office,??, percent of the
population starved on average. At the start, you had 10 acres
of land per person. You ended with ?? acres person.
and print one of these evaluations, based on percent starved and acres per person
if pct_strvd > .33 or acresperperson < 7
use the impeachment statement
if pct_strvd > .10 or acresperperson < 9
Your incompetent performance is reminiscent of Nero."
The people (remaining) are recruiting for a replacement."
if pct_strvd > .03 or acresperperson < 10
Your performance could have been better, but really wasn't
too bad at all. ?? of the people would like to have you
assassinated, but that's politics. (?? should be a random number up to 80% of the
population).
otherwise
A fantastic perfomance, worthy of Elizabeth I, Catherine the Great, and
possibly Margaret Thatcher. No one could have done better!
Outline the program with comments and build one section at a time, and test it!
[My apologies to whomever created this simulation in the first place – I cannot remember where it came
from but will attempt to find out and provide credit.]
How to write this program
This is a large but not complex program. The shape of it is:
def Sumeria():
acreage = 1000
pop = 100
stored_food = 3000
#welcome Hamuarabi
for year in range(1,11):
# proclaim the year
# purchase land
# sell land
# feed the people
# plant seed
# measure the harvest
# check for rats
# population growth
# check for plague
# report to Hamurabi
# Issue a final verdict on the king's reign
Some programming hints:
There are several places where you must insist on a correct answer from Hamurabi. A way to do this
is to set up a while loop:
good_answer = false
while not good_answer:
ask the question
if the answer falls within the permissible limits, set good_answer = true
when checking for bad answers briefly explain why answer is bad, e.g.
you cannot buy negative land, sire.
It is now year 1
Sire, the price of land is
It is now year 2
Sire, the price of land is
It is now year 3
Sire, the price of land is
It is now year 4
Sire, the price of land is
It is now year 5
Sire, the price of land is
It is now year 6
Sire, the price of land is
It is now year 7
Sire, the price of land is
It is now year 8
Sire, the price of land is
It is now year 9
Sire, the price of land is
It is now year 10
Sire, the price of land is
23 bushels per acre
24 bushels per acre
17 bushels per acre
22 bushels per acre
24 bushels per acre
24 bushels per acre
24 bushels per acre
18 bushels per acre
17 bushels per acre
19 bushels per acre
Download