js实现调用chatgpt等ai大模型api实现五子棋ai人机大战代码
代码语言:html
所属分类:游戏
代码描述:js实现调用chatgpt等ai大模型api实现五子棋ai人机大战代码,编写好的提示词,让ai也能理解棋盘数据和布局,自动落子,看看你和ai大模型chatgpt谁更厉害。
代码标签: js 调用 chatgpt ai 大模型 api 五子棋 ai 人机 大战 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>五子棋 vs ChatGPT</title> <style> #board { display: grid; grid-template-columns: repeat(15, 30px); grid-gap: 1px; background-color: #deb887; padding: 10px; width: fit-content; margin: 0 auto; } .cell { width: 30px; height: 30px; background-color: #deb887; border: 1px solid #000; display: flex; justify-content: center; align-items: center; cursor: pointer; } .black { background-color: #000; border-radius: 50%; } .white { background-color: #fff; border-radius: 50%; } </style> </head> <body> <h1 style="text-align:center;">五子棋 vs ChatGPT</h1> <div id="board"></div> <p id="status"></p> <script> const board = document.getElementById('board'); const status = document.getElementById('status'); const size = 15; let gameBoard = Array(size).fill().map(() => Array(size).fill(0)); let currentPlayer = 1; // 1 for player, 2 for AI // Create the board for (let i = 0; i < size * size; i++) { const cell = document.createElement('div'); cell.classList.add('cell'); cell.addEventListener('click', () => makeMove(i)); board.appendChild(cell); } function makeMove(index) { if (currentPlayer !== 1) return; const row = Math.floor(index / size); const col = index % size; if (gameBoard[row][col] !== 0) return; gameBoard[row][col] = 1; updateBoard(); if (checkWin(row, col, 1)) { status.textContent = "你赢了!"; return; } currentPlayer = 2; status.textContent = "AI正在思考..."; setTimeout(aiMove, 1000); } async function aiMove() { try { const response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY_HERE' // 替换为你的API密钥 }, body: JSON.stringify({ model: "gpt-3.5-turbo", messages: [{ role: "system", .........完整代码请登录后点击上方下载按钮下载查看
网友评论0