The Crops
Welcome to the last lesson of the Lua course! In it, you'll put together everything you've learned and build one program step by step: a season on Emma's farm. Emma buys seeds, plants them, watches them grow for six days, harvests them and sells them. When you finish, your program will print this:
PRICE LIST
carrot: seed 2, sells for 5
pumpkin: seed 5, sells for 20
strawberry: seed 4, sells for 12
wheat: seed 1, sells for 3
tomato: seed 2, sells for 7
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 x2
Plot 4: tomato x3 for 6 coins
Coins: 0
Day 1 (sunny): carrot 1/3, wheat 1/2, pumpkin 1/6, tomato 1/4
Day 2 (sunny): carrot 2/3, wheat 2/2, pumpkin 2/6, tomato 2/4
Hugo: wheat 6 x 4 = 24
Day 3 (rainy): carrot 3/3, wheat 2/2, pumpkin 4/6, tomato 4/4
Day 4 (cloudy): carrot 1/3, wheat 1/2, pumpkin 5/6, tomato 1/4
Hugo: carrot 4 x 7, tomato 3 x 9, wheat 6 x 5 = 85
Day 5 (sunny): carrot 2/3, wheat 2/2, pumpkin 6/6, tomato 2/4
Hugo: pumpkin 2 x 21, wheat 6 x 4 = 66
Day 6 (cloudy): carrot 3/3, wheat 1/2, pumpkin 1/6, tomato 3/4
Market: carrot 4 x 5 = 20
End of the season: 195 coinsFirst comes the price list of the crops. Emma has 30 coins, and she buys seeds for her plots. She doesn't have enough coins for the strawberries, but she does for the tomatoes. Then six days pass. Every day, the plots grow, and on a rainy day, twice as much: carrot 2/3 means that the carrots have grown for 2 of the 3 days they need. A ripe crop goes to the barn, and the plot starts growing again. Hugo the trader comes by every day, and when he pays more than the market, Emma sells him what's in her barn. What's left at the end of the season goes to the market.
You'll use most of the course: records in lists, dictionaries, functions, loops, conditions, string.format, a class with a metatable and a coroutine. Each exercise continues where the previous one ended (the editor starts with its finished code) and adds one piece.
The plan. The editor has three comments that split the program into parts. At the top, -- Data: the things the program works with, such as the crops, and later Emma's plan and the weather. Under it, -- Functions, and at the bottom, -- Main program, which uses the data and the functions and prints the results. The order matters. Lua runs the program from top to bottom, and a local variable or function exists only from the line where it's created. So a function that reads crops must be below crops, and the main program, which calls the functions, must be below them.
The first step: the crops. Every crop has a name, the number of days it needs to grow, the price of one seed, and the price the market pays for one crop. That's a record, a table with named fields, as in the lesson on dictionaries. And all the crops together are a list of records. For example, Grandpa Joe's stall at the market:
local stall = {
{name = "eggs", unit = "box", price = 4},
{name = "milk", unit = "bottle", price = 3},
}
for _, item in ipairs(stall) do
print(string.format("%s (%s): %d coins", item.name, item.unit, item.price))
endThis prints:
eggs (box): 4 coins
milk (bottle): 3 coinsNotice the comma after the last record: Lua allows it, and adding another record is then easier. But the comma between two records must not be missing. Without it, the program doesn't run at all: main.lua:3: '}' expected (to close '{' at line 1) near '{'. Lua expected the list to end after the first record, because no comma said that another one follows.
The loop for _, item in ipairs(stall) goes through the list in order. It doesn't need the index, so _ takes its place. string.format puts the values into the text, %s a text and %d a whole number, in the order in which they follow. If a field's name has a typo, for example crop.seedprice with a small p, the field is nil, and %d can't print it: main.lua:16: bad argument #3 to 'format' (number expected, got nil). Argument #3 is the third value in the parentheses of string.format, counting the text with the format.
Instructions
Under -- Data, create a local variable crops: a list of four records with the fields name, days, seedPrice and price. The first one is {name = "carrot", days = 3, seedPrice = 2, price = 5}. Then come a pumpkin (6 days, a seed for 5, sells for 20), a strawberry (5 days, 4, 12) and wheat (2 days, 1, 3). Write the names in lowercase, as you see them in the output: "pumpkin", "strawberry" and "wheat".
Under -- Main program, print the heading PRICE LIST. Under it, go through crops with ipairs and print the name of each crop. The program prints the heading and four names.
Complete the line in the loop with string.format, so that it also shows the price of a seed and what the market pays:
PRICE LIST
carrot: seed 2, sells for 5
pumpkin: seed 5, sells for 20
strawberry: seed 4, sells for 12
wheat: seed 1, sells for 3If the carrot's seed costs 5, you've swapped the two prices.
Start programming for free
1/7