← Explorations
Coding a Bot Brain
Experiment with code that drives your bot.
My weapon:
Distance
0
Angle
0
Spinner
0%
Battery
100%
Health
100
Damage
0
Times defeated
0

Think it through: from an idea to rules

Before you touch a single rule, decide what you want your bot to do. Then turning that plan into rules is the easy part. Here is how to think it through.

1. Your bot thinks in a loop

The arena runs your brain every tick, many times a second. Each tick it does the same three things:

SENSEWhere is the enemy? Am I hurt? Is my spinner charged?
DECIDEWhich rule matches the situation right now?
ACTdrive, turn, or fire

It is not a story that runs once. It is those three steps over and over, reacting to what is happening this instant. That is why a rule like "when the enemy is to my right, turn right" keeps aiming at a moving target: it re-checks dozens of times a second.

2. Turn a plan into rules

Say your plan in plain words, then ask one question for each behavior: "What should my bot do WHEN ___?" Every answer is a rule.

Plan: "Chase the enemy and hit it."

  • When the enemy is to my right? turn right
  • When the enemy is to my left? turn left
  • What should I always do? drive forward, and fire

That is the whole brain: four rules, straight from four questions.

3. Order matters: general first, reactions last

Rules run top to bottom, and the lower one wins. So put your "always" rules up top (your default behavior) and your "when the enemy is…" rules below, so they can override the default. "Always go straight" then "when the enemy is to my right, turn right" means: go straight, unless the enemy is to my right.

4. Build it up one rule at a time

Do not write the whole brain at once. Add one rule, press play, and watch. Add the next. If something looks wrong, pause or slow it to ¼x and watch which rule glows: that is the one in charge right at that moment. Fix that rule, not the others.

The glow is your debugger. If your bot drives the wrong way, slow it down and find which rule is lit while it does it. That rule is the problem (or a rule below it is overriding the one you meant to win).

Worked examples: three ways to think

Each of these is a real plan turned into rules. Open the Playground, pick it from the Rules tab's "Load an example" menu, slow it down, and watch the reasoning play out. Then try the tweak.

The Rusher: "get in their face"

Reasoning: I win by landing lots of hits, so I want to be right on top of the enemy with my weapon on. Sense its direction, turn to face it, drive in, and always fire.

  • Always go straight (the default)
  • When enemy is to my right → turn right
  • When enemy is to my left → turn left
  • Always drive forward
  • Always fire my weapons

(Load it as "Charge and fire.")

Your turn: Firing nonstop drains the battery. Change the last rule to "when the enemy is close → fire" and watch it save power for when it counts. (That is the "Fire only up close" example.)

Hit-and-run: "wind up, strike, peel off"

Reasoning: a spinner does almost no damage until it is wound up. So keep it spinning, back off while it charges, and charge in only when it is at full speed.

  • Always aim at the enemy (go straight, then turn toward it)
  • Always fire (keeps the spinner running)
  • When my spinner is not charged → drive backward (back off and rev)
  • When my spinner is charged up → drive forward (commit to the hit)

(Load it as "Hit and run (spinner).")

Your turn: Make it smarter when hurt. Add "when I'm badly hurt → drive backward" at the bottom so it retreats to repair.

The Defender: "survive, then strike"

Reasoning: I would rather outlast the enemy than trade hits. Attack while healthy, but retreat the moment I am hurt so I can repair, and only fire when something is in range.

  • Always aim at the enemy
  • Always drive forward
  • When I'm badly hurt → drive backward (retreat to recover)
  • When enemy is close → fire

(Load it as "Coward (flee when hurt).")

Your turn: Never let a spinner grind on you. Add "when the enemy is right next to me → drive backward."
There is no single best brain. A rusher overwhelms a timid bot; a defender outlasts a reckless one; hit-and-run punishes both if you time it right. The real game is finding the logic that fits your bot and the enemy in front of you.

What a brain is

A brain is a function called botTick() that the arena runs every tick (many times a second). Each tick you read the situation and call commands to act. The whole thing is just:

function botTick() {
  // read the situation, then decide what to do
}

Commands you can call

drive(n)Move. 1 forward, -1 reverse, 0 stop.
turn(n)Turn. 1 right, -1 left, 0 straight.
fire('name')Run the named weapon. fire() with no name runs them all.

Sensors you can read

enemyDistance()Distance to the nearest enemy.
enemyAngle()Direction to the enemy. + = to your right, - = to your left, near 0 = dead ahead.
myHealth()Your health, 0 to 100.
isFlipped()true if you are on your back.
spinnerCharge()How wound up your spinner is, 0 to 1.

Ideas to try in the Playground

  • Movement: chase head-on, keep your distance (kite), or circle the enemy by aiming a little to its side.
  • Weapons: fire all the time, or only fire() when an enemy is close to save battery.
  • Health: drive backward when myHealth() is low to retreat.
  • Spinner timing: use spinnerCharge() to wind up before you commit to a hit.
  • Recovery: fire() when isFlipped() to try to self-right.
The Playground runs a standard spinner bot on the arena's real numbers (top speed, turn rate, spinner wind-up, battery drain), so what works here carries over. Change a value, press Run, and watch what changes. When you like a brain, paste it into your bot in BotForge and take it to the arena.

Check your understanding