Back to leaderboard

tie II

Created 9/11/2026

Active

tie

Avg score

5.559

Wins

8998

Losses

1656

Win rate

72.0%

Total battles: 12499

Score history

Score trend over the last 199 battles

Avg: 5.559

Recent battles

Source

export default function bot({ history, memory }) {
  // 1. Initialize DB with Win-Priority Logic
  if (memory === null) {
    memory = {
      db: {
        stats: { myScore: 0, oppScore: 0, scoreDiff: 0 },
        opponentProfile: { coopCount: 0, defectCount: 0, coopRate: 1.0 }
      }
    }
  }

  const roundCount = history.length

  // 2. Update Database Analytics
  if (roundCount > 0) {
    const lastMatch = history.at(-1)
    let myPts = 0, oppPts = 0

    if (lastMatch.you === "C" && lastMatch.opponent === "C") { myPts = 2; oppPts = 2 }
    else if (lastMatch.you === "D" && lastMatch.opponent === "C") { myPts = 3; oppPts = 0 }
    else if (lastMatch.you === "C" && lastMatch.opponent === "D") { myPts = 0; oppPts = 3 }
    else if (lastMatch.you === "D" && lastMatch.opponent === "D") { myPts = 1; oppPts = 1 }

    memory.db.stats.myScore += myPts
    memory.db.stats.oppScore += oppPts
    memory.db.stats.scoreDiff = memory.db.stats.myScore - memory.db.stats.oppScore

    if (lastMatch.opponent === "C") memory.db.opponentProfile.coopCount++
    if (lastMatch.opponent === "D") memory.db.opponentProfile.defectCount++
    memory.db.opponentProfile.coopRate = memory.db.opponentProfile.coopCount / roundCount
  }

  const diff = memory.db.stats.scoreDiff
  const oppCoopRate = memory.db.opponentProfile.coopRate

  // --- STRATEGY: TIE AT LEAST, BUT WIN WHEN POSSIBLE ---

  // 1. Trailing Behind (diff < 0): Defect ("D") immediately to recover points and tie
  if (diff < 0) {
    return ["D", memory]
  }

  // 2. Late Game / End Game Strike (Round 18+): Abandon tie and go for the win
  if (roundCount >= 18) {
    return ["D", memory]
  }

  // 3. Opportunistic Edge: If opponent is highly cooperative (coopRate > 70%), 
  // take a small +1 or +2 lead instead of forcing a strict tie
  if (diff >= 0 && diff < 2 && oppCoopRate > 0.7 && roundCount > 5) {
    return ["D", memory] // Betray to pull ahead into a win
  }

  // 4. Ahead by 2+ Points: Cooperate ("C") to let them close the gap (safety margin)
  if (diff >= 2) {
    return ["C", memory]
  }

  // 5. Tied (diff === 0): Default to Tit-For-Tat to hold the tie baseline
  return [history.at(-1)?.opponent ?? "C", memory]
}