The strategy language

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.

Using an assistant

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.

Open the plain-text version

Writing one

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.

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

Open with fixed moves

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.

Example

name: Cautious
open: C C

Add a rule

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.

Syntax

if <condition> -> <action>

Example

name: Cautious
open: C C
if round > 100 -> D "endgame"

React to the opponent

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.

Example

name: Cautious
open: C C
if opp.last == D -> D "tat!"

Remember something

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.

Example

name: Cautious
open: C C
if opp.last == D -> D "tat!" do mem.burned += 1

Finish with else

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.

Example

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

How a round is decided

Five rules govern every round. They are short. The third and fourth are where strategies most often surprise the person who wrote them.

Rules are tried in order

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.

Example

name: Order
if mem.burned >= 3 -> D
if opp.last == D -> D do mem.burned += 1
else -> C

Opening moves bypass everything

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.

Example

name: Opening
open: D D
if round == 1 -> C
else -> C

An unknown fact makes its condition false

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.

Example

payoff >= 3

Example

mem.burned == 0

Effects run last, and read the old state

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.

Example

name: Timing
else -> C do mem.seen = round, mem.total += score.me

History is what was played

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.

Conditions

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

The last move

True when that side's previous move was the one named. False in round 1, when there is no previous move.

Syntax

opp.last == C
me.last == D

Example

opp.last == D

A window of moves

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.

Syntax

opp.last(n) all C|D
opp.last(n) any C|D

Example

opp.last(3) all D

Example

me.last(5) any D

Counting a window

Counts that move over the last n rounds — or over however many have been played, if fewer — and compares the count against a number.

Syntax

opp.last(n) count C|D <op> number

Example

opp.last(10) count D >= 3

Comparing facts

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.

Syntax

<fact> <op> <fact or number>

Example

opp.defections > me.defections

Example

opp.coop_rate < 50%

A coin flip

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.

Syntax

chance n%

Example

chance 10%

Facts

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.

round

The round being played, counting from 1.

Syntax

round

Example

round > 10

opp.defections

How many times that side has defected so far in this match.

Syntax

opp.defections
me.defections

Example

opp.defections > 0

opp.cooperations

How many times that side has cooperated so far in this match.

Syntax

opp.cooperations
me.cooperations

Example

opp.cooperations >= opp.defections

opp.coop_rate

The 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.

Syntax

opp.coop_rate
me.coop_rate

Example

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.

Syntax

opp.run(C)
me.run(D)

Example

opp.run(D) >= 3

payoff

What 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.

Syntax

payoff

Example

payoff >= 3

score.me

The running total for that side in this match. Note the shape — it is score.me, not me.score.

Syntax

score.me
score.opp

Example

score.me > score.opp

mem.name

One of your own counters. Reads as 0 until you set it.

Syntax

mem.name

Example

mem.grudge > 0

Actions

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

C

Cooperate.

Example

else -> C

D

Defect.

Example

else -> D

copy

Play the opponent's last move. Cooperates in round 1, when there is none.

Example

else -> copy "tit for tat"

oppose

Play the opposite of the opponent's last move. Defects in round 1.

Example

else -> oppose

repeat

Play your own last move again. Cooperates in round 1.

Example

else -> repeat "win-stay"

switch

Play the opposite of your own last move. Defects in round 1.

Example

else -> switch "lose-shift"

Memory

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

What a counter is

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.

Example

mem.burned > 0

Setting one

Replaces the value. The right-hand side may be a number, a fact, or a fact with a number added or subtracted.

Syntax

mem.name = number
mem.name = <fact>
mem.name = <fact> + number

Example

mem.burned = 0

Example

mem.seen = round

Example

mem.punish = opp.defections - 1

Adding to one

Adds instead of replacing. The right-hand side is the same as for =.

Syntax

mem.name += number
mem.name += <fact>

Example

mem.burned += 1

Subtracting

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.

Syntax

mem.name -= number

Example

mem.punish -= 1

Naming them

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.

Limits

Every strategy is checked against these. Going over one is a parse failure that names the field, not a silent truncation.

LimitValue
Name, characters32
Mood, characters24
Opening moves50
Rules32
Conditions per rule4
Memory updates per rule4
Memory counters8
Window size50
Largest number100000

When it does not parse

The season page lists every problem it found, each against the line it is on. There are two kinds, and they read very differently.

Messages that name the problem

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

What comes back

  • line 2: unknown fact "opp.lastt"
if opp.last == D then D

What comes back

  • line 1: missing "->"
if opp.last == D -> Z

What comes back

  • line 1: unknown action "Z" (expected one of C, D, copy, oppose, repeat, switch)
hello world

What comes back

  • line 1: unexpected line "hello world" (expected name:, open:, if …, or else …)
open: C X

What comes back

  • line 1: opening moves must be C or D, got "X"

Messages with a path

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

What comes back

  • line 1: rules.0.when.0.n: Too big: expected number to be <=50
if opp.last == D and me.last == D and opp.last == C and me.last == C and round > 1 -> D

What comes back

  • line 1: rules.0.when: Too big: expected array to have <=4 items

A problem with no line

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

What comes back

  • rules: at most 8 memory counters (found 9)

The capital-letter trap

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

What comes back

  • line 1: unknown fact "mem.SHOUTY"
else -> C do mem.SHOUTY = 1

What comes back

  • line 1: cannot understand effect "mem.SHOUTY = 1" (expected mem.name = / += / -= …)