Back to leaderboard

Swimmingbot1v3

Created 9/13/2026

Active

Adaptive v3

Avg score

5.719

Wins

8216

Losses

1884

Win rate

77.2%

Total battles: 10637

Score history

Score trend over the last 200 battles

Avg: 5.719

Recent battles

Source

export default function bot(state) {
    const history = state?.history || [];
    const memory = state?.memory || {
        mode: "unknown",
        farming: false,
        farmRounds: 0,
        probes: 0,
        punish: 0,
        cooperation: 0,
        defection: 0
    };

    const round = history.length;

    if (round === 0) {
        return ["C", memory];
    }

    const last = history[round - 1];
    const opp = last.opponent;
    const mine = last.you;

    if (opp === "C") {
        memory.cooperation++;
    } else {
        memory.defection++;
    }

    if (round === 1) {
        return ["C", memory];
    }

    if (round === 2) {
        if (
            history[0].opponent === "C" &&
            history[1].opponent === "C"
        ) {
            memory.mode = "cooperative";
        }

        if (
            history[0].opponent === "D" &&
            history[1].opponent === "D"
        ) {
            memory.mode = "aggressive";
        }

        return ["C", memory];
    }

    if (round >= 6 && memory.defection === 0) {
        memory.mode = "always-cooperate";
    }

    if (memory.mode === "always-cooperate") {
        if (opp === "D") {
            memory.mode = "reactive";
            memory.farming = false;
            return ["D", memory];
        }

        return ["D", memory];
    }

    if (round >= 5 && memory.cooperation === 0) {
        memory.mode = "always-defect";
        return ["D", memory];
    }

    if (round >= 7) {
        let matches = 0;
        let checks = 0;

        for (let i = 1; i < round; i++) {
            if (
                history[i].opponent ===
                history[i - 1].you
            ) {
                matches++;
            }

            checks++;
        }

        if (
            checks >= 6 &&
            matches / checks >= 0.8
        ) {
            memory.mode = "tft";
        }
    }

    if (round >= 8) {
        let reverse = 0;
        let checks = 0;

        for (let i = 1; i < round; i++) {
            if (
                history[i].opponent !==
                history[i - 1].you
            ) {
                reverse++;
            }

            checks++;
        }

        if (
            checks >= 7 &&
            reverse / checks >= 0.8
        ) {
            memory.mode = "reverse";
        }
    }

    if (round >= 8) {
        let alternating = true;
        const start = Math.max(1, round - 8);

        for (let i = start; i < round; i++) {
            if (
                history[i].opponent ===
                history[i - 1].opponent
            ) {
                alternating = false;
                break;
            }
        }

        if (alternating) {
            memory.mode = "alternating";
        }
    }

    const window = Math.min(10, round);

    let recentC = 0;
    let recentD = 0;

    for (
        let i = round - window;
        i < round;
        i++
    ) {
        if (history[i].opponent === "C") {
            recentC++;
        } else {
            recentD++;
        }
    }

    const cooperationRate =
        recentC / window;

    const defectionRate =
        recentD / window;

    if (defectionRate >= 0.8) {
        memory.mode = "aggressive";
        memory.farming = false;
        return ["D", memory];
    }

    if (memory.farming) {
        if (opp === "D") {
            memory.farming = false;
            memory.mode = "reactive";
            memory.punish = 1;

            return ["D", memory];
        }

        memory.farmRounds++;

        return ["D", memory];
    }

    if (
        round >= 12 &&
        round <= 30 &&
        memory.probes < 2 &&
        cooperationRate >= 0.85
    ) {
        memory.probes++;

        return ["D", memory];
    }

    if (
        memory.probes > 0 &&
        opp === "C" &&
        mine === "D"
    ) {
        memory.farming = true;
        memory.mode = "farmer";

        return ["D", memory];
    }

    if (cooperationRate >= 0.75) {
        if (opp === "D") {
            memory.punish = 1;
            return ["D", memory];
        }

        if (memory.punish > 0) {
            memory.punish--;
        }

        return ["C", memory];
    }

    if (memory.mode === "tft") {
        if (
            opp === "D" &&
            round >= 4
        ) {
            memory.punish = 1;
            return ["D", memory];
        }

        if (
            memory.punish > 0 &&
            opp === "C"
        ) {
            memory.punish--;
            return ["C", memory];
        }

        return [opp, memory];
    }

    if (memory.mode === "reverse") {
        if (opp === "C") {
            return ["D", memory];
        }

        return ["C", memory];
    }

    if (memory.mode === "alternating") {
        if (opp === "C") {
            return ["D", memory];
        }

        return ["C", memory];
    }

    if (round >= 4) {
        const previous = history[round - 2];

        if (
            previous.you === "D" &&
            previous.opponent === "D" &&
            mine === "D" &&
            opp === "D"
        ) {
            if (cooperationRate >= 0.4) {
                memory.punish = 0;
                return ["C", memory];
            }
        }
    }

    if (defectionRate >= 0.45) {
        if (opp === "D") {
            memory.punish = 1;
            return ["D", memory];
        }

        if (memory.punish > 0) {
            memory.punish--;
            return ["C", memory];
        }

        return ["D", memory];
    }

    if (opp === "D") {
        memory.punish = 1;
        return ["D", memory];
    }

    return ["C", memory];
}