Planting
Emma has 30 coins, and she has a plan: which seeds she wants to buy and how many. She goes through the plan in order. If she has enough coins for an item, she pays for the seeds and plants them on a new plot. If not, she skips the item and goes on to the next one.
An item of the plan has only the crop's name and the number of seeds. Everything else about the crop, such as the price of a seed, is in crops. So the program needs a function that finds the crop with a given name. That's the search from the lesson on dictionaries: a loop goes through the list, and when it finds the right record, return returns it and ends the whole function at once, loop and all. When the loop finds nothing, the function gets to the return nil after it. For example, the animals on the farm:
local animals = {
{name = "Bella", kind = "cow"},
{name = "Rex", kind = "dog"},
}
local function findAnimal(name)
for _, animal in ipairs(animals) do
if animal.name == name then
return animal
end
end
return nil
end
print(findAnimal("Rex").kind)
print(findAnimal("Tom"))This prints:
dog
nilIf the plan has a typo, for example "carot", findCrop returns nil, and the program stops at the first line that reads a field of the crop:
main.lua:40: attempt to index a nil value (local 'crop')A plot is a new record with three fields: crop, the crop's whole record (not only its name), seeds, the number of seeds, and grown, the number of days it has grown, which is 0 for now. You'll need grown in the next step. Thanks to the whole record in crop, the plot can later read plot.crop.days and plot.crop.name. Nothing is copied: as you know from the lesson on dictionaries, plot.crop and the record in crops are the same table.
The plots go into the list plots with table.insert. Right after it, #plots is the number of the new plot: 1 for the first one, 2 for the second. The seeds cost item.seeds * crop.seedPrice coins, and Emma can buy them if the price is <= her coins. With <, she could never spend her last coin.
In string.format, write the x right before %d, without a space: "%s x%d" gives carrot x4.
Instructions
Under crops, create a local variable plan: a list of records with the fields name and seeds. Emma wants 4 carrot seeds, 6 wheat seeds, 2 pumpkin seeds and 2 strawberry seeds, in this order. The first record is {name = "carrot", seeds = 4}.
Under -- Functions, write a function findCrop(name) that returns the crop from crops with this name, or nil.
At the end of the main program, create the variables coins (30) and plots (an empty list). Then go through plan. For each item, find its crop and compute the price of the seeds. If Emma has enough coins, subtract the price, add the plot {crop = crop, seeds = item.seeds, grown = 0} to plots and print a line about it. Otherwise, print that she doesn't have enough coins. Under the price list, the program prints:
Plot 1: carrot x4 for 8 coins
Plot 2: wheat x6 for 6 coins
Plot 3: pumpkin x2 for 10 coins
Not enough coins for strawberry x2If the strawberries get a plot too, check that you subtract the price from coins.
After the loop, print how many coins Emma has left. The program ends like this:
Not enough coins for strawberry x2
Coins: 6If the line appears four times, you print it inside the loop.
Start programming for free
2/7