Racing the bots
Practice, replay and the results board: why you lost, and the fast-then-careful pattern that wins.
Do this lesson in the simulatorEvery game comes with built-in bots. They are not there to be beaten once; they are a measuring stick you can run again and again.
Practice is free
Open any game and press Race. Your program runs against the bots on your own screen, as often as you like, with nobody watching. That is where a program gets good.
from bugbot import *
connect()
# park in the first bay: drive most of the way fast, then creep
forward(70, distance=30)
while distance() > 12:
forward(25)
wait(0.1)
stop()
print("parked, gap", round(distance(), 1))
Reading why you lost
After a race you get a results board and a replay. Three things to look at, in this order:
- Did the robot do what you meant? Watch the replay. Most losses are a program doing exactly what it was told.
- Where did the time go? A robot that drives at 30 the whole way loses to one that drives at 90 and creeps at the end.
- What did the bots do differently? Their descriptions are on the game page: one may look further ahead, another may take a straighter line.
Print as you go. Your log is yours alone, and a line like print("state", state, "gap", round(gap, 1)) each time round the loop tells you afterwards exactly when the robot changed its mind.
Fast, then careful
The pattern that wins nearly every game with a target is the same: go fast while the target is far away, slow down as you arrive, and stop before you overshoot.
from bugbot import *
connect()
# how speed could follow distance: far away, fast; close, slow
for gap in [80, 60, 40, 20, 10, 5]:
speed = max(20, min(90, gap * 1.2))
print("gap", gap, "-> speed", round(speed))
That is proportional control from Module 3, used as a tactic.
Task: park square
Drive into the bay and stop inside it, facing the way you started, within 10 degrees. Being in the bay is not enough: finish straight.
from bugbot import *
connect()
forward(60, distance=35)
stop()
print("heading", heading())
Challenges
- Run the same program five times. How much do the finishing positions differ?
- Add a correction at the end: if the heading is out by more than 5 degrees, turn back.
- Try the speed rule above instead of two fixed speeds, and see which parks closer.