ai编写的中国象棋小游戏html代码
代码语言:html
所属分类:游戏
代码描述:ai编写的中国象棋小游戏html代码,有点bug
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>中国象棋 - 人机对战</title> <style> body { display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f0f0f0; font-family: Arial, sans-serif; } canvas { box-shadow: 0 0 10px rgba(0,0,0,0.3); } #gameInfo { margin-top: 20px; font-size: 18px; } #restartButton { margin-top: 10px; padding: 10px 20px; font-size: 16px; background-color: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer; } #restartButton:hover { background-color: #45a049; } </style> </head> <body> <canvas id="chessboard" width="450" height="500"></canvas> <div id="gameInfo"></div> <button id="restartButton" style="display: none;">重新开始</button> <script> const canvas = document.getElementById('chessboard'); const ctx = canvas.getContext('2d'); const gameInfo = document.getElementById('gameInfo'); const restartButton = document.getElementById('restartButton'); const CELL_SIZE = 50; const PIECE_RADIUS = 23; const pieceTypes = { 'che': '车', 'ma': '马', 'xiang': '相', 'shi': '士', 'jiang': '将', 'pao': '炮', 'bing': '兵', 'shuai': '帅', 'zu': '卒' }; let pieces = []; let selectedPiece = null; let playerTurn = 'red'; // 玩家始终操控红方 let gameOver = false; let possibleMoves = []; function initializePieces() { return [ {type: 'che', x: 0, y: 0}, {type: 'ma', x: 1, y: 0}, {type: 'xiang', x: 2, y: 0}, {type: 'shi', x: 3, y: 0}, {type: 'jiang', x: 4, y: 0}, {type: 'shi', x: 5, y: 0}, {type: 'xiang', x: 6, y: 0}, {type: 'ma', x: 7, y: 0}, {type: 'che', x: 8, y: 0}, {type: 'pao', x: 1, y: 2}, {type: 'pao', x: 7, y: 2}, {type: 'bing', x: 0, y: 3}, {type: 'bing', x: 2, y: 3}, {type: 'bing', x: 4, y: 3}, {type: 'bing', x: 6, y: 3}, {type: 'bing', x: 8, y: 3}, {type: 'che', x: 0, y: 9, side: 'red'}, {type: 'ma', x: 1, y: 9, side: 'red'}, {type: 'xiang', x: 2, y: 9, side: 'red'}, {type: 'shi', x: 3, y: 9, side: 'red'}, {type: 'shuai', x: 4, y: 9, side: 'red'}, {type: 'shi', x: 5, y: 9, side: 'red'}, {type: 'xiang', x: 6, y: 9, side: 'red'}, {type: 'ma', x: 7, y: 9, side: 'red'}, {type: 'che', x: 8, y: 9, side: 'red'}, {type: 'pao', x: 1, y: 7, side: 'red'}, {type: 'pao', x: 7, y: 7, side: 'red'}, {type: 'zu', x: 0, y: 6, side: 'red'}, {type: 'zu', x: 2, y: 6, side: 'red'}, {type: 'zu', x: 4, y: 6, side: 'red'}, {type: 'zu', x: 6, y: 6, side: 'red'}, {type: 'zu', x: 8, y: 6, side: 'red'} ]; } function drawBoard() { ctx.fillStyle = '#f0d9b5'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = '#8b4513'; ctx.lineWidth = 2; for (let i = 0; i < 10; i++) { ctx.beginPath(); ctx.moveTo(CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE / 2); ctx.lineTo(8.5 * CELL_SIZE, i * CELL_SIZE + CELL_SIZE / 2); ctx.stroke(); } for (let i = 0; i < 9; i++) { ctx.beginPath(); ctx.moveTo(i * CELL_SIZE + CELL_SIZE / 2, CELL_SIZE / 2); ctx.lineTo(i * CELL_SIZE + CELL_SIZE / 2, 9.5 * CELL_SIZE); ctx.stroke(); } ctx.font = 'bold 24px Arial'; ctx.fillStyle = '#8b4513'; ctx.fillText('楚 河', 1.7 * CELL_SIZE, 5.2 * CELL_SIZE); ctx.fillText('汉 界', 6 * CELL_SIZE, 5.2 * CELL_SIZE); } function drawPiece(piece) { const gradient = ctx.createRadialGradient( (piece.x + 0.5) * CELL_SIZE, (piece.y + 0.5) * CELL_SIZE, 0, (piece.x + 0.5) * CELL_SIZE, (piece.y + 0.5) * CELL_SIZE, PIECE_RADIUS ); gradient.addColorStop(0, piece.side === 'red' ? '#ff9999' : '#999999'); gradient.addColorStop(1, piece.side === 'red' ? '#ff0000' : '#000000'); ctx.beginPath(); ctx.arc((piece.x + 0.5) * CELL_SIZE, (piece.y + 0.5) * CELL_SIZE, PIECE_RADIUS, 0, 2 * Math.PI); ctx.fillStyle = gradient; ctx.fill(); ctx.strokeStyle = piece.side === 'red' ? '#8b0000' : '#696969'; ctx.lineWidth = 2; ctx.stroke(); ctx.fillStyle = piece.side === 'red' ? '#ffff00' : '#ffffff'; ctx.font = 'bold 20px Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(pieceTypes[piece.type], (piece.x + 0.5) * CELL_SIZE, (piece.y + 0.5) * CELL_SIZE); if (piece === selectedPiece) { ctx.strokeStyle = '#00ff00'; ctx.lineWidth = 3; ctx.stroke(); } } function drawPieces() { pieces.forEach(drawPiece); } function drawPossibleMoves() { ctx.fillStyle = 'rgba(0, 255, 0, 0.3)'; possibleMoves.forEach(move => { ctx.beginPath(); ctx.arc((move.x + 0.5) * CELL_SIZE, (move.y + 0.5) * CELL_SIZE, PIECE_RADIUS / 2, 0, 2 * Math.PI); ctx.fill(); }); } function draw() { drawBoard(); drawPieces(); drawPossibleMoves(); } function getPieceAt(x, y) { return pieces.find(piece => piece.x === x && piece.y === y); } function isValidMove(piece, newX, newY) { if (newX < 0 || newX > 8 || newY < 0 || newY > 9) return false; const targetPiece = getPieceAt(newX, newY); if (targetPiece && targetPiece.side === piece.side) return false; switch (piece.type) { case 'che': return (piece.x === newX || piece.y === newY) && !hasObstacleBetween(piece, newX, newY); case 'ma': const dx = Math.abs(newX - piece.x); const dy = Math.abs(newY - piece.y); return (dx === 1 && dy === 2) || (dx === 2 && dy === 1) && !hasHorseLegObstacle(piece, newX, newY); case 'xiang': return Math.abs(newX - piece.x) === 2 && Math.abs(newY - piece.y) === 2 && !getPieceAt((piece.x + newX) / 2, (piece.y + newY) / 2) && (piece.side === 'red' ? newY > 4 : newY < 5); case 'shi': return Math.abs.........完整代码请登录后点击上方下载按钮下载查看
网友评论0