【gpt实践】同时让chatgpt和claude开发俄罗斯方块

最近chatgpt和claude都在使用,其实大部分日常使用场景表现都没有相差太多,想搞一个有趣的小实验,如果同时让chatgpt和claude开发俄罗斯方块谁会表现的更好呢,说干就干!

prompt

我选择了用英文描述,毕竟英文数据库更丰富,而且claude封禁比较严格,语言上也是小心翼翼

prompt:I have few development experience, and I want to develope the game 'Tetris' totally basing on html5, css and javascript. I have a few requests: 1, we need a start button, 2, a scoreboard recording score 3, pop up gameover 4, full functionality of a complete Tetris game, their movement, collision detection, scoring, and clearing lines give me all the codes, that can run the game in the browse.

我的开发经验不多,我想开发一款完全基于 html5、css 和 javascript 的 "俄罗斯方块 "游戏。我有几个要求: 1、我们需要一个开始按钮;2、一个记录得分的记分板;3、弹出式游戏结束界面;4、一个完整俄罗斯方块游戏的全部功能,包括移动、碰撞检测、得分和清线。

chatgpt回答

// Define variables and constants
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const startButton = document.getElementById('startButton');
const scoreboard = document.getElementById('score');
const gameOver = document.getElementById('gameOver');
let score = 0;
let gameInterval;
let gameOverFlag = false;

// Define Tetris pieces and their colors
const pieces = [
  [[1, 1, 1, 1]], // I
  [[1, 1, 1], [0, 1, 0]], // T
  [[1, 1, 0], [0, 1, 1]], // Z
  [[0, 1, 1], [1, 1, 0]], // S
  [[1, 1, 1], [0, 1, 0]], // L
  [[1, 1, 1], [1, 0, 0]], // J
  [[1, 1], [1, 1]], // O
];

const colors = ['#00FFFF', '#FF00FF', '#FFA500', '#FFFF00', '#00FF00', '#0000FF', '#FF0000'];

// Define functions for game logic (e.g., draw, move, rotate, check collision, etc.)

// Implement game logic

// Implement startGame function

// Implement gameOver function

// Implement key event listeners for controlling the game (e.g., arrow keys for movement)

// Implement the main game loop

// Implement functions for clearing lines, updating score, etc.

// Implement collision detection and game over check

// Implement rendering functions (e.g., drawPiece, drawBoard, etc.)

// Implement functions for moving and rotating pieces

// Implement functions for handling user input (e.g., start game, pause, resume, etc.)

// Start the game when the start button is clicked
startButton.addEventListener('click', startGame);

考虑篇幅关系,忽略了html和css部分截图,文章最后会给出源码!!!

可以看出chatgpt的做了一些方法定义,但是并没有实现。

claude回答
const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('score');
const startButton = document.getElementById('start-button');
const gameOverMessage = document.getElementById('game-over-message');

// Game constants
const ROWS = 20;
const COLS = 10;
const BLOCK_SIZE = 20;
const LINE_CLEAR_POINTS = 100;

// Game state
let board = [];
let piece = null;
let isGameOver = false;
let isPlaying = false;
let score = 0;

// Tetromino shapes
const shapes = [
  [
    [0, 0, 0, 0],
    [1, 1, 1, 1],
    [0, 0, 0, 0],
    [0, 0, 0, 0]
  ],
  [
    [1, 0, 0],
    [1, 1, 1],
    [0, 0, 0]
  ],
  [
    [0, 0, 1],
    [1, 1, 1],
    [0, 0, 0]
  ],
  [
    [1, 1, 0],
    [0, 1, 1],
    [0, 0, 0]
  ],
  [
    [0, 1, 1],
    [1, 1, 0],
    [0, 0, 0]
  ],
  [
    [1, 1],
    [1, 1]
  ],
  [
    [0, 1, 0],
    [1, 1, 1],
    [0, 0, 0]
  ]
];

// Helper functions
function drawBoard() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for (let row = 0; row < ROWS; row++) {
    for (let col = 0; col < COLS; col++) {
      if (board[row][col]) {
        ctx.fillStyle = 'yellow';
        ctx.fillRect(col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
      }
    }
  }

  if (piece) {
    drawPiece(piece.shape, piece.x, piece.y);
  }
}

function drawPiece(shape, x, y) {
  ctx.fillStyle = 'red';
  for (let row = 0; row < shape.length; row++) {
    for (let col = 0; col < shape[row].length; col++) {
      if (shape[row][col]) {
        ctx.fillRect((x + col) * BLOCK_SIZE, (y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
      }
    }
  }
}

function resetBoard() {
  board = Array.from({ length: ROWS }, () => Array(COLS).fill(0));
}

function randomPiece() {
  const randomIndex = Math.floor(Math.random() * shapes.length);
  return {
    shape: shapes[randomIndex],
    x: 3,
    y: 0
  };
}

function movePiece(direction) {
  if (!piece) return;

  const newX = piece.x + (direction === 'left' ? -1 : direction === 'right' ? 1 : 0);
  const newY = piece.y + (direction === 'down' ? 1 : 0);

  if (canMove(piece.shape, newX, newY)) {
    piece.x = newX;
    piece.y = newY;
  } else if (direction === 'down') {
    placePiece();
    clearLines();
    if (!isGameOver) {
      piece = randomPiece();
    }
  }

  drawBoard();
}

function canMove(shape, x, y) {
  for (let row = 0; row < shape.length; row++) {
    for (let col = 0; col < shape[row].length; col++) {
      if (shape[row][col]) {
        const newX = x + col;
        const newY = y + row;

        if (
          newX < 0 ||
          newX >= COLS ||
          newY >= ROWS ||
          (newY >= 0 && board[newY][newX])
        ) {
          return false;
        }
      }
    }
  }
  return true;
}

function placePiece() {
  if (!piece) return;

  for (let row = 0; row < piece.shape.length; row++) {
    for (let col = 0; col < piece.shape[row].length; col++) {
      if (piece.shape[row][col]) {
        board[piece.y + row][piece.x + col] = 1;
      }
    }
  }
}

function clearLines() {
  let linesCleared = 0;
  for (let row = ROWS - 1; row >= 0; row--) {
    let isFull = true;
    for (let col = 0; col < COLS; col++) {
      if (!board[row][col]) {
        isFull = false;
        break;
      }
    }

    if (isFull) {
      linesCleared++;
      for (let r = row; r > 0; r--) {
        for (let c = 0; c < COLS; c++) {
          board[r][c] = board[r - 1][c];
        }
      }
      for (let c = 0; c < COLS; c++) {
        board[0][c] = 0;
      }
    }
  }

  score += linesCleared * LINE_CLEAR_POINTS;
  scoreDisplay.textContent = `Score: ${score}`;
}

function rotatePiece() {
  if (!piece) return;

  const newShape = Array.from({ length: piece.shape[0].length }, () =>
    Array(piece.shape.length).fill(0)
  );

  for (let row = 0; row < piece.shape.length; row++) {
    for (let col = 0; col < piece.shape[row].length; col++) {
      newShape[col][piece.shape.length - 1 - row] = piece.shape[row][col];
    }
  }

  if (canMove(newShape, piece.x, piece.y)) {
    piece.shape = newShape;
  }

  drawBoard();
}

function isGameOverCheck() {
  for (let col = 0; col < COLS; col++) {
    if (board[0][col]) {
      isGameOver = true;
      gameOverMessage.classList.remove('hidden');
      break;
    }
  }
}

// Event listeners
document.addEventListener('keydown', (event) => {
  if (!isPlaying) return;

  switch (event.code) {
    case 'ArrowLeft':
      movePiece('left');
      break;
    case 'ArrowRight':
      movePiece('right');
      break;
    case 'ArrowDown':
      movePiece('down');
      break;
    case 'ArrowUp':
      rotatePiece();
      break;
  }
});

startButton.addEventListener('click', () => {
  if (isPlaying) {
    isPlaying = false;
    startButton.textContent = 'Start';
    gameOverMessage.classList.add('hidden');
  } else {
    isPlaying = true;
    startButton.textContent = 'Pause';
    isGameOver = false;
    score = 0;
    scoreDisplay.textContent = `Score: ${score}`;
    resetBoard();
    piece = randomPiece();
    drawBoard();
  }
});

// Game loop
function gameLoop() {
  if (!isPlaying) return;

  movePiece('down');
  isGameOverCheck();

  if (!isGameOver) {
    requestAnimationFrame(gameLoop);
  }
}

// Start the game
resetBoard();
piece = randomPiece();
drawBoard();
gameLoop();

同样只贴出js部分的代码。可以看到claude给出了相对完善的js逻辑,所以我们直接选择用claude回答的代码进行调试,看看是否能正常在浏览器中运行。

贴出代码:

浏览器打开tetris运行效果:

可以看到,点击开始后方块不会自动下落,需要手动按下,继续反馈优化:

反馈完善功能

prompt:I mean when the game starts without pressing the down button, the cube falls at a constant speed

即当游戏开始时,方块保持匀速下落。

const canvas = document.getElementById('game-canvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('score');
const startButton = document.getElementById('start-button');
const gameOverMessage = document.getElementById('game-over-message');

// Game constants
const ROWS = 20;
const COLS = 10;
const BLOCK_SIZE = 20;
const LINE_CLEAR_POINTS = 100;
const FALL_SPEED = 500; // Adjust this value to control the falling speed (in milliseconds)

// Game state
let board = [];
let piece = null;
let isGameOver = false;
let isPlaying = false;
let score = 0;
let lastTime = 0;
let fallInterval = null;

// Tetromino shapes
// ... (shapes array remains the same)

// Helper functions
// ... (drawBoard, drawPiece, resetBoard, randomPiece, movePiece, canMove, placePiece, clearLines, rotatePiece, isGameOverCheck functions remain the same)

// Event listeners
document.addEventListener('keydown', (event) => {
  if (!isPlaying) return;

  switch (event.code) {
    case 'ArrowLeft':
      movePiece('left');
      break;
    case 'ArrowRight':
      movePiece('right');
      break;
    case 'ArrowUp':
      rotatePiece();
      break;
  }
});

startButton.addEventListener('click', () => {
  if (isPlaying) {
    isPlaying = false;
    startButton.textContent = 'Start';
    gameOverMessage.classList.add('hidden');
    clearInterval(fallInterval); // Stop the automatic falling
  } else {
    isPlaying = true;
    startButton.textContent = 'Pause';
    isGameOver = false;
    score = 0;
    scoreDisplay.textContent = `Score: ${score}`;
    resetBoard();
    piece = randomPiece();
    drawBoard();
    lastTime = performance.now(); // Get the current time
    fallInterval = setInterval(gameLoop, FALL_SPEED); // Start the automatic falling
  }
});

// Game loop
function gameLoop() {
  if (!isPlaying) return;

  const currentTime = performance.now();
  const deltaTime = currentTime - lastTime;

  if (deltaTime >= FALL_SPEED) {
    movePiece('down');
    isGameOverCheck();
    lastTime = currentTime;
  }

  if (!isGameOver) {
    requestAnimationFrame(gameLoop);
  } else {
    clearInterval(fallInterval); // Stop the automatic falling when the game is over
  }
}

// Start the game
resetBoard();
piece = randomPiece();
drawBoard();

可以看到claude会的的js代码里增加了方块下落的逻辑。点击开始后方块就能匀速下落

当然后面还有几个小问题,比如按下键没有加速下落,方块无法移动到最右端,这些都通过反馈cluade得到了解决,最终能正常运行记分,效果如下:

源码及claude导出文件

https://github.com/JiangSanJie/gpt-practice/

总结:

claude稳定账号获取

在本次俄罗斯方块的开发prompt中,claude回答相对完善,通过少量的反馈即可完成开发。当然其实chatgpt也能完成,但是需要prompt更加细致,可能针对回答的反馈会繁琐一些,这个感兴趣的读者可以去实验。我相信好的prompt会提高回答满意率!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/452837.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

导入空管基础数据

1、首先将data.tar.gz解压到自定义目录中 注意&#xff1a;由于数据文件的压缩包比较大&#xff0c;解压过程可能会持续3~5分钟&#xff0c;请耐心等待。 [rootnode3 ~]# cd /opt/software/ [rootnode3 software]# tar -xzf data.tar.gz -C /opt/ 2、利用SQLyog或者其他数据库…

LeetCode:链表相交

题目描述 给你两个单链表的头节点 headA 和 headB &#xff0c;请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点&#xff0c;返回 null 。 示例 解题思想 将两个链表从尾部对齐&#xff0c;然后进行寻找。 此时我们就可以比较curA和curB是否相同&#xff0c…

Pytorch基础(21)-- torch.repeat_interleave()方法

分享一下自己目前在维护的Github项目&#xff0c;由于本人博士阶段接触了一个全新的研究方向-----使用机器学习、强化学习、深度学习等方法解决组合优化问题&#xff0c;维护这个项目的目的&#xff1a; &#xff08;1&#xff09;记录自己阅读过的paper&#xff0c;同时分享一…

G. Rudolf and Subway

解题思路 每条边的边权可选&#xff0c;由颜色决定同一颜色的线路可以直达颜色最多有种考虑将颜色视作链接点&#xff0c;进行分层图跑最短路最终结果除2最多建条边&#xff08;直接存状态Map跑最短路被毙掉了&#xff09; import java.io.*; import java.math.BigInteger; im…

【Python】新手入门学习:详细介绍接口分隔原则(ISP)及其作用、代码示例

【Python】新手入门学习&#xff1a;详细介绍接口分隔原则&#xff08;ISP&#xff09;及其作用、代码示例 &#x1f308; 个人主页&#xff1a;高斯小哥 &#x1f525; 高质量专栏&#xff1a;Matplotlib之旅&#xff1a;零基础精通数据可视化、Python基础【高质量合集】、Py…

Postman请求API接口测试步骤和说明

Postman请求API接口测试步骤 本文测试的接口是国内数智客&#xff08;www.shuzike.com&#xff09;的API接口手机三要素验证&#xff0c;验证个人的姓名&#xff0c;身份证号码&#xff0c;手机号码是否一致。 1、设置接口的Headers参数。 Content-Type&#xff1a;applicati…

三防手机与普通手机的区别在哪里?

一、三防手机和普通手机主要的区别在于其防护性能&#xff0c;它们是针对不同环境和使用场景设计的。三防手机一般包括防水、防尘和防摔三个方面的特点&#xff0c;而普通手机则往往没有这些特点。 防水性能。三防手机的防水性能通常比普通手机更强大。三防手机可以在一定程度上…

【XR806开发板试用】简单移植coremark并测试实际跑分

前言 首先&#xff0c;拿到板子的时候&#xff0c;由于xr806它是个IOT的片子&#xff0c;所以自然而然必然&#xff0c;在预想里就会有很多大佬会把文章出发点考虑在wifi/bt&#xff0c;各种物联网/WEB应用上。那么怎么另辟蹊径回避这个热点又体现出XR806的特色呢&#xff0c;…

一款好用的AI工具——边界AICHAT(三)

目录 3.23、文档生成PPT演示3.24、AI文档翻译3.25、AI翻译3.26、论文模式3.27、文章批改3.28、文章纠正3.29、写作助手3.30、文言文翻译3.31、日报周报月报生成器3.32、OCR-DOC办公文档识别3.33、AI真人语音合成3.34、录音音频总结3.35、域方模型市场3.36、模型创建3.37、社区交…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的条形码二维码检测系统(深度学习+UI界面+训练数据集+Python代码)

摘要&#xff1a;在物流和制造业中&#xff0c;开发一套高效的条形码与二维码识别系统显得尤为关键。本博文深入探讨了如何利用深度学习技术打造出一套先进的条形码及二维码检测系统&#xff0c;并且提供了一套完整的实施方案。该系统搭载了性能卓越的YOLOv8算法&#xff0c;并…

[LeetCode][LCR 175]计算二叉树的深度

题目 计算二叉树的深度 某公司架构以二叉树形式记录&#xff0c;请返回该公司的层级数。 示例 1&#xff1a; 输入&#xff1a;root [1, 2, 2, 3, null, null, 5, 4, null, null, 4] 输出&#xff1a;4 解释&#xff1a; 上面示例中的二叉树的最大深度是 4&#xff0c;沿…

【BI-Dataease】dataease设计思路

参考&#xff1a;https://juejin.cn/post/7089310768671227940 1、BI可视化工具的关键问题是什么&#xff1f; &#xff08;1&#xff09;各种数据源的数据结构和类型如何统一&#xff1f; &#xff08;2&#xff09;各种图表的配置属性不一致&#xff0c;属性如何绑定到数据…

【C++庖丁解牛】实现string容器的增删查改 | string容器的基本接口使用

&#x1f341;你好&#xff0c;我是 RO-BERRY &#x1f4d7; 致力于C、C、数据结构、TCP/IP、数据库等等一系列知识 &#x1f384;感谢你的陪伴与支持 &#xff0c;故事既有了开头&#xff0c;就要画上一个完美的句号&#xff0c;让我们一起加油 目录 前言&#x1f4d6;push_b…

关于stm32(CubeMX+HAL库)的掉电检测以及flash读写

1.掉电检测 CubeMX配置 只需使能PVD中断即可 但是使能了PVD中断后还需要自行配置一些PWR寄存器中的参数&#xff0c;我也通过HAL库进行编写 void PVD_config(void) {//配置PWRPWR_PVDTypeDef sConfigPVD; sConfigPVD.PVDLevel PWR_PVDLEVEL_7; …

蓝桥杯2023年第十四届省赛真题-与或异或

一共10个电门&#xff0c;穷举310次 #include<stdio.h> #include<math.h> int main(){int a[5][5], max (int)pow(3, 10), t, op, count 0;a[0][0] a[0][2] a[0][4] 1;a[0][1] a[0][3] 0;for(int k 0; k < max; k){t k;for(int i 1; i < 5; i){fo…

vite ssr服务端渲染

阅读 Vue文档 这一章里有说过&#xff0c;vue是支持服务端渲染的。 通过createSSRApp创建vue组件实例&#xff0c;并使用renderToString将在服务器渲染好template并返回字符串结构&#xff0c;通过替换占位字符将渲染好的字符串输出到html上&#xff0c;这样的一个过程就实现了…

发送短信验证码

​​​​​​【短信验证码-快速报备签名】三网短信接口-短信-短信验证码-短信服务-三网短信接口-短信-三网短信【最新版】_商业智能_电商_金融-云市场-阿里云阿里云云市场提供 专注企业短信服务10年运营与技术积累&#xff0c;稳定、安全、快速。服务&#xff0c;建站服务&…

【word技巧】word文件如何设置打开密码?可以试试这两种方法!

Word文件想要设置密码打开文件&#xff0c;我们可以给文件设置一个打开密码&#xff0c;这样只有知道密码的人才能够打开查看文件&#xff0c;今天分享两个word文件设置打开密码的方法。 一、 打开word文档后&#xff0c;点击【文件】-【信息】-【保护文档】这里有很多选项&a…

excel统计分析——一元直线回归

参考资料&#xff1a;生物统计学 两个具有因果关系的协变量如果呈直线关系&#xff0c;可以用直线回归模型来分析两个变量的关系。直线回归&#xff08;linear regression&#xff09;是回归分析中最简单的类型&#xff0c;建立直线回归方程并经检验证明两个变量存在直线回归关…

Altium Designer怎么设置默认原理图纸张大小

Altium Designer怎么设置默认原理图纸张大小 绘制原理图时我们需要设置好原理图图纸大小&#xff0c;建议大家可以将默认原理图图纸设置为A3&#xff0c;A3图纸大小可以容纳下大部分原理图&#xff0c;这样就不用每次画原理图前去修改图纸大小&#xff0c;可以提高设计效率。 …
最新文章