Back to leaderboard

big ULTIMATE guy

Created 9/12/2026

Active

ULTIMATE

Avg score

8.170

Wins

2908

Losses

3958

Win rate

26.2%

Total battles: 11099

Score history

Score trend over the last 199 battles

Avg: 8.170

Recent battles

Source

const RESPONSE_WINDOW_MIN = 10;          // floor for the dynamic response-rate window
const RESPONSE_WINDOW_FRACTION = 0.3;    // window grows with match length
const COOPERATION_THRESHOLD = 0.6;       // pC above this => opponent is "TFT-like", play nice
const EXPLOIT_THRESHOLD = 0.6;           // pD above this => opponent doesn't punish defection
const MIN_DEFECT_RESPONSE_SAMPLES = 3;
const OLIVE_BRANCH_SCHEDULE = [2, 5, 10, 20]; // escalating mutual-D streak lengths to offer peace
const MAX_FAILED_OLIVES = 2;             // stop offering peace after this many rejections
const GRIM_LOCK_MIN_ROUNDS = 4;          // rounds since first D to confirm a true grim trigger
const FORGIVE_BASE_PROB = 0.12;          // chance to forgive an isolated, unprovoked defection
const PROBE_MIN_ROUND = 8;               // earliest round to test a pristine cooperator
const PROBE_COOLDOWN = 6;                // rounds between exploit probes
const PROBE_CONFIRM_SAMPLES = 3;         // probes needed before trusting "won't retaliate"
const EXPLOIT_RETALIATION_THRESHOLD = 0.75; // opp coop-rate-after-our-D must stay >= this to keep farming
 
function freshMemory() {
	return {
		seen: 0,
		opponentC: 0,
		opponentD: 0,
		mutualDStreak: 0,
		olives: 0,
		failedOlives: 0,
		pendingOliveRound: -1,
		// live exploit-probe bookkeeping (replaces hardcoded population fingerprints)
		probesSent: 0,
		probesForgiven: 0,
		lastProbeAt: -999,
		exploiting: false,
		lastMoveWasProbe: false,
	};
}
 
function rebuildMemory(history, previous) {
	const memory = freshMemory();
	memory.olives = previous && Number.isFinite(previous.olives) ? Math.max(0, Math.floor(previous.olives)) : 0;
	memory.failedOlives = previous && Number.isFinite(previous.failedOlives) ? Math.max(0, Math.floor(previous.failedOlives)) : 0;
	memory.pendingOliveRound = previous && Number.isFinite(previous.pendingOliveRound) ? previous.pendingOliveRound : -1;
	memory.probesSent = previous && Number.isFinite(previous.probesSent) ? previous.probesSent : 0;
	memory.probesForgiven = previous && Number.isFinite(previous.probesForgiven) ? previous.probesForgiven : 0;
	memory.exploiting = previous ? Boolean(previous.exploiting) : false;
 
	for (let i = 0; i < history.length; i++) {
		const round = history[i];
		if (round.opponent === "D") memory.opponentD++; else memory.opponentC++;
		if (round.you === "D" && round.opponent === "D") memory.mutualDStreak++;
		else memory.mutualDStreak = 0;
	}
	memory.seen = history.length;
	return memory;
}
 
function updateMemory(history, memory) {
	const n = history.length;
	if (
		!memory || typeof memory !== "object" || memory.seen !== n - 1 ||
		!Number.isFinite(memory.opponentC) || !Number.isFinite(memory.opponentD) ||
		!Number.isFinite(memory.mutualDStreak) || !Number.isFinite(memory.olives) ||
		!Number.isFinite(memory.failedOlives)
	) {
		return rebuildMemory(history, memory);
	}
 
	const last = history[n - 1];
	if (last.opponent === "D") memory.opponentD++; else memory.opponentC++;
	if (last.you === "D" && last.opponent === "D") memory.mutualDStreak++;
	else memory.mutualDStreak = 0;
 
	if (!Number.isFinite(memory.pendingOliveRound)) memory.pendingOliveRound = -1;
	if (!Number.isFinite(memory.probesSent)) memory.probesSent = 0;
	if (!Number.isFinite(memory.probesForgiven)) memory.probesForgiven = 0;
	if (typeof memory.exploiting !== "boolean") memory.exploiting = false;
 
	// Resolve any pending olive branch: did the peace offer get met with more D?
	if (memory.pendingOliveRound === n - 1) {
		if (last.opponent === "D") memory.failedOlives++;
		memory.pendingOliveRound = -1;
	}
 
	// Resolve any pending exploit probe: did an unprovoked D of ours get forgiven?
	if (memory.lastMoveWasProbe) {
		if (last.opponent === "C") memory.probesForgiven++;
		memory.lastMoveWasProbe = false;
	}
 
	memory.seen = n;
	return memory;
}
 
function responseRates(history) {
	let coopAfterC = 0, samplesAfterC = 0;
	let coopAfterD = 0, samplesAfterD = 0;
	const window = Math.max(RESPONSE_WINDOW_MIN, Math.floor(history.length * RESPONSE_WINDOW_FRACTION));
	const start = Math.max(1, history.length - window);
 
	for (let i = start; i < history.length; i++) {
		const weMoved = history[i - 1].you;
		const theyMoved = history[i].opponent;
		if (weMoved === "C") {
			samplesAfterC++;
			if (theyMoved === "C") coopAfterC++;
		} else {
			samplesAfterD++;
			if (theyMoved === "C") coopAfterD++;
		}
	}
 
	return {
		// Laplace smoothing avoids overreacting to tiny samples early on.
		pC: (coopAfterC + 0.5) / (samplesAfterC + 1),
		pD: (coopAfterD + 0.5) / (samplesAfterD + 1),
		samplesAfterD,
	};
}
 
// Catches rigid periodic opponents (e.g. C,C,D,C,C,D,...) that are NOT simply
// mirroring our own moves. Checked before any other recognition so it can
// never be shadowed by a partial pattern match elsewhere.
function isUnconditionalPeriodic(history) {
	const n = history.length;
	if (n < 15) return false;
	const start = Math.max(0, n - 30);
	let sawC = false, sawD = false;
	for (let i = start; i < n; i++) {
		if (history[i].opponent === "C") sawC = true; else sawD = true;
	}
	if (!sawC || !sawD) return false;
 
	for (let period = 2; period <= 12; period++) {
		let matches = true, comparisons = 0;
		for (let i = start + period; i < n; i++) {
			comparisons++;
			if (history[i].opponent !== history[i - period].opponent) { matches = false; break; }
		}
		if (!matches || comparisons < 8) continue;
 
		let explainedByTFT = true;
		for (let i = Math.max(1, start); i < n; i++) {
			if (history[i].opponent !== history[i - 1].you) { explainedByTFT = false; break; }
		}
		if (!explainedByTFT) return true;
	}
	return false;
}
 
// True unforgiving trigger: opponent has defected on literally every round
// since (and including) its first-ever defection. Once confirmed, further
// cooperation just donates points for nothing — settle for mutual D's 1pt.
function isConfirmedGrimLock(history, minRoundsSinceTrigger) {
	const n = history.length;
	let firstD = -1;
	for (let i = 0; i < n; i++) {
		if (history[i].opponent === "D") { firstD = i; break; }
	}
	if (firstD === -1) return false;
	if (n - firstD < minRoundsSinceTrigger) return false;
	for (let i = firstD; i < n; i++) {
		if (history[i].opponent === "C") return false;
	}
	return true;
}
 
function decide(history, memory) {
	const n = history.length;
	if (n === 0) return ["C", freshMemory()];
 
	memory = updateMemory(history, memory);
	const last = history[n - 1];
	const previous = n >= 2 ? history[n - 2] : null;
 
	// --- 1. Never keep feeding a confirmed grim/AllD opponent. ---
	if (isConfirmedGrimLock(history, GRIM_LOCK_MIN_ROUNDS)) {
		memory.exploiting = false;
		return ["D", memory];
	}
 
	// --- 2. Pristine cooperation: earn trust first, then live-test for exploitability. ---
	if (memory.opponentD === 0) {
		if (memory.exploiting) {
			// Confirmed non-retaliator: keep farming 3-point rounds, but stay
			// alert — if they ever start punishing our D's, stop immediately.
			const { pD, samplesAfterD } = responseRates(history);
			if (samplesAfterD >= PROBE_CONFIRM_SAMPLES && pD < EXPLOIT_RETALIATION_THRESHOLD) {
				memory.exploiting = false;
				return ["C", memory]; // one guaranteed cooperation to repair
			}
			memory.lastMoveWasProbe = true;
			return ["D", memory];
		}
 
		const sinceLastProbe = n - memory.lastProbeAt;
		const readyToProbe = n >= PROBE_MIN_ROUND && sinceLastProbe >= PROBE_COOLDOWN;
		if (readyToProbe) {
			memory.lastProbeAt = n;
			memory.probesSent++;
			memory.lastMoveWasProbe = true;
			if (memory.probesSent >= PROBE_CONFIRM_SAMPLES && memory.probesForgiven >= PROBE_CONFIRM_SAMPLES - 1) {
				memory.exploiting = true;
			}
			return ["D", memory];
		}
		return ["C", memory];
	}
 
	// --- 3. Rigid periodic defectors: punish, don't get farmed by them. ---
	if (isUnconditionalPeriodic(history)) return ["D", memory];
 
	const { pC, pD, samplesAfterD } = responseRates(history);
 
	// --- 4. Opponent doesn't punish our defections: exploit. ---
	if (samplesAfterD >= MIN_DEFECT_RESPONSE_SAMPLES && pD >= EXPLOIT_THRESHOLD) {
		return ["D", memory];
	}
 
	// --- 5. Generous Tit-for-Tat core. ---
	if (pC >= COOPERATION_THRESHOLD) {
		const theirDWasProvoked = last.opponent === "D" && previous !== null && previous.you === "D";
		if (last.opponent === "D" && !theirDWasProvoked) {
			// Isolated defection from an otherwise reciprocal opponent: forgive
			// with some probability instead of always retaliating (avoids
			// permanent echo-lock with noisy/probabilistic opponents).
			const oppCoopShare = memory.opponentC / Math.max(1, memory.opponentC + memory.opponentD);
			const forgiveProb = FORGIVE_BASE_PROB * Math.max(0.2, oppCoopShare);
			if (Math.random() < forgiveProb) return ["C", memory];
			return ["D", memory];
		}
		return ["C", memory];
	}
 
	// --- 6. Olive branch: try to break a mutual-defection deadlock, but stop
	// offering peace to an opponent who has already proven it doesn't work. ---
	if (
		memory.failedOlives < MAX_FAILED_OLIVES &&
		memory.opponentC > 0 &&
		memory.olives < OLIVE_BRANCH_SCHEDULE.length &&
		memory.mutualDStreak >= OLIVE_BRANCH_SCHEDULE[memory.olives]
	) {
		memory.olives++;
		memory.pendingOliveRound = n;
		return ["C", memory];
	}
 
	return ["D", memory];
}
 
export default function ultimateBot(state) {
	const memory = state && state.memory && typeof state.memory === "object" ? state.memory : null;
	try {
		const rawHistory = state && Array.isArray(state.history) ? state.history : [];
 
		// Defensive validation: a malformed entry must never throw mid-decision
		// (an uncaught error typically forfeits the match).
		for (let i = 0; i < rawHistory.length; i++) {
			const r = rawHistory[i];
			if (!r || (r.you !== "C" && r.you !== "D") || (r.opponent !== "C" && r.opponent !== "D")) {
				return ["D", memory || freshMemory()];
			}
		}
 
		const [move, nextMemory] = decide(rawHistory, memory);
		return [move === "D" ? "D" : "C", nextMemory];
	} catch {
		return ["D", memory || freshMemory()];
	}
}