Give it a name
name: is the line every strategy should have. It is what the standings show. Leave it out and your entry is called Untitled.
Example
name: Cautious
A strategy is a short piece of text. You write it once; the league runs it against every other entry, over and over.
Every example on this page is checked by the same parser that reads your submission, so none of them can quietly stop being true.
If you would rather describe a strategy than write one, copy the machine-readable version of this page into any AI assistant and tell it what you want. It is English only, and it is generated from the same parser as this page, so it cannot fall out of date.
We will build a strategy called Cautious, a line at a time. Each listing below is complete and parses on its own, so you can stop at any step and submit what you have.
name: is the line every strategy should have. It is what the standings show. Leave it out and your entry is called Untitled.
name: Cautious
open: lists moves to play before any rule is consulted — one per round, separated by spaces. Cautious opens by cooperating twice.
Opening moves are not a rule. Nothing you write below them runs while they last.
name: Cautious open: C C
A rule pairs a condition with an action, in the shape below. Rules are tried from the top, and the first one whose condition holds decides the round.
The text in quotes is a mood. It is optional and changes nothing, but it shows up in the replay, so you can see which rule fired and why the round went the way it did.
if <condition> -> <action>
name: Cautious open: C C if round > 100 -> D "endgame"
opp.last is the move your opponent played last round. This one line is the whole of Tit for Tat: punish a defection, do nothing else.
Facts about your opponent start with opp., and the same facts about you start with me.
name: Cautious open: C C if opp.last == D -> D "tat!"
do runs after the action has been decided. mem.burned is a counter of your own: it starts at 0, holds whole numbers, and lasts for the match.
You choose the name. Lowercase letters, digits and underscores, and you get eight counters.
name: Cautious open: C C if opp.last == D -> D "tat!" do mem.burned += 1
else is the card played when no rule matched. Without one, your strategy cooperates.
Look at the order. The mem.burned rule sits above the rule that increments it, because the first match wins. Swap those two lines and Cautious never gets past three, since the counting rule fires first every time and the one below it is never reached.
name: Cautious open: C C if mem.burned >= 3 -> D "done being nice" if opp.last == D -> D "tat!" do mem.burned += 1 else -> C
Five rules govern every round. They are short. The third and fourth are where strategies most often surprise the person who wrote them.
Top to bottom. The first match fires and nothing below it is consulted. Put the narrow rule above the broad one, or the broad one will answer for both.
name: Order if mem.burned >= 3 -> D if opp.last == D -> D do mem.burned += 1 else -> C
While open: still has moves left, the rules are not consulted at all — not even one whose condition plainly holds. Their do clauses do not run either, so a counter you expected to be set is still 0 on the first round after the opening.
The listing below plays D on round 1, despite the rule saying otherwise.
name: Opening open: D D if round == 1 -> C else -> C
payoff has no value in round 1, and opp.coop_rate has none until the opponent has moved. A condition that reads one of them is simply false, and the rule does not fire. It is not an error, and you will not be told.
Memory is the exception. A counter you have never set reads as 0, so mem.burned == 0 is true from the very first round — which is rarely what someone writing that line intends.
payoff >= 3
mem.burned == 0
The action is decided first. Then every do on that line runs, in the order written.
All of them read the round as it was when it started. In the listing below, mem.total gains the score you had at the top of the round — not one already changed by the update sitting beside it.
name: Timing else -> C do mem.seen = round, mem.total += score.me
A season can be run with noise, which flips a move after your strategy has chosen it. Everything you can ask about — opp.last, the counts, the runs, the scores — reports the move that was actually played, not the one that was decided.
The replay records both, so when a match goes strangely you can see which rule fired and what the round did with it.
A rule holds when all of its conditions hold. Join them with and. There is no or — write the other branch as a second rule — and there are at most four conditions to a rule.
name: Both if opp.last == D and chance 10% -> C "forgiven" else -> C
True when that side's previous move was the one named. False in round 1, when there is no previous move.
opp.last == C me.last == D
opp.last == D
all needs the full window: with fewer than n moves played it is false. any looks at whatever history exists, and is false when there is none.
opp.last(n) all C|D opp.last(n) any C|D
opp.last(3) all D
me.last(5) any D
Counts that move over the last n rounds — or over however many have been played, if fewer — and compares the count against a number.
opp.last(n) count C|D <op> number
opp.last(10) count D >= 3
The operators are ==, !=, <, <=, > and >=. Either side may be a fact; the right-hand side may also be a plain number.
A trailing % on that number is allowed and ignored, so opp.coop_rate < 50% reads the way you would say it out loud.
<fact> <op> <fact or number>
opp.defections > me.defections
opp.coop_rate < 50%
True with probability n, a whole number from 0 to 100. The season's seed decides it, so replaying the same season flips the same way every time.
chance n%
chance 10%
A fact is a number you can compare. Facts about your opponent start with opp. and facts about you with me. — except score, which puts the side last.
roundThe round being played, counting from 1.
round
round > 10
opp.defectionsHow many times that side has defected so far in this match.
opp.defections me.defections
opp.defections > 0
opp.cooperationsHow many times that side has cooperated so far in this match.
opp.cooperations me.cooperations
opp.cooperations >= opp.defections
opp.coop_rateThe share of that side's moves that were C, from 0 to 100. It has no value until that side has moved, so a condition using it is false in round 1.
opp.coop_rate me.coop_rate
opp.coop_rate < 50%
opp.run(C)The length of the run that side is currently on, if it is a run of that move — and 0 if their last move was the other one.
opp.run(C) me.run(D)
opp.run(D) >= 3
payoffWhat you scored last round: 5 for defecting against a cooperator, 3 when both cooperated, 1 when both defected, 0 for cooperating against a defector. It has no value in round 1.
payoff
payoff >= 3
score.meThe running total for that side in this match. Note the shape — it is score.me, not me.score.
score.me score.opp
score.me > score.opp
mem.nameOne of your own counters. Reads as 0 until you set it.
mem.name
mem.grudge > 0
The right-hand side of a rule. An action may be followed by a mood in double quotes, and then by do and a comma-separated list of memory updates.
name: Mood if opp.last == D -> D "tat!" do mem.burned += 1 else -> C
CCooperate.
else -> C
DDefect.
else -> D
copyPlay the opponent's last move. Cooperates in round 1, when there is none.
else -> copy "tit for tat"
opposePlay the opposite of the opponent's last move. Defects in round 1.
else -> oppose
repeatPlay your own last move again. Cooperates in round 1.
else -> repeat "win-stay"
switchPlay the opposite of your own last move. Defects in round 1.
else -> switch "lose-shift"
Counters carry what you have noticed from one round to the next. They hold whole numbers, they start at 0, they are cleared between matches, and you get eight of them.
name: Gradual-ish if mem.punish > 0 -> D "still mad" do mem.punish -= 1 if opp.last == D -> D "GRUDGE!" do mem.punish = opp.defections - 1 else -> C
A named whole number, yours for the length of a match. Reading one you never set gives 0 — memory is the one kind of fact that is never undefined.
mem.burned > 0
Replaces the value. The right-hand side may be a number, a fact, or a fact with a number added or subtracted.
mem.name = number mem.name = <fact> mem.name = <fact> + number
mem.burned = 0
mem.seen = round
mem.punish = opp.defections - 1
Adds instead of replacing. The right-hand side is the same as for =.
mem.name += number mem.name += <fact>
mem.burned += 1
Takes a plain number only — no facts. mem.punish -= round is rejected, where mem.punish = mem.punish - 1 is not what you want either, since it reads the value from the start of the round.
mem.name -= number
mem.punish -= 1
Lowercase letters, digits and underscores, starting with a letter or an underscore, up to 16 characters.
A capital letter is not reported as a naming mistake. mem.Burned fails the name pattern before anything else is tried, so it comes back as an unknown fact instead. See the last part of this page.
Every strategy is checked against these. Going over one is a parse failure that names the field, not a silent truncation.
| Limit | Value |
|---|---|
| Name, characters | 32 |
| Mood, characters | 24 |
| Opening moves | 50 |
| Rules | 32 |
| Conditions per rule | 4 |
| Memory updates per rule | 4 |
| Memory counters | 8 |
| Window size | 50 |
| Largest number | 100000 |
The season page lists every problem it found, each against the line it is on. There are two kinds, and they read very differently.
Most of them. They quote the text that confused the parser, which is usually enough to find the typo without reading anything else.
name: X if opp.lastt == D -> D
if opp.last == D then D
if opp.last == D -> Z
hello world
open: C X
A limit violation is caught after parsing, by the schema, so it names a field rather than your text. Read the path from the left: rules.0 is the first rule, when.0 is its first condition, and n is the number inside the window.
So the first one below says the window in the first condition of the first rule is larger than 50.
if opp.last(99) all D -> D
if opp.last == D and me.last == D and opp.last == C and me.last == C and round > 1 -> D
Some problems belong to no single line, and are listed without one. The counter ceiling is the one you are likely to meet: nine counters spread across three lines is not any one line at fault.
else -> C do mem.a = 1, mem.b = 1, mem.c = 1, mem.d = 1 if round > 1 -> C do mem.e = 1, mem.f = 1, mem.g = 1, mem.h = 1 if round > 2 -> C do mem.i = 1
Both of these are the same mistake — a counter name with a capital letter in it — and neither message says so.
A name that fails the pattern stops being a name at all. In a condition it is then an unknown fact; in a do it is an effect that cannot be understood. Neither message mentions the capital.
if mem.SHOUTY > 0 -> D
else -> C do mem.SHOUTY = 1