gemini-2.0-flash-001· Google LMArena Human Preference 140K
开发一款关于“囚徒困境”的博弈论游戏,在单个HTML文件里面展示。
- User
开发一款关于“囚徒困境”的博弈论游戏,在单个HTML文件里面展示。
- Assistant
好的,这是一个包含“囚徒困境”博弈论游戏的单个HTML文件示例。这个例子包含简单的HTML结构,CSS样式和JavaScript逻辑,允许两个玩家(或一个玩家对抗电脑)做出选择,并显示结果和累计分数。
<!DOCTYPE html> <html> <head> <title>囚徒困境</title> <style> body { font-family: sans-serif; text-align: center; } .container { width: 600px; margin: 0 auto; } .choice-button { padding: 10px 20px; font-size: 16px; cursor: pointer; margin: 10px; } #result { margin-top: 20px; font-weight: bold; } </style> </head> <body> <div class="container"> <h1>囚徒困境</h1> <p>选择合作(C)或背叛(D)。</p> <div> <h2>你 (玩家 1)</h2> <button class="choice-button" onclick="makeChoice('C')">合作 (C)</button> <button class="choice-button" onclick="makeChoice('D')">背叛 (D)</button> </div> <div> <h2>电脑 (玩家 2)</h2> <p id="computerChoice">等待你的选择...</p> </div> <div id="result"></div> <div id="score"> 你: <span id="playerScore">0</span> | 电脑: <span id="computerScore">0</span> </div> </div> <script> let playerScore = 0; let computerScore = 0; let round = 0; function makeChoice(playerChoice) { round++; const computerChoice = getComputerChoice(); document.getElementById('computerChoice').textContent = '电脑选择了: ' + computerChoice; const result = determineOutcome(playerChoice, computerChoice); displayResult(result, playerChoice, computerChoice); updateScore(result); } function getComputerChoice() { // 电脑策略:随机选择 // 可以更改策略,例如:模仿对方,或总是背叛。 const choices = ['C', 'D']; const randomIndex = Math.floor(Math.random() * choices.length); return choices[randomIndex]; } function determineOutcome(playerChoice, computerChoice) { if (playerChoice === 'C' && computerChoice === 'C') { return '双方合作'; } else if (playerChoice === 'C' && computerChoice === 'D') { return '你合作,电脑背叛'; } else if (playerChoice === 'D' && computerChoice === 'C') { return '你背叛,电脑合作'; } else { return '双方背叛'; } } function displayResult(result, playerChoice, computerChoice) { let playerSentence = 0; let computerSentence = 0; if(result === '双方合作'){ playerSentence = 2; computerSentence = 2; }else if (result === '你合作,电脑背叛'){ playerSentence = 5; computerSentence = 0; }else if (result === '你背叛,电脑合作'){ playerSentence = 0; computerSentence = 5; } else { playerSentence = 3; computerSentence = 3; } document.getElementById('result').textContent = `结果:${result}. 你: ${playerSentence}年 | 电脑: ${computerSentence}年`; } function updateScore(result) { if (result === '双方合作') { playerScore += 2; computerScore += 2; } else if (result === '你合作,电脑背叛') { playerScore += 0; computerScore += 5; } else if (result === '你背叛,电脑合作') { playerScore += 5; computerScore += 0; } else { playerScore += 3; computerScore += 3; } document.getElementById('playerScore').textContent = playerScore; document.getElementById('computerScore').textContent = computerScore; } </script> </body> </html>关键点:
- HTML 结构: 包含标题、说明、选择按钮、结果显示区域和分数显示区域。
- CSS 样式: 提供基本的页面布局和样式。
- JavaScript 逻辑:
makeChoice(playerChoice): 玩家做出选择时调用。 它会生成电脑的选择,确定结果,显示结果,并更新分数。getComputerChoice(): 模拟电脑的选择。 现在是随机选择,你可以修改它来实现不同的电脑策略。determineOutcome(playerChoice, computerChoice): 根据玩家和电脑的选择,确定结果。 这里实现了囚徒困境的支付矩阵。displayResult(result, playerChoice, computerChoice): 将结果显示在页面上。updateScore(result): 更新玩家和电脑的分数。
如何运行:
- 将代码复制到文本编辑器中。
- 保存为
index.html(或其他你喜欢的名字,但要确保文件扩展名为.html)。 - 在浏览器中打开
index.html文件。
改进和扩展:
- 电脑策略: 实现更复杂的电脑策略,例如:
- TFT (Tit-for-Tat): 第一次合作,之后模仿对手上次的选择。
- 总是背叛: 电脑总是选择背叛。
- 随机策略: 以一定的概率选择合作或背叛。
- 多轮游戏: 让游戏进行多轮,并显示每轮的结果和累计分数。
- 图形界面: 使用更丰富的 HTML 和 CSS 来改善游戏界面。
- 多人游戏: 允许两个玩家通过网络进行游戏 (这需要服务器端代码)。
- 策略可视化: 使用图表或其他方式来可视化玩家和电脑的策略和分数。
- 解释: 添加更详细的解释,说明囚徒困境的原理和不同策略的优缺点。
- 支付矩阵自定义: 允许用户自定义支付矩阵,从而创建不同版本的囚徒困境。
这个示例为你提供了一个开始,你可以根据自己的需要进行修改和扩展。 希望它能帮助你理解和探索囚徒困境!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.