README with the five lecture step screenshots and the option table, Doxygen builds without warnings, wiki pages for the home page and the five steps in three languages (screenshots and what each step taught), sidebar and footer. Closes #29 Closes #30 Closes #31 Closes #32 Closes #33
2.3 KiB
2.3 KiB
English · Dansk · Bahasa Malaysia
Lecture step 3: buy the most expensive affordable upgrade
What the assignment asks
Every 5 seconds, look at the right-hand pane, find the upgrades you can afford, and buy the most expensive one. For example, with 105 cookies both Cursor (15) and Grandma (100) are affordable, so the bot buys Grandma. The hints point to split, strip and replace for turning the page text into numbers.
What we built
parse_amountandparse_cookie_countturn page text into numbers. They remove the wordcookies, drop everything afterper second, take away the thousands commas withreplace, and understand words such as1.4 million. Examples:5,000 cookiesgives 5000;1.235andmillion cookieson two lines gives 1,235,000.SeleniumGame.upgradesreads every visible product: its id, its name and its price.choose_most_expensive_affordable(upgrades, cookies)is a pure function: of the upgrades whose price is not above the cookies, return the most expensive one, or nothing. It is tested with the example above (105 cookies gives Grandma).click_and_buykeeps clicking, and whencheck_interval_seconds(5 by default) have passed it reads the count, chooses, and buys.- In a 45 second container run the bot bought 8 upgrades, and the screenshot shows the Cursors and Grandmas it owns.
What we gained in this step
- The hard part is the text, not the click. The same number appears as
120,5,000,1.4 millionand on two lines. Parsing is a small pure function with many test cases, so it is safe to change. - Keep the rule away from the browser. "Buy the most expensive affordable upgrade" is one function that knows nothing about Selenium. That is why step 5 could add a second rule without touching the browser code.
- Do not count a purchase you did not make. The first version logged a purchase whenever it clicked. A real run showed the click did not always reach the product, so
buynow says whether it did, and only real purchases are counted (see Step 4).
