使用deekpseek v2开发中国象棋游戏

使用AI可以完成简单程序(如:五子棋),甚至都不要调试即可以运行,但逻辑规则复杂的程序就需要反复的调整,修改运行BUG,优化运行性能。(如:中国象棋,支持提示目标落子位置,并要求使用AI算法自动对弈)。

下面是经过反复调整后(N多次),得到的中国象棋游戏的js代码。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>中国象棋</title><style>.board {display: grid;grid-template-columns: repeat(9, 50px);grid-template-rows: repeat(10, 50px);gap: 1px;background-color: #f0d9b5;width: fit-content;margin: auto;}.cell {width: 50px;height: 50px;background-color: #b58863;display: flex;align-items: center;justify-content: center;font-size: 24px;cursor: pointer;}.piece {width: 40px;height: 40px;border-radius: 50%;display: flex;align-items: center;justify-content: center;border: 2px solid;background-color: white;}.red {color: red;border-color: red;}.black {color: black;border-color: black;}.possible-move {position: relative;z-index: 1000;}.possible-move::after {content: '';position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);width: 45px;height: 45px;border: 2px dashed white;border-radius: 50%;z-index: 1000;}@keyframes blink {0%, 100% { opacity: 1; }50% { opacity: 0; }}.blink {animation: blink 1s linear infinite;}</style>
</head>
<body><h1 align="center">中国象棋</h1><div class="board" id="board"></div><script>document.addEventListener('DOMContentLoaded', function() {const board = document.getElementById('board');const pieces = {'red': ['车_red', '马_red', '相_red', '仕_red', '帅_red', '仕_red', '相_red', '马_red', '车_red', '炮_red', '炮_red', '兵_red', '兵_red', '兵_red', '兵_red', '兵_red'],'black': ['车_black', '马_black', '象_black', '士_black', '将_black', '士_black', '象_black', '马_black', '车_black', '炮_black', '炮_black', '卒_black', '卒_black', '卒_black', '卒_black', '卒_black']};const initialPositions = {'red': [[0, 1, 2, 3, 4, 5, 6, 7, 8],[null, null, null, null, null, null, null, null, null],[null, 9, null, null, null, null, null, 10, null],[11, null, 12, null, 13, null, 14, null, 15],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null]],'black': [[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[null, null, null, null, null, null, null, null, null],[11, null, 12, null, 13, null, 14, null, 15],[null, 9, null, null, null, null, null, 10, null],[null, null, null, null, null, null, null, null, null],[0, 1, 2, 3, 4, 5, 6, 7, 8]]};let selectedPiece = null;function createBoard() {for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const cell = document.createElement('div');cell.classList.add('cell');cell.dataset.row = row;cell.dataset.col = col;board.appendChild(cell);const redPieceIndex = initialPositions.red[row]?.[col];const blackPieceIndex = initialPositions.black[row]?.[col];if (redPieceIndex !== null && redPieceIndex !== undefined) {const piece = document.createElement('div');piece.classList.add('piece', 'red');piece.textContent = pieces.red[redPieceIndex].split('_')[0];cell.appendChild(piece);} else if (blackPieceIndex !== null && blackPieceIndex !== undefined) {const piece = document.createElement('div');piece.classList.add('piece', 'black');piece.textContent = pieces.black[blackPieceIndex].split('_')[0];cell.appendChild(piece);}cell.addEventListener('click', handleCellClick);}}}function handleCellClick(event) {let cell = event.target;if (cell.classList.contains('piece')) {cell = cell.parentElement;}if (!cell) {console.error('Cell is null');return;}const row = parseInt(cell.dataset.row);const col = parseInt(cell.dataset.col);if (selectedPiece) {if (cell.classList.contains('possible-move')) {const oldRow = parseInt(selectedPiece.parentElement.dataset.row);const oldCol = parseInt(selectedPiece.parentElement.dataset.col);const targetCell = document.querySelector(`.cell[data-row="${oldRow}"][data-col="${oldCol}"]`);const targetPiece = cell.querySelector('.piece');if (targetPiece) {cell.removeChild(targetPiece);}targetCell.innerHTML = '';cell.appendChild(selectedPiece);selectedPiece = null;renderBoard();setTimeout(aiMove, 500);} else {selectedPiece = null;}clearPossibleMoves();} else {const piece = cell.querySelector('.piece');if (piece) {const pieceText = piece.textContent;const color = piece.classList.contains('red') ? 'red' : 'black';if (color) {selectedPiece = piece;highlightPossibleMoves(row, col, color);} else {console.error('Color is undefined');}}}}function renderBoard() {const boardState = getBoardState();const board = document.getElementById('board');board.innerHTML = '';for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const cell = document.createElement('div');cell.classList.add('cell');cell.dataset.row = row;cell.dataset.col = col;board.appendChild(cell);const piece = boardState[row][col];if (piece) {const [pieceText, color] = piece.split('_');const pieceElement = document.createElement('div');pieceElement.classList.add('piece', color);pieceElement.textContent = pieceText;cell.appendChild(pieceElement);}cell.addEventListener('click', handleCellClick);}}}function highlightPossibleMoves(row, col, color) {const piece = selectedPiece.textContent;const possibleMoves = getPossibleMoves(getBoardState(), row, col, color);possibleMoves.forEach(([r, c]) => {const targetCell = document.querySelector(`.cell[data-row="${r}"][data-col="${c}"]`);if (targetCell) {targetCell.classList.add('possible-move');}});}function clearPossibleMoves() {document.querySelectorAll('.possible-move').forEach(cell => {cell.classList.remove('possible-move');});}function getPossibleMoves(boardState, row, col, color) {const piece = boardState[row][col];const [pieceText, _] = piece.split('_');const possibleMoves = [];switch (pieceText) {case '车':// 水平移动for (let i = col + 1; i < 9; i++) {if (boardState[row][i]) {if (boardState[row][i].includes(color)) {break;} else {possibleMoves.push([row, i]);break;}}possibleMoves.push([row, i]);}for (let i = col - 1; i >= 0; i--) {if (boardState[row][i]) {if (boardState[row][i].includes(color)) {break;} else {possibleMoves.push([row, i]);break;}}possibleMoves.push([row, i]);}// 垂直移动for (let i = row + 1; i < 10; i++) {if (boardState[i][col]) {if (boardState[i][col].includes(color)) {break;} else {possibleMoves.push([i, col]);break;}}possibleMoves.push([i, col]);}for (let i = row - 1; i >= 0; i--) {if (boardState[i][col]) {if (boardState[i][col].includes(color)) {break;} else {possibleMoves.push([i, col]);break;}}possibleMoves.push([i, col]);}break;case '马':const horseMoves = [[row - 2, col - 1], [row - 2, col + 1],[row - 1, col - 2], [row - 1, col + 2],[row + 1, col - 2], [row + 1, col + 2],[row + 2, col - 1], [row + 2, col + 1]];const horseBlockers = [[row - 1, col], [row - 1, col],[row, col - 1], [row, col + 1],[row, col - 1], [row, col + 1],[row + 1, col], [row + 1, col]];for (let i = 0; i < horseMoves.length; i++) {const [r, c] = horseMoves[i];const [br, bc] = horseBlockers[i];if (r >= 0 && r < 10 && c >= 0 && c < 9) {if (!boardState[br][bc] && (!boardState[r][c] || !boardState[r][c].includes(color))) {possibleMoves.push([r, c]);}}}break;case '象':case '相':const elephantMoves = [[row - 2, col - 2], [row - 2, col + 2],[row + 2, col - 2], [row + 2, col + 2]];const elephantBlockers = [[row - 1, col - 1], [row - 1, col + 1],[row + 1, col - 1], [row + 1, col + 1]];for (let i = 0; i < elephantMoves.length; i++) {const [r, c] = elephantMoves[i];const [br, bc] = elephantBlockers[i];if (r >= 0 && r < 10 && c >= 0 && c < 9) {if (!boardState[br][bc] && (!boardState[r][c] || !boardState[r][c].includes(color))) {possibleMoves.push([r, c]);}}}break;case '士':case '仕':const guardMoves = [[row - 1, col - 1], [row - 1, col + 1],[row + 1, col - 1], [row + 1, col + 1]];guardMoves.forEach(([r, c]) => {if (r >= 0 && r < 10 && c >= 3 && c <= 5) {if (!boardState[r][c] || !boardState[r][c].includes(color)) {possibleMoves.push([r, c]);}}});break;case '帅':case '将':const kingMoves = [[row - 1, col], [row + 1, col],[row, col - 1], [row, col + 1]];kingMoves.forEach(([r, c]) => {if (r >= 0 && r < 10 && c >= 3 && c <= 5) {if (!boardState[r][c] || !boardState[r][c].includes(color)) {possibleMoves.push([r, c]);}}});break;case '炮':function checkMoves(start, end, step, fixedRow, fixedCol) {let piecesBetween = 0;for (let i = start; i !== end; i += step) {const targetCell = fixedRow !== null ? boardState[fixedRow][i] : boardState[i][fixedCol];if (targetCell) {piecesBetween++;}if (piecesBetween === 0) {if (!targetCell || !targetCell.includes(color)) {possibleMoves.push(fixedRow !== null ? [fixedRow, i] : [i, fixedCol]);}} else if (piecesBetween === 2) {if (targetCell && !targetCell.includes(color)) {possibleMoves.push(fixedRow !== null ? [fixedRow, i] : [i, fixedCol]);}}}}// 水平移动checkMoves(col + 1, 9, 1, row, null);checkMoves(col - 1, -1, -1, row, null);// 垂直移动checkMoves(row + 1, 10, 1, null, col);checkMoves(row - 1, -1, -1, null, col);break;case '兵':case '卒':if (color === 'red') {if (row < 5) {possibleMoves.push([row + 1, col]);} else {possibleMoves.push([row - 1, col]);possibleMoves.push([row, col - 1]);possibleMoves.push([row, col + 1]);}} else {if (row > 4) {possibleMoves.push([row - 1, col]);} else {possibleMoves.push([row + 1, col]);possibleMoves.push([row, col - 1]);possibleMoves.push([row, col + 1]);}}break;}return possibleMoves;}function getBoardState() {const boardState = [];for (let row = 0; row < 10; row++) {const rowState = [];for (let col = 0; col < 9; col++) {const cell = document.querySelector(`.cell[data-row="${row}"][data-col="${col}"]`);const piece = cell.querySelector('.piece');if (piece) {const color = piece.classList.contains('red') ? 'red' : 'black';rowState.push(`${piece.textContent}_${color}`);} else {rowState.push(null);}}boardState.push(rowState);}return boardState;}function aiMove() {const boardState = getBoardState();const moves = generateMoves(boardState, true);let bestMove = null;let bestValue = -Infinity;for (const [fromRow, fromCol, toRow, toCol] of moves) {const newBoardState = makeMove(boardState, fromRow, fromCol, toRow, toCol);const value = evaluateBoard(newBoardState, true);if (value > bestValue) {bestValue = value;bestMove = [fromRow, fromCol, toRow, toCol];}}if (bestMove) {const [fromRow, fromCol, toRow, toCol] = bestMove;const fromCell = document.querySelector(`.cell[data-row="${fromRow}"][data-col="${fromCol}"]`);const toCell = document.querySelector(`.cell[data-row="${toRow}"][data-col="${toCol}"]`);const piece = fromCell.querySelector('.piece');const targetPiece = toCell.querySelector('.piece');if (targetPiece) {toCell.removeChild(targetPiece);}fromCell.innerHTML = '';toCell.appendChild(piece);piece.classList.add('blink');setTimeout(() => {piece.classList.remove('blink');}, 2000);// Check if the game is over after the moveconst newBoardState = getBoardState();const gameStatus = isGameOver(newBoardState);if (gameStatus) {if (gameStatus === 'check') {alert('将军!');} else {alert(`游戏结束!${gameStatus === 'red' ? '红方' : '黑方'}获胜!`);resetBoard();}}} else {console.error('No valid move found');}}function resetBoard() {const board = document.getElementById('board');board.innerHTML = '';createBoard();}function generateMoves(boardState, isMaximizingPlayer) {const moves = [];const player = isMaximizingPlayer ? 'red' : 'black';for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const piece = boardState[row][col];if (piece && (piece.includes(player))) {const possibleMoves = getPossibleMoves(boardState, row, col, player);for (const [toRow, toCol] of possibleMoves) {moves.push([row, col, toRow, toCol]);}}}}return moves;}function makeMove(boardState, fromRow, fromCol, toRow, toCol) {const newBoardState = boardState.map(row => row.slice());newBoardState[toRow][toCol] = newBoardState[fromRow][fromCol];newBoardState[fromRow][fromCol] = null;return newBoardState;}function evaluateBoard(boardState, isMaximizingPlayer) {const pieceValues = {'车': 10,'马': 5,'象': 3,'相': 3,'士': 3,'仕': 3,'将': 100,'帅': 100,'炮': 5,'兵': 2,'卒': 2};let score = 0;const player = isMaximizingPlayer ? 'red' : 'black';for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const piece = boardState[row][col];if (piece){const [pieceText, color] = piece.split('_');if (pieceText) {const value = pieceValues[pieceText];if (color ===player) {score += value;} else {score -= value;}}}}}return score;}function isGameOver(boardState) {let redKingExists = false;let blackKingExists = false;for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const piece = boardState[row][col];if (piece) {const [pieceText, color] = piece.split('_');if (pieceText === '帅') {redKingExists = true;} else if (pieceText === '将') {blackKingExists = true;}}}}if (!redKingExists) {return 'black'; // 黑方获胜} else if (!blackKingExists) {return 'red'; // 红方获胜}// Check for check conditionconst redKingPosition = findKingPosition(boardState, 'red');const blackKingPosition = findKingPosition(boardState, 'black');if (isKingInCheck(boardState, redKingPosition, 'red')) {return 'check';}if (isKingInCheck(boardState, blackKingPosition, 'black')) {return 'check';}return null;}function findKingPosition(boardState, color) {const kingText = color === 'red' ? '帅' : '将';for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const piece = boardState[row][col];if (piece && piece.includes(kingText)) {return [row, col];}}}return null;}function isKingInCheck(boardState, kingPosition, color) {const [kingRow, kingCol] = kingPosition;const opponentColor = color === 'red' ? 'black' : 'red';for (let row = 0; row < 10; row++) {for (let col = 0; col < 9; col++) {const piece = boardState[row][col];if (piece && piece.includes(opponentColor)) {const [pieceText, _] = piece.split('_');const possibleMoves = getPossibleMoves(boardState, row, col, opponentColor);if (possibleMoves.some(([r, c]) => r === kingRow && c === kingCol)) {return true;}}}}return false;}createBoard();});</script>
</body>
</html>


http://www.ppmy.cn/server/113770.html

相关文章

狂奔的荣耀,稳健的苹果:AI Agent手机竞速赛

每一次技术革命&#xff0c;都需要一个技术落地的锚点&#xff0c;比如燃油革命时代的汽车&#xff0c;信息革命时代的PC与手机。而这一次以预训练大模型为主导的AI技术爆发中&#xff0c;被认为最有可能成为智能技术落地锚点的&#xff0c;就是AI Agent&#xff0c;或者称为智…

sv标准研读第四章-时间调度机制

书接上回&#xff1a; sv标准研读第一章-综述 sv标准研读第二章-标准引用 sv标准研读第三章-设计和验证的building block 第4章 时间调度机制 4.1 概述 此章描述以下内容: -基于事件的模拟调度语义 - SystemVerilog的分层事件调度算法 -事件排序的确定性和非确定性 -…

java计算机毕设课设—停车管理信息系统(附源码、文章、相关截图、部署视频)

这是什么系统&#xff1f; 资源获取方式在最下方 java计算机毕设课设—停车管理信息系统(附源码、文章、相关截图、部署视频) 停车管理信息系统是为了提升停车场的运营效率和管理水平而设计的综合性平台。系统涵盖用户信息管理、车位管理、收费管理、违规车辆处理等多个功能…

模型压缩之剪枝

&#xff08;1&#xff09;通道选择 这里要先解释一下&#xff1a; &#xff08;1&#xff09;通道剪枝 那我们实际做法不是上面直接对所有层都添加L1正则项&#xff0c;而是仅仅对BN层权重添加L1正则项。通道剪枝具体步骤如下&#xff1a; 1.BN层权重添加L1正则项&#xf…

Redis 事务

本篇文章内容为Redis 事务的四种命令介绍及应用场景和示例 目录 Redis 事务 使用Redis事务 1.事务开始 2.命令入队 3.事务执行 取消事务 WATCH命令的实现 总结 Redis 事务 Redis通过MULTI、EXEC、WATCH、DISCARD等命令来实现事务&#xff08;transaction&#xff09;功…

微信小程序登录与获取手机号 (Python)

文章目录 相关术语登录逻辑登录设计登录代码 相关术语 调用接口[wx.login()]获取登录凭证&#xff08;code&#xff09;。通过凭证进而换取用户登录态信息&#xff0c;包括用户在当前小程序的唯一标识&#xff08;openid&#xff09;、微信开放平台账号下的唯一标识&#xff0…

亚信安全荣获“2024年网络安全优秀创新成果大赛”优胜奖

近日&#xff0c;由中央网信办网络安全协调局指导、中国网络安全产业联盟&#xff08;CCIA&#xff09;主办的“2024年网络安全优秀创新成果大赛”评选结果公布。亚信安全信舱ForCloud荣获“创新产品”优胜奖&#xff0c;亚信安全“宁波市政务信息化网络数据安全一体化指挥系统…

【Rust光年纪】从心理学计算到机器学习:Rust语言数据科学库全方位解读!

Rust语言的数据科学和机器学习库大揭秘&#xff1a;核心功能、使用指南一网打尽&#xff01; 前言 随着数据科学和机器学习在各个领域的广泛应用&#xff0c;使用高效、稳定的编程语言来实现这些功能变得尤为重要。Rust语言作为一种安全且高性能的系统编程语言&#xff0c;正…

前端核心基础知识总结

目录 前言 一、HTML模块 1. 标签结构 2. 语义化标签 3. 表单元素 二、CSS模块 1. 选择器 2. 盒模型 示例一&#xff1a;为一个div标签设置了宽度为 200 像素&#xff0c;高度为 100 像素的内容区。 示例二&#xff1a;内边距的存在可以使内容与边框之间有一定的间隔&…

基于云函数的自习室预约微信小程序+LW示例参考

全阶段全种类学习资源&#xff0c;内涵少儿、小学、初中、高中、大学、专升本、考研、四六级、建造师、法考、网赚技巧、毕业设计等&#xff0c;持续更新~ 文章目录 [TOC](文章目录) 1.项目介绍2.项目部署3.项目部分截图4.获取方式 1.项目介绍 技术栈工具&#xff1a;云数据库…

java设计模式(行为型模式:状态模式、观察者模式、中介者模式、迭代器模式、访问者模式、备忘录模式、解释器模式)

6&#xff0c;行为型模式 6.5 状态模式 6.5.1 概述 【例】通过按钮来控制一个电梯的状态&#xff0c;一个电梯有开门状态&#xff0c;关门状态&#xff0c;停止状态&#xff0c;运行状态。每一种状态改变&#xff0c;都有可能要根据其他状态来更新处理。例如&#xff0c;如果…

JS中【async】和【defer】属性详解与区别

理解浏览器如何处理JavaScript以及相关的async和defer属性对于前端开发是非常重要的。以下是相关知识点的详细讲解&#xff1a; 1. 浏览器的解析和渲染过程 浏览器在加载网页时&#xff0c;会按照以下步骤解析和渲染内容&#xff1a; HTML解析: 浏览器从顶部开始逐行解析HTML…

【语音告警】博灵智能语音报警灯JavaScript循环播报场景实例-语音报警灯|声光报警器|网络信号灯

功能说明 本文将以JavaScript代码为实例&#xff0c;讲解如何通过JavaScript代码调用博灵语音通知终端 A4实现声光语音告警。主要博灵语音通知终端如何实现无线循环播报或者周期播报的功能。 本代码实现HTTP接口的声光语音播报&#xff0c;并指定循环次数、播报内容。由于通知…

C++ linux下的cmake

cmake是一个帮助我们构建项目的跨平台工具。让我们不需要一次次手动配置makefile&#xff0c;或者手动去链接库这些操作。 配置 &#xff08;基于vscode编辑器&#xff09; 在项目main.cpp同级目录下&#xff0c;创建CMakeLists.txt文件&#xff0c;举例内容如下&#xff08;需…

衡石分析平台使用手册-快速入门

快速入门​ 快速指南​ 创建管理员账号​ 按照文档安装成功之后&#xff0c;假设安装所在服务器 IP 是<Server IP>&#xff0c;端口是<Server Port>&#xff0c;则可以通过浏览器访问http://<Server IP>:<Server Port>/ 访问衡石分析平台&#xff0…

代码随想录算法day28 | 动态规划算法part01 | 理论基础、509. 斐波那契数、70. 爬楼梯、 746. 使用最小花费爬楼梯

理论基础 什么是动态规划 动态规划&#xff0c;英文&#xff1a;Dynamic Programming&#xff0c;简称DP&#xff0c;如果某一问题有很多重叠子问题&#xff0c;使用动态规划是最有效的。 所以动态规划中每一个状态一定是由上一个状态推导出来的&#xff0c;这一点就区分于贪…

任务执行拓扑排序(华为od机考题)

一、题目 1.原题 一个应用启动时&#xff0c;会有多个初始化任务需要执行&#xff0c; 并且任务之间有依赖关系&#xff0c; 例如&#xff1a;A任务依赖B任务&#xff0c;那么必须在B任务执行完成之后&#xff0c;才能开始执行A任务。 现在给出多条任务依赖关系的规则&#x…

银行定期产品

银行存款产品如下: 其中对私的储蓄存款: 定期存款是指存款人在银行或金融机构存入一定金额的资金,并约定一个固定的存期,在存期内不得随意支取,到期后可以获取本金和预先约定好的利息的一种存款方式。根据不同的存取方式和特点,定期存款主要可以分为以下几种类型: 整存…

Redis进阶(二)--Redis高级特性和应用

文章目录 第二章、Redis高级特性和应用一、Redis的慢查询1、慢查询配置2、慢查询操作命令3、慢查询建议 二、Pipeline三、事务1、Redis的事务原理2、Redis的watch命令3、Pipeline和事务的区别 四、Lua1、Lua入门&#xff08;1&#xff09;安装Lua&#xff08;2&#xff09;Lua基…

无人机纪录片航拍认知

写在前面 博文内容为纪录片航拍简单认知&#xff1a;纪录片 航拍镜头&#xff0c;航拍流程&#xff0c;航拍环境条件注意事项介绍航拍学习书籍推荐《无人机商业航拍教程》读书笔记整理&#xff0c;适合小白认知理解不足小伙伴帮忙指正 &#x1f603;,生活加油 99%的焦虑都来自于…