A team in a game
Radio in a game, agreeing roles without talking, and swapping jobs when the score says so.
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
Shuttle Relay, Control Points, Dodgeball and Capture the Flag are won by teams, and a team that has not agreed anything is two robots doing the same job.
Saying who does what
The radio from Module 9 works in a game exactly as it did in the lesson: send(text) goes to every robot on the mat, and messages() gives you what has arrived since you last looked.
from bugbot import *
connect()
# in a game, the first thing a team does is agree who is doing what
send("striker")
for m in messages():
print("heard:", m)
Everyone hears everything, including the other team. Say what your team needs and nothing you would mind an opponent knowing.
A rule that settles it without talking
Radio is useful, but two robots can agree without a word if they follow the same rule. The usual one is by name or by number: the robot whose name sorts first attacks, the other defends.
from bugbot import *
connect()
me = "bugbot-2"
team = ["bugbot-2", "bugbot-7"]
job = "attack" if me == min(team) else "defend"
print(me, "is going to", job)
That is worth knowing because a rule cannot be jammed, cannot arrive late, and works on the first tick of the game.
Swapping jobs
Fixed jobs lose to a team that notices. The pattern is: do your job, watch the score, and swap when it makes sense.
- Attacker taken out? The defender becomes the attacker.
- Two goals ahead with ten seconds left? Everyone defends.
- Both chasing the same ball? The nearer one takes it, and says so.
# the shape of it, inside the game loop. In Control Points, Capture the Flag and Dodgeball,
# info()['score'] holds every team's score, such as {'red': 2, 'blue': 3}
score = info()['score']
mine = score[info()['team']]
best_other = max(v for t, v in score.items() if t != info()['team'])
if mine < best_other and clock() > 40:
job = "attack"
Task: call it and go
Send a message saying which job you are taking, then do it: drive to the base zone that matches. This time the job is attack: send attack, then drive to the far base, up the mat and to the left. (Defending would mean the near base, down and to the right.)
from bugbot import *
connect()
# say what you are doing, then do it
send("attack")
Challenges
- Choose the job from the robot's own name with
min(), as above, and send what you chose. - Print everything
messages()gives you for ten seconds, so you can see what a busy game sounds like. - Send a message when you arrive, so a team mate knows the job is done.