Page:
Step 3 Buy Upgrades
Pages
Home da
Home ms
Home
Step 1 Get to Know the Game da
Step 1 Get to Know the Game ms
Step 1 Get to Know the Game
Step 2 Click the Cookie da
Step 2 Click the Cookie ms
Step 2 Click the Cookie
Step 3 Buy Upgrades da
Step 3 Buy Upgrades ms
Step 3 Buy Upgrades
Step 4 Five Minute Run da
Step 4 Five Minute Run ms
Step 4 Five Minute Run
Step 5 Improve the Strategy da
Step 5 Improve the Strategy ms
Step 5 Improve the Strategy
Clone
1
Step 3 Buy Upgrades
Tirsvad edited this page 2026-10-28 11:07:58 +01:00
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).
Cookie Clicker Bot
- Home
- Step 1: Get to know the game
- Step 2: Click the cookie
- Step 3: Buy upgrades
- Step 4: Five minute run
- Step 5: Improve the strategy
Language
Cookie Clicker Bot, a solution to day 48 of Udemy 100 Days of Code. English · Dansk · Bahasa Malaysia
