【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 timefallInterval = 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.hqwc.cn/news/537597.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图像处理与图像分析—图像统计特性的计算(纯C语言实现灰度值显示)

根据输入的灰度图像&#xff0c;分别计算图像的均值、方差等统计特征&#xff0c;并计算图像的直方图特征并以图形方式显示图像的直方图&#xff08;用C或C语言实现&#xff09;。 学习将会依据教材图像处理与图像分析基础&#xff08;C/C&#xff09;版内容展开 在上个笔记中&…

windows批处理脚本(bat脚本指令)

一、常用批处理内部命令简介 批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件&#xff0c;其扩展名为BAT或者CMD。这些命令统称批处理命令。 可以在键盘上按下CtrC组合键来强行终止一个批处理的执行过程。 二、常用命令 1、rem 和 ∷ 2、echo 和 3、…

【Python】成功解决NameError: name ‘plt‘ is not defined

【Python】成功解决NameError: name ‘plt’ is not defined &#x1f308; 个人主页&#xff1a;高斯小哥 &#x1f525; 高质量专栏&#xff1a;Matplotlib之旅&#xff1a;零基础精通数据可视化、Python基础【高质量合集】、PyTorch零基础入门教程&#x1f448; 希望得到您…

Java基础_内部类

文章目录 1.基本介绍1.定义&#xff1a;2.基本语法&#xff1a;3.内部类的分类 2.局部内部类1.快速入门2.局部内部类的使用 3.匿名内部类1.匿名内部类实现接口2.匿名内部类继承抽象类3.匿名内部类细节4.匿名内部类最佳实践1.匿名内部类作为实参传入函数2.匿名内部类课堂练习 4.…

算法详解——图的深度优先遍历和广度优先遍历

目录 一、图结构二、深度优先遍历2.1 图的遍历2.2 深度优先遍历过程2.3 深度优先遍历核心思想2.4 深度优先遍历实现 三、广度优先遍历3.1 广度优先遍历过程3.2 广度优先遍历核心思想3.3 广度优先遍历实现 参考文献 一、图结构 图结构指的是如下图所示的由节点和边组成的数据。 …

最新CLion + STM32 + CubeMX 开发环境搭建

网上有不少相关教程&#xff0c;但都是基于老版本Clion&#xff0c;新版有一些改变&#xff0c;但整体是简单了。 PS&#xff1a;本教程基于CLion 2023.3.4 安装所需工具参考&#xff1a;Clion搭建stm32开发环境&#xff08;STM32F103C8T6&#xff09;&#xff0c;有这一篇就够…

【论文速读】| MOCK:上下文依赖引导的内核模糊测试

本次分享论文为&#xff1a;MOCK: Optimizing Kernel Fuzzing Mutation with Context-aware Dependency 基本信息 原文作者&#xff1a;Jiacheng Xu&#xff0c;Xuhong Zhang&#xff0c;Shouling Ji&#xff0c;Yuan Tian&#xff0c;Binbin Zhao&#xff0c; Qinying Wang&a…

ubuntu 23.04 安装 中文输入法

1、安装 fcitx sudo apt install fcitxfcitx 安装好后&#xff0c;可以使用 fcitx-configtool 命令进行配置&#xff0c;其界面如下所示。在这里可以配置不同输入法的切换快捷键&#xff0c;默认输入法等。刚安装系统后&#xff0c;这里只有一个输入法&#xff0c;所以接下来要…

如何建立自己的会员系统_打造专属会员系统

打造专属会员系统&#xff0c;尊享无限特权与惊喜 在如今信息爆炸的时代&#xff0c;无论是线上还是线下&#xff0c;各种服务、产品层出不穷&#xff0c;如何才能在激烈的市场竞争中脱颖而出&#xff0c;吸引并留住客户呢&#xff1f;答案就是——建立自己的会员系统。今天&a…

北大核心期刊《思想政治课教学》投稿有什么要求?

北核《思想政治课教学》收职教、中专、大学、中小学&#xff0c;职称没要求&#xff0c;学生可以 探寻智慧之源&#xff0c;倾听思想之声&#xff0c;欢迎投稿《思想政治课教学》。我们致力于搭建一个开放、前沿的学术交流平台&#xff0c;汇聚国内外思想政治课教学的最新研究成…

AP AUTOSAR 执行管理的秘密揭晓

本文主要分享执行管理和状态管理以及操作系统接口模块,这些功能集群是Adaptive AUTOSAR的核心部分。你们可能会问,什么是执行管理和状态管理?它们是不是很复杂很高深?其实不然,它们就像是你的汽车的大脑和心脏,它们控制着你的汽车软件的启动、运行和停止,以及与你的汽车…

SecureCRT出现乱码的解决方法

SecureCRT是一个商业终端连接工具&#xff0c;它支持多种自定义设置。默认设置下&#xff0c;通过SecureCRT连接SSH服务器可能出现中文乱码的情况。这是由于SecureCRT字符编码与服务器的字符编码不一致造成的。 当然解决这个问题也很简单&#xff0c;将SecureCRT字符编码设置成…