救救孩子!Trae在今天写了个象棋

792 阅读5分钟

本人在今天突发奇想做一个传统游戏——象棋。但是没想到trae仅仅只是询问一下就直接生成完整的象棋

屏幕截图 2025-05-14 210432.png




          
# 中国象棋基本页面设计

我将为您创建一个基本的中国象棋页面。这个页面将包含象棋棋盘、棋子以及基本的游戏功能。我们将使用HTMLCSSJavaScript来实现这个项目。

## 文件结构

我们需要创建以下文件:
1. `index.html` - 主页面
2. `style.css` - 样式表
3. `script.js` - 游戏逻辑
4. `README.md` - 项目说明文档

让我们开始创建这些文件:

### index.html

```html:index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中国象棋</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <h1>中国象棋</h1>
        <div class="game-info">
            <div id="status">当前回合: 红方</div>
            <button id="restart">重新开始</button>
        </div>
        <div id="chessboard">
            <!-- 棋盘将通过JavaScript动态生成 -->
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>

style.css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Microsoft YaHei', sans-serif;
    background-color: #f5f5f5;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.container {
    text-align: center;
}

h1 {
    color: #8B4513;
    margin-bottom: 20px;
}

.game-info {
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#status {
    font-size: 18px;
    font-weight: bold;
}

#restart {
    padding: 8px 16px;
    background-color: #8B4513;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

#restart:hover {
    background-color: #A0522D;
}

#chessboard {
    position: relative;
    width: 450px;
    height: 500px;
    background-color: #DEB887;
    margin: 0 auto;
    border: 2px solid #8B4513;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50"><line x1="0" y1="0" x2="50" y2="0" stroke="%238B4513" stroke-width="1"/><line x1="0" y1="50" x2="50" y2="50" stroke="%238B4513" stroke-width="1"/><line x1="0" y1="0" x2="0" y2="50" stroke="%238B4513" stroke-width="1"/><line x1="50" y1="0" x2="50" y2="50" stroke="%238B4513" stroke-width="1"/></svg>');
    background-size: 50px 50px;
}

.chess-piece {
    position: absolute;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.2s;
    z-index: 10;
}

.chess-piece:hover {
    transform: scale(1.1);
}

.red-piece {
    background-color: #f44336;
    color: white;
    border: 2px solid #d32f2f;
}

.black-piece {
    background-color: #212121;
    color: white;
    border: 2px solid #000;
}

.selected {
    box-shadow: 0 0 10px 3px yellow;
}

.possible-move {
    position: absolute;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: rgba(0, 255, 0, 0.3);
    z-index: 5;
}

.river {
    position: absolute;
    width: 100%;
    height: 50px;
    top: 225px;
    background-color: rgba(135, 206, 250, 0.3);
    z-index: 1;
}

.palace {
    position: absolute;
    width: 150px;
    height: 150px;
    background-color: rgba(255, 215, 0, 0.1);
    z-index: 1;
}

.black-palace {
    top: 0;
    left: 150px;
}

.red-palace {
    bottom: 0;
    left: 150px;
}

.coordinate {
    position: absolute;
    font-size: 12px;
    color: #8B4513;
    z-index: 2;
}

script.js

document.addEventListener('DOMContentLoaded', function() {
    // 棋盘尺寸
    const BOARD_WIDTH = 9;
    const BOARD_HEIGHT = 10;
    const GRID_SIZE = 50;
    
    // 游戏状态
    let gameState = {
        currentPlayer: 'red', // 'red' 或 'black'
        selectedPiece: null,
        pieces: []
    };
    
    // 初始化棋盘
    function initBoard() {
        const chessboard = document.getElementById('chessboard');
        
        // 清空棋盘
        chessboard.innerHTML = '';
        
        // 添加河流
        const river = document.createElement('div');
        river.className = 'river';
        chessboard.appendChild(river);
        
        // 添加宫殿
        const blackPalace = document.createElement('div');
        blackPalace.className = 'palace black-palace';
        chessboard.appendChild(blackPalace);
        
        const redPalace = document.createElement('div');
        redPalace.className = 'palace red-palace';
        chessboard.appendChild(redPalace);
        
        // 添加坐标
        for (let x = 0; x < BOARD_WIDTH; x++) {
            for (let y = 0; y < BOARD_HEIGHT; y++) {
                const coordinate = document.createElement('div');
                coordinate.className = 'coordinate';
                coordinate.style.left = (x * GRID_SIZE + 25) + 'px';
                coordinate.style.top = (y * GRID_SIZE + 25) + 'px';
                chessboard.appendChild(coordinate);
            }
        }
        
        // 初始化棋子
        initPieces();
    }
    
    // 初始化棋子
    function initPieces() {
        gameState.pieces = [];
        
        // 红方棋子
        addPiece('red', '车', 0, 9);
        addPiece('red', '马', 1, 9);
        addPiece('red', '相', 2, 9);
        addPiece('red', '士', 3, 9);
        addPiece('red', '帅', 4, 9);
        addPiece('red', '士', 5, 9);
        addPiece('red', '相', 6, 9);
        addPiece('red', '马', 7, 9);
        addPiece('red', '车', 8, 9);
        addPiece('red', '炮', 1, 7);
        addPiece('red', '炮', 7, 7);
        addPiece('red', '兵', 0, 6);
        addPiece('red', '兵', 2, 6);
        addPiece('red', '兵', 4, 6);
        addPiece('red', '兵', 6, 6);
        addPiece('red', '兵', 8, 6);
        
        // 黑方棋子
        addPiece('black', '车', 0, 0);
        addPiece('black', '马', 1, 0);
        addPiece('black', '象', 2, 0);
        addPiece('black', '士', 3, 0);
        addPiece('black', '将', 4, 0);
        addPiece('black', '士', 5, 0);
        addPiece('black', '象', 6, 0);
        addPiece('black', '马', 7, 0);
        addPiece('black', '车', 8, 0);
        addPiece('black', '炮', 1, 2);
        addPiece('black', '炮', 7, 2);
        addPiece('black', '卒', 0, 3);
        addPiece('black', '卒', 2, 3);
        addPiece('black', '卒', 4, 3);
        addPiece('black', '卒', 6, 3);
        addPiece('black', '卒', 8, 3);
        
        renderPieces();
    }
    
    // 添加棋子
    function addPiece(color, type, x, y) {
        gameState.pieces.push({
            color: color,
            type: type,
            x: x,
            y: y
        });
    }
    
    // 渲染棋子
    function renderPieces() {
        const chessboard = document.getElementById('chessboard');
        
        // 移除所有棋子
        const oldPieces = document.querySelectorAll('.chess-piece');
        oldPieces.forEach(piece => piece.remove());
        
        // 添加棋子
        gameState.pieces.forEach(piece => {
            const pieceElement = document.createElement('div');
            pieceElement.className = `chess-piece ${piece.color}-piece`;
            pieceElement.textContent = piece.type;
            pieceElement.style.left = (piece.x * GRID_SIZE + 5) + 'px';
            pieceElement.style.top = (piece.y * GRID_SIZE + 5) + 'px';
            
            // 添加点击事件
            pieceElement.addEventListener('click', function() {
                handlePieceClick(piece);
            });
            
            chessboard.appendChild(pieceElement);
        });
        
        // 高亮选中的棋子
        if (gameState.selectedPiece) {
            const selectedElement = document.querySelector(`.chess-piece[style*="left: ${gameState.selectedPiece.x * GRID_SIZE + 5}px"][style*="top: ${gameState.selectedPiece.y * GRID_SIZE + 5}px"]`);
            if (selectedElement) {
                selectedElement.classList.add('selected');
            }
        }
    }
    
    // 处理棋子点击
    function handlePieceClick(piece) {
        // 如果点击的是当前玩家的棋子
        if (piece.color === gameState.currentPlayer) {
            // 选择棋子
            gameState.selectedPiece = piece;
            renderPieces();
            showPossibleMoves(piece);
        } 
        // 如果已经选择了棋子,并且点击的是对方的棋子或空位
        else if (gameState.selectedPiece) {
            // 尝试移动棋子
            movePiece(gameState.selectedPiece, piece.x, piece.y);
        }
    }
    
    // 显示可能的移动位置
    function showPossibleMoves(piece) {
        // 这里应该根据不同棋子类型计算可能的移动位置
        // 简化版本,只显示周围的位置
        const chessboard = document.getElementById('chessboard');
        
        // 移除之前的可能移动标记
        const oldMarkers = document.querySelectorAll('.possible-move');
        oldMarkers.forEach(marker => marker.remove());
        
        // 简单示例:显示上下左右四个方向
        const directions = [
            {dx: 0, dy: -1}, // 上
            {dx: 1, dy: 0},  // 右
            {dx: 0, dy: 1},  // 下
            {dx: -1, dy: 0}  // 左
        ];
        
        directions.forEach(dir => {
            const newX = piece.x + dir.dx;
            const newY = piece.y + dir.dy;
            
            // 检查是否在棋盘范围内
            if (newX >= 0 && newX < BOARD_WIDTH && newY >= 0 && newY < BOARD_HEIGHT) {
                // 检查目标位置是否有己方棋子
                const targetPiece = getPieceAt(newX, newY);
                if (!targetPiece || targetPiece.color !== piece.color) {
                    const marker = document.createElement('div');
                    marker.className = 'possible-move';
                    marker.style.left = (newX * GRID_SIZE + 15) + 'px';
                    marker.style.top = (newY * GRID_SIZE + 15) + 'px';
                    
                    // 添加点击事件
                    marker.addEventListener('click', function() {
                        movePiece(piece, newX, newY);
                    });
                    
                    chessboard.appendChild(marker);
                }
            }
        });
    }
    
    // 获取指定位置的棋子
    function getPieceAt(x, y) {
        return gameState.pieces.find(p => p.x === x && p.y === y);
    }
    
    // 移动棋子
    function movePiece(piece, newX, newY) {
        // 检查目标位置是否有对方棋子
        const targetPiece = getPieceAt(newX, newY);
        if (targetPiece) {
            // 如果是对方棋子,吃掉它
            if (targetPiece.color !== piece.color) {
                gameState.pieces = gameState.pieces.filter(p => p !== targetPiece);
            } 
            // 如果是己方棋子,选择新棋子
            else {
                gameState.selectedPiece = targetPiece;
                renderPieces();
                showPossibleMoves(targetPiece);
                return;
            }
        }
        
        // 移动棋子
        piece.x = newX;
        piece.y = newY;
        
        // 清除选择
        gameState.selectedPiece = null;
        
        // 切换玩家
        gameState.currentPlayer = gameState.currentPlayer === 'red' ? 'black' : 'red';
        
        // 更新状态显示
        document.getElementById('status').textContent = `当前回合: ${gameState.currentPlayer === 'red' ? '红方' : '黑方'}`;
        
        // 重新渲染
        renderPieces();
        
        // 移除可能移动标记
        const oldMarkers = document.querySelectorAll('.possible-move');
        oldMarkers.forEach(marker => marker.remove());
    }
    
    // 重新开始游戏
    document.getElementById('restart').addEventListener('click', function() {
        gameState.currentPlayer = 'red';
        gameState.selectedPiece = null;
        initBoard();
        document.getElementById('status').textContent = '当前回合: 红方';
    });
    
    // 初始化游戏
    initBoard();
});

README.md

# 中国象棋

这是一个简单的中国象棋网页游戏,使用HTML、CSS和JavaScript实现。

## 功能特点

- 完整的中国象棋棋盘和棋子
- 支持两人对战
- 简单的走棋规则
- 可视化的可行走位置
- 游戏状态显示
- 重新开始功能

## 如何使用

1. 克隆或下载本仓库
2. 在浏览器中打开`index.html`文件
3. 开始游戏!

## 游戏规则

- 红方先行
- 点击己方棋子选中,再点击目标位置移动
- 吃掉对方的"帅"或"将"获胜

## 注意事项

这是一个简化版的中国象棋游戏,目前只实现了基本的移动功能,尚未实现完整的象棋规则(如马走日、象走田等特殊走法)。

## 未来改进

- 实现完整的象棋规则
- 添加胜利判定
- 添加音效
- 添加计时器
- 支持悔棋功能
- 支持保存和加载游戏

说明

这个中国象棋页面包含以下功能:

  1. 完整的象棋棋盘和棋子布局
  2. 基本的走棋功能(简化版,未实现完整规则)
  3. 可视化的可行走位置
  4. 回合显示和切换
  5. 重新开始游戏功能

目前这是一个简化版的实现,主要展示了基本的界面和交互。要实现完整的象棋规则(如马走日、象走田等特殊走法),需要进一步扩展代码。

您可以通过在浏览器中打开index.html文件来运行这个游戏。红方先行,点击己方棋子选中,再点击目标位置移动。

看着是不是很想一回事的,但是,

屏幕截图 2025-05-14 205940.png 我是真蚌埠住了,小马也是进化成超模怪了直接横冲直撞,使用模型ai是真的可以

象棋的大概轮廓是出来了,对于一个编程局外人是真的有种看fate里圣杯实现愿望的既视感, 实现了象棋,你别管到底能不能玩,你就说像不像吧!