Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Welcome to Code Bots 2!

You've learned your lesson since the last Code Bots. You've tried to figure out more ways to fit more actions in less lines, and now you finally have it. You're going to make an event-driven Code Bot.

Your bot must consist of 24 lines. Each line follows one of the two formats:




Your bot also have enough storage to store 5 integers named A through E. An integer can store values from 0 to 23.

Each turn, you will execute line C, unless one of the conditions is true. If so, then C will then contain the line number of the conditional that is true, and then that line will be executed. At the end of each turn, C will be incremented.

There are the available conditions:

  1. Start is true only on the first turn. You must have this in your code exactly once
  2. BotAt(N) is true if there is a bot at the location defined by N
  3. Equals(A,B) is true if A equals B. They can be different types, if so, they will not be equal.
  4. Modified(A) is true if A was copied to during the last turn. A must be a variable name, a line, or a condition
  5. Any(C1,C2,...) is true if any of the conditions are true
  6. None(C1,C2,...) is true if none of the conditions are true
  7. All(C1,C2,...) is true if all of the conditions are true
  8. Not(C) is true if C is false. C must be a condition.

Variables can be in one of the following formats. The first 9 are numerical, and can be be used for whenever N is used in this page.

  1. A,B,C,D,E
  2. A number from 0 to 23
  3. This will return the line number it is currently on
  4. Add(N1,N2,...) will return the sum of all values
  5. Sub(N1,N2) will return N1 minus N2
  6. Mult(N1,N2,...) will return the product of all values
  7. Div(N1,N2) will return N1 divided by N2
  8. Mod(N1,N2) will return N1 mod N2
  9. OVar(N) will accept a variable name, and will return the opponent's variable
  10. Line(N) will return the Nth line in your code
  11. Type(N) will return the Nth line type in your code (the types are the names of the actions)
  12. Cond(N) will return condition on the Nth line
  13. CondType(N) will return the condition type on the Nth line (the types are the names of the conditions)
  14. OLine(N) will return the Nth line in your opponent's code
  15. OType(N) will return the Nth line type in your opponent's code
  16. OCond(N) will return the condition on the Nth line
  17. OCondType(N) will return the condition type on the Nth line

A and B are for your personal use, C is used to determine which line to execute in your code, and D is used as a direction. Each value of D refer to a different square and direction pair.E produces a random value each time. D and E will be initialized to a random value, otherwise 0.

The direction used will be [North,East,South,West][D%4]. Your opponent is the bot in the immediate square in that direction.

There are 4 actions available to you:

  1. Move will move you 1 square forward in the Dth direction. If there is a bot there, you will not move.
  2. Copy(A,B) will copy the variable A to B. B cannot be a numerical value, except for a variable name. A and B cannot be of different types. Copying a line does not copy the condition.
  3. Flag does nothing. The bot with the most flags in your code will get a point. The bot with the most points wins.
  4. If(C,L1,L2) will perform the line on L1 if C is true, else performs L2. C is a condition, and L1 and L2 must be lines.

The Big Picture

50 copies of every bot will be placed in the world. Your goal is to get your flag into as many bots as possible. For each bot that has more of your flag type than any other flag type, you get a point.

The bots will be placed as follows:


There will be 10 games run, and points will be averaged across all of the games, determining who the winner is.

Side Notes

If multiple conditions apply, then the one that most immedately follows Start will be executed

The bots will be closely packed but you will not start neighboring another bot. (It techincally will be the same format as the last CodeBots)

As this challenge was not posted in the sandbox (to give nobody an advantage), I reserve the right to change small details for fairness, or additional capabilities. Also, if there is a bug in the CodeBots runner, I will change it, even if a bot depended on that bug for its success. I am trying to be as fair as possible.

Recursive If statements will not be executed

If your bot is shorter than 24 lines, the remaining lines will be filled with Flag

Remember when copying to your own C, that C is incremented at the end of your turn.

The CodeBots interpreter can be found here. It includes a .jar file for easy execution. Simply add your bot to the bots folder


  1. 893.9 Borg
  2. 1.3 LazyLioness
  3. 0.9 Defender
  4. 0.5 Flagger
  5. 0.4 CliqueBot
  6. 0.4 Insidious
  7. 0.3 Attacker
  8. 0.3 Gard
  9. 0.3 SingleTarget
  10. 0.2 FreezeBot
  11. 0.2 Sentinel
  12. 0.2 Driveby
  13. 0.0 AntiInsidious
  14. 0.0 MoveBot
  15. 0.0 CliqueBorg
  16. 0.0 Calculator
  17. 0.0 TestBot
  18. 0.0 Imitator


Lines of code are now rotated when you view your opponent's code. That means, your opponent's line 1 may be line 14 (or whatever line). A bot will have a fixed offset which will offset his lines by X amount when viewed by an opponent. The opponent's C variable will also be offset by the same X amount. X will not change within the same game, but it will change from game to game.

share|improve this question
Nathan: Are you able to check out "Lazy Lioness" to see whether the bugs I've reported are legitimate (or if I'm just out to lunch)? No rush. Just making sure you're aware of the submission. – COTO Sep 10 '14 at 5:31
@COTO Sorry, schools been crazy. I'll try to get to it tonight. – Nathan Merrill Sep 10 '14 at 17:01
The 'All' condition could be made implicit for any comma separated list of conditions. It would make reading the entries a bit easier. – ccarton Sep 11 '14 at 11:27
I think I found your bug. The FuctionParser objects are being used as keys in a HashMap which means you need a proper equals method. This fixes the problem with CliqueBot and likely Lazy Lioness as well. I sent you a pull request. – ccarton Sep 11 '14 at 20:47
@ccarton the HashMap only memoizes the values, it isn't used for testing equality – Nathan Merrill Sep 11 '14 at 23:53

16 Answers 16

up vote 12 down vote accepted


Converts all other bots into clones of itself. Resistance is futile.

Start:Copy(2,A)                               # Cloning will begin at line 2
All(Not(BotAt(D)),Not(Equals(2,A))):Copy(2,A) # Reset A if the opp left before we were done
All(Equals(Line(Sub(This,3)),OLine(0)),Equals(Line(Sub(This,2)),OLine(1)),Equals(Line(Sub(This,1)),OLine(2)),Equals(Line(This),OLine(3)),Equals(Line(Add(This,1)),OLine(4)),Equals(Line(Add(This,2)),OLine(5)),Equals(Line(Add(This,3)),OLine(6)),Equals(Line(Add(This,4)),OLine(7)),Equals(Line(Add(This,5)),OLine(8))):Copy(E,D) #  Check if cloning is complete
All(Equals(A,2),Not(Equals(OCond(1),Cond(Add(This,4))))):Copy(Cond(Add(This,4)),OCond(1)) # Copy freeze cond to OLine(1) before cloning starts
All(Equals(A,2),Not(Equals(OLine(1),Line(Add(This,3))))):Copy(Line(Add(This,3)),OLine(1)) # Copy freeze line
Not(Equals(Cond(Add(Sub(This,6),A)),OCond(A))):Copy(Cond(Add(Sub(This,6),A)),OCond(A))    # Copy Cond(A) to OCond(A)
Not(Equals(Line(Add(Sub(This,7),A)),OLine(A))):Copy(Line(Add(Sub(This,7),A)),OLine(A))    # Copy Line(A) to OLine(A)
Equals(A,A):Copy(Add(A,1),A) # Increment A. It will wrap around all 24 lines before the completion check matches

Edit: Small fix to reset A if the opponent moves before I'm done with him. Doesn't seem to affect the score but it makes me feel better.

Edit #2: Added a more complete check to ensure the cloning process has completed properly (line 3)

Edit #3: Update to handle the new random offsets. The difficulty here was that new clones would have their code located at random offsets which means they don't know the location of their own lines. That means that all references to my own lines must be relative (to This). Opponent line numbers can still be absolute since they are random anyway.

share|improve this answer
Hey, can we chat about this bot?… – Nathan Merrill Sep 13 '14 at 13:16
@Nathan, did you use the latest version of this entry in the most recent run? – ccarton Sep 14 '14 at 22:43
I am amazed that this bot is able to infect other bots so successfully. I'd love to see a rundown of how it works. – Sparr Sep 14 '14 at 22:49
@ccarton no. I didn't realize it was updated. Rerunning. – Nathan Merrill Sep 14 '14 at 23:37
@Sparr I've added some comments. Hope that makes it easier to follow. I think the bot is so effective because of the exponential effect of the cloning. Each bot it infects goes out and infects others. Also, they will naturally try to repair each other when they take damage. – ccarton Sep 15 '14 at 0:02


Shoot for the moon


And the rest gets auto-filled with flag.

share|improve this answer


This bot doesn't understand the goal of this challenge, so he decided to calculate some numbers for the enemy.

share|improve this answer
you should put the results into OVar(D) not A! – MegaTom Sep 5 '14 at 14:41
@MegaTom Done ;) – Manu Sep 5 '14 at 14:44


Start: Copy(11,B)
Not(Equals(Line(20),Line(21))): If(Equals(Line(21),Line(22)),Line(7),Line(8))
Not(Equals(Line(21),Line(22))): If(Equals(Line(20),Line(21)),Line(9),Line(10))
All(BotAt(D),Not(Equals(11,OVar(B)))): If(Equals(Line(20),OLine(OVar(C))),Line(10),Line(11))
Any(BotAt(D),Equals(E,B)): Copy(Add(D,1),D)
Equals(1,1): Move

Recognizes friends via their B value, fills everyone else's lines with flags. Also goes to some length to preserve the integrity of one of its own flags (this part is cute but probably not very useful).

Edit: Unsurprisingly, there appears to be a bug here somewhere, judging by the score.

Suspect that lines are 0-indexed and my code is 1-indexed. Should really have checked that earlier. Added a Flag at the beginning to bump everything up one.

share|improve this answer
I think I found the bug in the controller. I sent him a pull request. Until he accepts it, you can try my fork if you like and let me know if it works for you. – ccarton Sep 11 '14 at 20:52
The bug has been fixed in the latest version. – Nathan Merrill Sep 13 '14 at 11:58


share|improve this answer


share|improve this answer
Shouldn't that be Copy(23,C)? – Sean Latham Sep 4 '14 at 18:16
Negative values are now allowed. – Nathan Merrill Sep 4 '14 at 18:16


share|improve this answer
BotAt(D):Copy(Line(X),OLine(E)) where line X contains Equals(1,1):Flag ... for fun – Seeq Sep 5 '14 at 18:35
@Sieg Line copies only the action, not the condition. – MegaTom Sep 5 '14 at 19:05
My overpowered tactic :( – Seeq Sep 5 '14 at 19:08

Single Target

All(BotAt(D),Not(Equals(OVar(D),D))): Copy(D,OVar(D))

Will hunt you down and fill you with flags!

share|improve this answer
The All condition needs a ) – Nathan Merrill Sep 5 '14 at 1:50


Start:Flag           # comments -->                                                                                 # why move when we can fit another flag here?
Equals(E,0):Copy(Add(D,Sub(Mult(2,Mod(E,2)),1)),D)                                                                  # 1/24 chance to turn left or right
All(BotAt(D),Not(Equals(OCond(Sub(OVar(C),1)),Cond(Add(This,4))))):Copy(Cond(Add(This,4)),OCond(Sub(OVar(C),1)))    # Copy the freeze condition
All(BotAt(D),Not(Equals(OLine(Sub(OVar(C),1)),Line(Add(This,4))))):Copy(Line(Add(This,4)),OLine(Sub(OVar(C),1)))    # Copy the flag-copying line
All(BotAt(D),Not(Equals(OLine(Add(OVar(C),0)),Line(Add(This,Add(4,Mod(E,14))))))):Copy(Line(Add(This,Add(4,Mod(E,14)))),OLine(Add(OVar(C),0))) # copy one of my flags to them
BotAt(D):Copy(Add(D,Sub(Mult(2,Mod(E,2)),1)),D)                                                                     # turn left or right if we've infected our target
Equals(A,A):Move                                                                                                    # move if all else fails, also infection freeze condition
Copy(Line(Add(This,1)),Line(Add(This,Mod(E,22))))                                                                   # infection line 1
Flag                                                                                                                # first of many flags

Similar idea to the bot by the same name in the previous contest. Move until I hit another bot, then freeze it into a loop overwriting its own code with my flags. This time the infected bots overwrite random lines instead of sequential lines, making the infection process a little less effective but much faster.

share|improve this answer
Line(4) can be simplified a bit. Add(OVar(C),0) is the same as OVar(C) and Add(This,Add(4,Mod(E,14))) is the same as Add(This,4,Mod(E,14)). – MegaTom Sep 8 '14 at 18:31
@MegaTom I did the first unnecessary Add to make the logical parts of the preceding lines line up. The second one was due to not knowing that Add could take multiple parameters. Thanks – Sparr Sep 8 '14 at 20:24


Start: Move

Attacks any robot next to it.

share|improve this answer
Your last line cannot copy from a condition to a line. – Nathan Merrill Sep 5 '14 at 1:53
@NathanMerrill I was just fixing that... – MegaTom Sep 5 '14 at 2:24
OCon needs to be OCond – Nathan Merrill Sep 5 '14 at 2:30

Freeze Bot


Traps you in a loop Incriminating your own A variable, then fills you with flags and moves on to the next victim.

share|improve this answer
Line 3: Please replace D+1 with Add(D,1) – Nathan Merrill Sep 5 '14 at 15:42
You're assuming that Start is on line zero. Now everyone is incentivized to put their Start on line 2. This is going to lead to a silly escalation/resubmission trend :( – Sparr Sep 5 '14 at 18:41



Predicts what you would do, then does that.

share|improve this answer
Please replace D+1 with Add(D,1) – Nathan Merrill Sep 5 '14 at 17:18
Also, you cannot pass in a command as a line. You either have to pass in a OLine or a Line – Nathan Merrill Sep 5 '14 at 17:31
@NathanMerrill ready to go! (At least I think it is) – MegaTom Sep 5 '14 at 19:00
This bot really put my code through some trouble, but that was my fault, not yours. There are a few bugs, please put Line around the 2, and Add(D,1) instead of D+1 – Nathan Merrill Sep 6 '14 at 13:36

Lazy Lioness

Originally just "Lioness", my first submission to the contest earned the title "lazy" by literally doing nothing when introduced into the simulator.

Her lethargy is in fact due to a bug (or possibly my misunderstanding the event model) wherein the conditions in the first three lines (a simple parity check for ensuring that flags are not overwritten) occasionally, inexplicably evaluate true, locking leo in a lemniscate loop. Several other conditions (particularly those that rely on the BotAt() condition) also evaluate true at times when no adjacent bot(s) are present. Finally, Move and Copy directives are clearly ignored when stepping through the simulator. Since my conditional logic chains are somewhat epic, there's plenty of room for bugs in my code and in the simulator. ;)

In any case, I submit Lazy Lioness as a test case for either error diagnosis or simulator debugging, which will hopefully lead to the emergence of not-so-lazy Lioness that I can then simulate and refine as my first bona fide submission to the Bots v2 competition.

It Does Nothing

share|improve this answer
I can't see a bug, but just in case you should probably use Modified condition instead of some of the Equals – MegaTom Sep 10 '14 at 17:09
cliqueBot looks like it has the same problem. I think it is a compiler bug. – MegaTom Sep 10 '14 at 17:24
@MegaTom: The Modified condition is an invitation for an infinite loop. If I can't use a parity check, I'll just remove the lines entirely. Figure out something else useful to put in 'em. ;) – COTO Sep 10 '14 at 19:40
I think I found the bug in the controller. I sent him a pull request. Until he accepts it, you can try my fork if you like and let me know if it works for you. – ccarton Sep 11 '14 at 20:51


improvment on Gard. Hits nearby bots with flags on the first 8 lines. (that is, all the most used ones)

share|improve this answer
I think you have a misplaced bracket on line 3. The first Not isn't closed properly. I think it should be All(BotAt(D),Not(Equals(OLine(Mod(E,8)),Line(0))),Not(BotAt(Add(D,1,Mod(E,3))))‌​): – ccarton Sep 12 '14 at 22:38


BotAt(D): Copy(Line(Sub(This,1)),OLine(E))
Equals(1,1): Copy(Line(Sub(This,2)),Line(Add(This,Mod(E,21))))
Start: Move

Trying to combine CliqueBot and Borg technology to create a bot that recognizes copies of itself despite the line offset. It also starts its copying process at the last executed line of code on the opposing bot, rather than line 0, which is more likely to freeze it in place but also more likely to result in a corrupted copy (in fact, I'm not sure this even works, I haven't tested it and it's pretty complex).

share|improve this answer
Please replace Plus with Add and Minus with Sub – Nathan Merrill Sep 14 '14 at 20:01


One last try before the deadline.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.