Harvest and Market
A plot is ripe when it has grown for as many days as its crop needs. Then Emma harvests it: every seed gives one crop, and the crops go to the barn. And the plot starts growing again from 0: on Emma's farm, every plot grows its crop again after the harvest.
The barn is a dictionary. Its keys are the names of the crops, and its values say how many of them are in the barn. Adding to it is the counting pattern from the lesson on dictionaries: barn[name] = (barn[name] or 0) + plot.seeds. The first time, barn[name] is nil, and or 0 turns it into 0. Without it, the program stops at the very first harvest:
main.lua:95: attempt to perform arithmetic on a nil value (field '?')The question mark is there because the key isn't a name written in the code, but the value of the variable name. And watch out for barn.name: that's the key "name", not the crop's name, so all the crops would end up under one key.
Harvest a plot right after its description goes into the day's list. Its line then shows carrot 3/3 on the day of the harvest, and carrot 1/3 the next day. Harvested before the description, it would show carrot 0/3.
At the end of the season, Emma takes everything in the barn to the market. The function sell(barn) prints one line with every crop, how many there are and the price of one, and the total. Then it returns the total, and the main program adds it to the coins. The crops should be in alphabetical order. But pairs goes through a dictionary in no particular order, which can even change from one run to the next. So first collect the keys in a list and sort it, as in the lesson on dictionaries. For example, eggs counted by the hen that laid them:
local laidBy = {"Pepper", "Goldie", "Pepper"}
local counts = {}
for _, hen in ipairs(laidBy) do
counts[hen] = (counts[hen] or 0) + 1
end
local names = {}
for name in pairs(counts) do
table.insert(names, name)
end
table.sort(names)
for _, name in ipairs(names) do
print(name, counts[name])
endThis prints:
Goldie 1
Pepper 2In your program, a function of its own, sortedNames(t), will return the sorted list of a dictionary's keys. sell then goes through sortedNames(barn) with ipairs, and for each name, it gets the crop's price with findCrop. Put the two functions under describePlot: sell calls findCrop, so it must be below it.
Instructions
In the main program, create an empty dictionary barn above the loop over the days. In the inner loop, under the line that adds the plot's description to the list, harvest the plot if it's ripe: add its seeds to the barn under the crop's name, and set its grown back to 0. The days now look like this:
Day 1 (sunny): carrot 1/3, wheat 1/2, pumpkin 1/6
Day 2 (sunny): carrot 2/3, wheat 2/2, pumpkin 2/6
Day 3 (rainy): carrot 3/3, wheat 2/2, pumpkin 4/6
Day 4 (cloudy): carrot 1/3, wheat 1/2, pumpkin 5/6
Day 5 (sunny): carrot 2/3, wheat 2/2, pumpkin 6/6
Day 6 (cloudy): carrot 3/3, wheat 1/2, pumpkin 1/6If a plot shows 0/3 instead of 3/3, you harvest it before its description.
Under describePlot, write a function sortedNames(t) that collects the keys of the dictionary t in a list with pairs, sorts the list with table.sort and returns it.
Under it, write a function sell(barn). For each name from sortedNames(barn), it makes a text such as carrot 8 x 5 (the name, the number in the barn and the market's price), and it adds the number times the price to the total. Then it prints one line with Market:, the texts joined with ", ", an equals sign and the total, for example Market: carrot 8 x 5, wheat 18 x 3 = 94, and it returns the total.
At the end of the main program, add what sell(barn) returns to coins, and print the coins at the end of the season. The program ends like this:
Day 6 (cloudy): carrot 3/3, wheat 1/2, pumpkin 1/6
Market: carrot 8 x 5, pumpkin 2 x 20, wheat 18 x 3 = 134
End of the season: 140 coinsEmma sold carrots from two harvests (4 + 4) and wheat from three (6 + 6 + 6).
Start programming for free
4/7