Growing

Now the season begins: six days, each with its own weather. On a sunny or a cloudy day, every plot grows by one day. On a rainy day, the rain waters the plots, and they grow by two. The weather of the six days is in a list: sunny, sunny, rainy, cloudy, sunny, cloudy. In a real game, it might be random, with math.random. But then the program would print something else every time you run it, and the tests couldn't check it. So the weather is fixed.

How much the plots grow depends on the weather, and that's a job for a dictionary. Its keys are the kinds of weather, and its values are the days of growth: growth[today] is 2 when today is "rainy". Every kind of weather in the list needs its key in the dictionary. A typo in the list, such as "suny", gives nil, and the program stops in growPlot, which can't add nil:

main.lua:35: attempt to perform arithmetic on a nil value (local 'amount')

A plot can't grow more than its crop needs. Carrots that have grown for 2 of their 3 days have 3 of 3 after a rainy day, not 4. math.min returns the smaller of two numbers, so math.min(plot.grown + amount, plot.crop.days) is the new number of days, but never more than plot.crop.days.

The function growPlot changes the plot it gets, and it doesn't need to return anything. A table isn't copied when you pass it to a function, so the change happens to the plot in the list plots.

Each day's line describes all the plots. That's a pattern from the lesson on lists: build a list of texts in a loop, then join them with table.concat. For example, Emma's hens lay two eggs on a sunny day and one on a rainy day:

local hens = {
    {name = "Goldie", eggs = 0},
    {name = "Pepper", eggs = 0},
}
local laid = {sunny = 2, rainy = 1}

for day, sky in ipairs({"sunny", "rainy"}) do
    local parts = {}
    for _, hen in ipairs(hens) do
        hen.eggs = hen.eggs + laid[sky]
        table.insert(parts, hen.name .. " " .. hen.eggs)
    end
    print("Day " .. day .. ": " .. table.concat(parts, ", "))
end

This prints:

Day 1: Goldie 2, Pepper 2
Day 2: Goldie 3, Pepper 3

The outer loop goes through the days, and the inner one through the hens. ipairs gives both the number of the day and its weather, so in the first pass, day is 1 and sky is "sunny". The list parts is created inside the outer loop, so every day starts with an empty one. Created above the loop, it would keep growing, and the second day's line would list four hens.

Instructions

Under plan, create a list weather with the weather of the six days, "sunny", "sunny", "rainy", "cloudy", "sunny", "cloudy", and a dictionary growth, in which sunny and cloudy have the value 1 and rainy the value 2.

Under findCrop, write two functions. growPlot(plot, amount) adds amount to the plot's grown, but never above its crop's days. describePlot(plot) returns a short text about the plot, for example carrot 2/3: the crop's name, the days it has grown and the days it needs.

At the end of the main program, go through weather with ipairs. Every day, each plot grows by growth[today], and then the program prints a line with the number of the day, its weather and the descriptions of all the plots, joined with ", ". The program ends like this:

Coins: 6
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 3/3, wheat 2/2, pumpkin 5/6
Day 5 (sunny): carrot 3/3, wheat 2/2, pumpkin 6/6
Day 6 (cloudy): carrot 3/3, wheat 2/2, pumpkin 6/6

If the carrots have 4/3 on day 3, the plot grew past its crop's days.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

3/7