game

· · 休闲·娱乐

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>双人·运兵车·仅己方倒地判定</title>
    <style>
        * {
            box-sizing: border-box;
            user-select: none;
        }
        body {
            background: #1a2a32;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: 'Segoe UI', Roboto, system-ui, sans-serif;
            margin: 0;
            padding: 16px;
        }
        .game-container {
            background: #2a3f4a;
            padding: 24px 24px 30px;
            border-radius: 42px;
            box-shadow: 0 20px 30px rgba(0,0,0,0.6);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: #f0e6d0;
            padding: 0 12px 20px 12px;
            font-weight: bold;
            text-shadow: 2px 2px 0 #1a2a32;
        }
        .player-panel {
            display: flex;
            gap: 36px;
            background: #17242b;
            padding: 12px 32px;
            border-radius: 60px;
            box-shadow: inset 0 -3px 0 #0f1a1f;
        }
        .player-info {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 1.3rem;
        }
        .blue .badge { background: #3b7dbd; box-shadow: 0 0 0 2px #7ab0e6; }
        .red .badge { background: #c44b4b; box-shadow: 0 0 0 2px #f29b9b; }
        .badge {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-size: 1.5rem;
            color: white;
        }
        .funds {
            background: #0f1f26;
            padding: 6px 18px;
            border-radius: 40px;
            font-family: monospace;
            font-size: 1.8rem;
            color: #ffd966;
            box-shadow: inset 0 -3px 0 #071117;
        }
        .victory-points {
            background: #2d2010;
            padding: 4px 16px;
            border-radius: 30px;
            font-size: 1.4rem;
            color: #faca7e;
            font-weight: 700;
            margin-left: 10px;
            border: 1px solid #b58947;
        }
        .turn-indicator {
            background: #dca54c;
            color: #1d2b32;
            padding: 8px 28px;
            border-radius: 40px;
            font-size: 1.4rem;
            font-weight: 800;
            letter-spacing: 1px;
            box-shadow: 0 6px 0 #8f6530;
        }
        .board-wrapper {
            display: flex;
            justify-content: center;
            background: #3f555f;
            padding: 24px;
            border-radius: 48px;
            box-shadow: inset 0 -6px 0 #26373f;
        }
        .grid {
            display: grid;
            grid-template-columns: repeat(18, 34px);
            grid-template-rows: repeat(16, 34px);
            gap: 3px;
            background: #56717e;
            padding: 10px;
            border-radius: 24px;
            box-shadow: 0 0 0 4px #8faebd, inset 0 0 0 2px #2b404a;
        }
        .cell {
            background: #b9cfda;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 800;
            font-size: 1.2rem;
            box-shadow: inset 0 -3px 0 #7f97a3;
            transition: 0.1s ease;
            cursor: pointer;
            color: #1f2f38;
            position: relative;
        }
        .cell.blue-unit {
            background: #7bb3e0;
            box-shadow: inset 0 -3px 0 #346fa0, 0 0 0 2px #1f4b7a;
            color: #0a2940;
        }
        .cell.red-unit {
            background: #e08484;
            box-shadow: inset 0 -3px 0 #aa4a4a, 0 0 0 2px #852e2e;
            color: #3d1515;
        }
        .cell.highlight {
            background: #fee684;
            box-shadow: 0 0 0 3px #ffbb2e, inset 0 -3px 0 #c28b30;
            transform: scale(1.02);
            cursor: pointer;
        }
        .cell.blue-unit.highlight, .cell.red-unit.highlight {
            filter: brightness(1.2);
            box-shadow: 0 0 0 4px gold;
        }
        .cell.blue-unit.defeated, .cell.red-unit.defeated {
            opacity: 0.5;
            filter: grayscale(0.6);
        }
        .cell.blue-unit.defeated::before, .cell.red-unit.defeated::before {
            content: "💫";
            position: absolute;
            font-size: 20px;
            opacity: 0.8;
            z-index: 10;
        }
        /* 据点样式 */
        .cell.outpost {
            background: #c2b28b;
            box-shadow: inset 0 -3px 0 #8f7a5c, 0 0 0 2px #dbb15c;
            position: relative;
        }
        .cell.outpost::after {
            content: "⚑";
            position: absolute;
            top: 0px;
            right: 2px;
            font-size: 12px;
            color: #4a3a1a;
            opacity: 0.9;
        }
        .cell.outpost.blue-unit {
            background: #7bb3e0;
            box-shadow: inset 0 -3px 0 #346fa0, 0 0 0 2px #1f4b7a;
        }
        .cell.outpost.red-unit {
            background: #e08484;
            box-shadow: inset 0 -3px 0 #aa4a4a, 0 0 0 2px #852e2e;
        }
        .cell.outpost.blue-unit::after, .cell.outpost.red-unit::after {
            content: "⚑";
            color: gold;
            text-shadow: 0 0 2px black;
        }

        .unit-emoji {
            font-size: 1.5rem;
            line-height: 1;
            filter: drop-shadow(2px 2px 0 rgba(0,0,0,0.3));
        }
        .move-cost-badge {
            position: absolute;
            top: -2px;
            right: -4px;
            background: #222;
            color: white;
            border-radius: 20px;
            font-size: 0.7rem;
            font-weight: bold;
            padding: 2px 5px;
            border: 1px solid gold;
            background: #3f280e;
            z-index: 5;
        }
        .attack-range-badge {
            position: absolute;
            bottom: -2px;
            right: -4px;
            background: #8b0000;
            color: white;
            border-radius: 20px;
            font-size: 0.7rem;
            font-weight: bold;
            padding: 2px 5px;
            border: 1px solid orange;
            z-index: 5;
        }
        .footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-top: 20px;
            color: #e0d7c0;
        }
        .info {
            background: #1a2f38;
            padding: 12px 24px;
            border-radius: 30px;
            font-size: 1.1rem;
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }
        .info span {
            color: #fdd47e;
            font-weight: 600;
        }
        .btn {
            background: #c5a156;
            border: none;
            padding: 10px 32px;
            border-radius: 40px;
            font-weight: 800;
            font-size: 1.3rem;
            color: #202d34;
            box-shadow: 0 6px 0 #7b5d31;
            cursor: pointer;
            transition: 0.06s linear;
        }
        .btn:active {
            transform: translateY(4px);
            box-shadow: 0 2px 0 #7b5d31;
        }
        .btn:disabled {
            opacity: 0.4;
            transform: translateY(2px);
            box-shadow: 0 4px 0 #7b5d31;
            pointer-events: none;
        }
        .victory-banner {
            background: #ca933a;
            color: #212121;
            font-size: 1.8rem;
            font-weight: 800;
            padding: 12px 32px;
            border-radius: 60px;
            text-align: center;
            margin-bottom: 16px;
            border: 3px solid gold;
        }
        .status-message {
            background: #2f1e2e;
            color: #ffb6b6;
            padding: 4px 16px;
            border-radius: 30px;
            font-size: 1rem;
            margin-left: 10px;
        }
    </style>
</head>
<body>
<div class="game-container">
    <div class="header">
        <div class="player-panel">
            <div class="player-info blue">
                <span class="badge">⚔️</span>
                <span>蓝方</span>
                <span id="blueFunds" class="funds">0</span>
                <span id="blueVP" class="victory-points">0</span>
            </div>
            <div class="player-info red">
                <span class="badge">⚔️</span>
                <span>红方</span>
                <span id="redFunds" class="funds">0</span>
                <span id="redVP" class="victory-points">0</span>
            </div>
        </div>
        <div id="turnDisplay" class="turn-indicator">蓝方回合</div>
    </div>

    <div id="victoryMessage" class="victory-banner" style="display: none;">🏆 胜利 🏆</div>

    <div class="board-wrapper">
        <div id="chessboard" class="grid"></div>
    </div>

    <div class="footer">
        <div class="info">
            <div>⚡ 移动: 1/3/5... <span id="moveCountDisplay">0</span></div>
            <div>💰 生产(8费): 点击空格➕ | 新兵⏳当回合不可移动</div>
            <div>⚔️ 攻击(1费): 点击敌方单位 | 概率 2/(距离+1)</div>
            <div><span id="selectedInfo"></span></div>
        </div>
        <button id="endTurnBtn" class="btn">结束回合</button>
    </div>
</div>

<script>
    (function() {
        // ---------- 配置 ----------
        const ROWS = 16;
        const COLS = 18;
        // 初始运兵车坐标
        const BLUE_START = [7, 4];
        const RED_START = [8, 13];

        // 中心对称据点 (三个2x2)
        const OUTPOSTS = [
            { name: 'A', cells: [[4,5],[4,6],[5,5],[5,6]] },
            { name: 'B', cells: [[10,11],[10,12],[11,11],[11,12]] },
            { name: 'C', cells: [[7,8],[7,9],[8,8],[8,9]] }  // 中心自对称
        ];

        // 据点映射
        const outpostCellMap = new Map(); // key "row,col" -> outpostName
        OUTPOSTS.forEach(op => {
            op.cells.forEach(([r,c]) => {
                outpostCellMap.set(`${r},${c}`, op.name);
            });
        });

        // 全局状态
        let board = Array(ROWS).fill().map(() => Array(COLS).fill(null));
        let blueFunds = 0;
        let redFunds = 0;
        let blueVP = 0;
        let redVP = 0;
        let currentPlayer = 'blue';
        let gameOver = false;

        // 选中状态
        let selectedRow = -1, selectedCol = -1;
        // 攻击模式标记(当选中自己的步兵时,可以进入攻击模式)
        let attackMode = false;

        // 生产标记:记录本回合生产的步兵
        let producedThisTurn = new Set();

        // 标记新回合是否已加过分(防止重复加点)
        let hasAddedVPGainThisTurn = false;

        function getRandomFunds() {
            return Math.floor(Math.random() * 10);
        }

        function resetProducedSet() {
            producedThisTurn.clear();
        }

        function resetMoveCountsForTurn() {
            for (let r = 0; r < ROWS; r++) {
                for (let c = 0; c < COLS; c++) {
                    const unit = board[r][c];
                    if (unit) {
                        unit.moveCountThisTurn = 0;
                        // 注意:不重置defeated状态,defeated持续到回合结束判定
                    }
                }
            }
        }

        function createUnit(type, player, row, col) {
            if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return null;
            if (board[row][col] !== null) return null;
            const unit = {
                type: type,
                player: player,
                moveCountThisTurn: 0,
                defeated: false, // 是否被击倒
            };
            board[row][col] = unit;
            return unit;
        }

        function initBoard() {
            for (let r = 0; r < ROWS; r++) {
                for (let c = 0; c < COLS; c++) {
                    board[r][c] = null;
                }
            }
            createUnit('jeep', 'blue', BLUE_START[0], BLUE_START[1]);
            createUnit('jeep', 'red', RED_START[0], RED_START[1]);
        }

        function isWithinJeepRange(jeepRow, jeepCol, targetRow, targetCol) {
            const rowDiff = Math.abs(jeepRow - targetRow);
            const colDiff = Math.abs(jeepCol - targetCol);
            return rowDiff <= 2 && colDiff <= 2;
        }

        function getJeepPos(player) {
            for (let r = 0; r < ROWS; r++) {
                for (let c = 0; c < COLS; c++) {
                    const u = board[r][c];
                    if (u && u.type === 'jeep' && u.player === player && !u.defeated) {
                        return [r, c];
                    }
                }
            }
            return null;
        }

        // 生产步兵
        function tryProduceInfantry(player, row, col) {
            if (gameOver) return false;
            if (player !== currentPlayer) return false;
            const funds = player === 'blue' ? blueFunds : redFunds;
            if (funds < 8) return false;
            if (board[row][col] !== null) return false;

            const jeepPos = getJeepPos(player);
            if (!jeepPos) return false;
            if (!isWithinJeepRange(jeepPos[0], jeepPos[1], row, col)) return false;

            if (player === 'blue') {
                blueFunds -= 8;
            } else {
                redFunds -= 8;
            }
            const newUnit = createUnit('infantry', player, row, col);
            if (newUnit) {
                producedThisTurn.add(newUnit);
            }
            updateUI();
            return true;
        }

        // 单位是否可移动(不能是倒地,不能是本回合生产步兵)
        function canUnitMove(unit) {
            if (!unit) return false;
            if (unit.defeated) return false; // 倒地无法行动
            if (unit.type === 'jeep') return true;
            return !producedThisTurn.has(unit);
        }

        // 移动单位
        function tryMoveUnit(player, fromRow, fromCol, toRow, toCol) {
            if (gameOver) return false;
            if (player !== currentPlayer) return false;
            const unit = board[fromRow][fromCol];
            if (!unit || unit.player !== player) return false;
            if (!canUnitMove(unit)) {
                alert(unit.defeated ? '单位已被击倒,无法移动' : '本回合生产的步兵还不能移动');
                return false;
            }
            if (board[toRow][toCol] !== null) return false;
            const rowDiff = Math.abs(fromRow - toRow);
            const colDiff = Math.abs(fromCol - toCol);
            if (rowDiff + colDiff !== 1) return false;

            const nextMoveNumber = unit.moveCountThisTurn + 1;
            const moveCost = 2 * nextMoveNumber - 1;

            const playerFunds = player === 'blue' ? blueFunds : redFunds;
            if (playerFunds < moveCost) return false;

            if (player === 'blue') {
                blueFunds -= moveCost;
            } else {
                redFunds -= moveCost;
            }
            board[toRow][toCol] = unit;
            board[fromRow][fromCol] = null;
            unit.moveCountThisTurn += 1;

            if (selectedRow === fromRow && selectedCol === fromCol) {
                selectedRow = toRow;
                selectedCol = toCol;
            }
            updateUI();
            return true;
        }

        // 攻击:步兵对7*7范围内敌方单位
        function tryAttack(attackerRow, attackerCol, targetRow, targetCol) {
            if (gameOver) return false;

            const attacker = board[attackerRow][attackerCol];
            if (!attacker || attacker.type !== 'infantry' || attacker.player !== currentPlayer) return false;
            if (attacker.defeated) {
                alert('攻击者已被击倒');
                return false;
            }

            const target = board[targetRow][targetCol];
            if (!target || target.player === currentPlayer) return false; // 不能攻击己方
            if (target.defeated) {
                alert('目标已被击倒');
                return false;
            }

            // 检查范围:7*7,即切比雪夫距离≤3
            const rowDiff = Math.abs(attackerRow - targetRow);
            const colDiff = Math.abs(attackerCol - targetCol);
            if (rowDiff > 3 || colDiff > 3) {
                alert('目标超出攻击范围(7x7内)');
                return false;
            }

            // 消耗1费
            const playerFunds = currentPlayer === 'blue' ? blueFunds : redFunds;
            if (playerFunds < 1) {
                alert('费用不足1点');
                return false;
            }

            // 计算击倒概率:2/(曼哈顿距离+1)
            const manhattanDist = rowDiff + colDiff;
            const probability = 2 / (manhattanDist + 1); // 2/(d+1)
            const roll = Math.random();

            // 扣费
            if (currentPlayer === 'blue') {
                blueFunds -= 1;
            } else {
                redFunds -= 1;
            }

            let attackSuccess = false;
            if (roll < probability) {
                // 击倒
                target.defeated = true;
                attackSuccess = true;
                alert(`攻击成功!击倒概率 ${probability.toFixed(2)},骰子: ${roll.toFixed(2)},目标被击倒`);
            } else {
                alert(`攻击失败!击倒概率 ${probability.toFixed(2)},骰子: ${roll.toFixed(2)}`);
            }

            // 退出攻击模式
            attackMode = false;
            // 不清空选中,但可保留
            updateUI();
            return true;
        }

        // 回合结束时处理**当前玩家**的倒地单位:1/2概率死亡,否则恢复正常
        function processDefeatedUnitsForPlayer(player) {
            for (let r = 0; r < ROWS; r++) {
                for (let c = 0; c < COLS; c++) {
                    const unit = board[r][c];
                    if (unit && unit.player === player && unit.defeated) {
                        // 1/2概率死亡
                        if (Math.random() < 0.5) {
                            // 死亡,移除单位
                            board[r][c] = null;
                        } else {
                            // 恢复正常
                            unit.defeated = false;
                        }
                    }
                }
            }
        }

        // 计算当前玩家应得的胜利点(每个友方单位在据点上得1点)
        function calculateVPGainForPlayer(player) {
            let gain = 0;
            for (let [cellKey, opName] of outpostCellMap.entries()) {
                const [r, c] = cellKey.split(',').map(Number);
                const unit = board[r][c];
                if (unit && unit.player === player && !unit.defeated) { // 倒地单位不提供胜利点
                    gain += 1;
                }
            }
            return gain;
        }

        // 己方回合开始时加点
        function addVictoryPointsForCurrentPlayer() {
            if (gameOver) return;
            const gain = calculateVPGainForPlayer(currentPlayer);
            if (gain > 0) {
                if (currentPlayer === 'blue') {
                    blueVP += gain;
                } else {
                    redVP += gain;
                }
            }
        }

        function checkVictory() {
            if (blueVP >= 50) {
                gameOver = true;
                document.getElementById('victoryMessage').style.display = 'block';
                document.getElementById('victoryMessage').innerText = '🏆 蓝方胜利! 🏆';
                return true;
            } else if (redVP >= 50) {
                gameOver = true;
                document.getElementById('victoryMessage').style.display = 'block';
                document.getElementById('victoryMessage').innerText = '🏆 红方胜利! 🏆';
                return true;
            }
            return false;
        }

        // 开始新回合
        function startNewTurn() {
            resetMoveCountsForTurn();
            resetProducedSet();

            if (currentPlayer === 'blue') {
                blueFunds = getRandomFunds();
            } else {
                redFunds = getRandomFunds();
            }

            hasAddedVPGainThisTurn = false;
        }

        // 结束回合
        function endTurn() {
            if (gameOver) return;

            // 处理**当前玩家**的倒地单位(回合结束时)
            processDefeatedUnitsForPlayer(currentPlayer);

            // 清空当前玩家费用
            if (currentPlayer === 'blue') {
                blueFunds = 0;
            } else {
                redFunds = 0;
            }

            // 切换玩家
            currentPlayer = currentPlayer === 'blue' ? 'red' : 'blue';

            // 准备新回合
            startNewTurn();

            // 清空选中和攻击模式
            selectedRow = -1; selectedCol = -1;
            attackMode = false;

            // 新回合加点
            if (!hasAddedVPGainThisTurn) {
                addVictoryPointsForCurrentPlayer();
                hasAddedVPGainThisTurn = true;
            }

            checkVictory();
            updateUI();
        }

        // 确保回合开始加点
        function ensureVPGainAdded() {
            if (gameOver) return false;
            if (!hasAddedVPGainThisTurn) {
                addVictoryPointsForCurrentPlayer();
                hasAddedVPGainThisTurn = true;
                if (checkVictory()) {
                    updateUI();
                    return true;
                }
                updateUI();
            }
            return false;
        }

        // 单元格点击处理
        function onCellClick(row, col) {
            if (gameOver) return;

            // 确保加点
            if (ensureVPGainAdded()) return;

            const unit = board[row][col];
            const currentUnit = (selectedRow !== -1 && selectedCol !== -1) ? board[selectedRow][selectedCol] : null;

            // 攻击模式:如果选中了自己的步兵且点击了敌方单位,尝试攻击
            if (attackMode && currentUnit && currentUnit.player === currentPlayer && currentUnit.type === 'infantry' && !currentUnit.defeated) {
                if (unit && unit.player !== currentPlayer && !unit.defeated) {
                    tryAttack(selectedRow, selectedCol, row, col);
                    return;
                } else {
                    // 点击无效,退出攻击模式
                    attackMode = false;
                    // 但不取消选中
                }
            }

            // 如果当前有选中单位且是当前玩家单位
            if (selectedRow !== -1 && selectedCol !== -1 && currentUnit && currentUnit.player === currentPlayer) {
                // 如果点击的是自己的单位
                if (unit && unit.player === currentPlayer) {
                    // 切换选中
                    selectedRow = row;
                    selectedCol = col;
                    attackMode = false; // 退出攻击模式
                    updateUI();
                    return;
                }
                // 如果点击空格,尝试移动
                if (!unit) {
                    tryMoveUnit(currentPlayer, selectedRow, selectedCol, row, col);
                    return;
                }
                // 如果点击敌方单位且当前选中是步兵,进入攻击模式(但还没攻击,需要再点一次)
                if (unit && unit.player !== currentPlayer && currentUnit.type === 'infantry' && !currentUnit.defeated) {
                    // 进入攻击模式,高亮攻击范围
                    attackMode = true;
                    updateUI();
                    return;
                }
                // 其他情况清空选中
                selectedRow = -1;
                selectedCol = -1;
                attackMode = false;
                updateUI();
                return;
            }

            // 没有有效选中:如果点击自己的单位,选中它
            if (unit && unit.player === currentPlayer && !unit.defeated) {
                selectedRow = row;
                selectedCol = col;
                attackMode = false;
                updateUI();
                return;
            }

            // 点击空格,尝试生产
            if (!unit) {
                tryProduceInfantry(currentPlayer, row, col);
                return;
            }

            // 其他情况(点击敌方单位等)清空选中
            selectedRow = -1;
            selectedCol = -1;
            attackMode = false;
            updateUI();
        }

        function renderBoard() {
            const gridEl = document.getElementById('chessboard');
            gridEl.innerHTML = '';

            for (let r = 0; r < ROWS; r++) {
                for (let c = 0; c < COLS; c++) {
                    const cell = document.createElement('div');
                    cell.className = 'cell';

                    const cellKey = `${r},${c}`;
                    if (outpostCellMap.has(cellKey)) {
                        cell.classList.add('outpost');
                    }

                    const unit = board[r][c];
                    if (unit) {
                        cell.classList.add(unit.player === 'blue' ? 'blue-unit' : 'red-unit');
                        if (unit.defeated) {
                            cell.classList.add('defeated');
                        }
                        const emoji = unit.type === 'jeep' ? '🚛' : '🚶';
                        cell.innerHTML = `<span class="unit-emoji">${emoji}</span>`;

                        if (!unit.defeated && unit.moveCountThisTurn > 0) {
                            const nextCost = 2 * (unit.moveCountThisTurn + 1) - 1;
                            const costDiv = document.createElement('span');
                            costDiv.className = 'move-cost-badge';
                            costDiv.textContent = nextCost;
                            cell.appendChild(costDiv);
                        }

                        if (unit.type === 'infantry' && producedThisTurn.has(unit) && !unit.defeated) {
                            const hourglass = document.createElement('span');
                            hourglass.style.position = 'absolute';
                            hourglass.style.bottom = '-2px';
                            hourglass.style.left = '2px';
                            hourglass.style.fontSize = '0.7rem';
                            hourglass.style.filter = 'drop-shadow(0 0 2px black)';
                            hourglass.textContent = '⏳';
                            cell.appendChild(hourglass);
                        }
                    }

                    // 高亮选中的单元格
                    if (selectedRow === r && selectedCol === c) {
                        cell.classList.add('highlight');
                    }

                    // 攻击模式高亮:显示攻击范围(7x7)内的敌方单位
                    if (attackMode && selectedRow !== -1 && selectedCol !== -1) {
                        const attacker = board[selectedRow][selectedCol];
                        if (attacker && attacker.player === currentPlayer && attacker.type === 'infantry' && !attacker.defeated) {
                            const rowDiff = Math.abs(selectedRow - r);
                            const colDiff = Math.abs(selectedCol - c);
                            if (rowDiff <= 3 && colDiff <= 3) {
                                // 在7x7内
                                if (unit && unit.player !== currentPlayer && !unit.defeated) {
                                    cell.classList.add('highlight'); // 可攻击目标高亮
                                }
                            }
                        }
                    }

                    // 移动高亮
                    if (!attackMode && selectedRow !== -1 && selectedCol !== -1 && !gameOver) {
                        const selUnit = board[selectedRow][selectedCol];
                        if (selUnit && selUnit.player === currentPlayer && canUnitMove(selUnit)) {
                            const dr = [0,1,0,-1], dc = [1,0,-1,0];
                            for (let d=0; d<4; d++) {
                                const nr = selectedRow + dr[d];
                                const nc = selectedCol + dc[d];
                                if (nr===r && nc===c && board[nr][nc]===null) {
                                    cell.classList.add('highlight');
                                }
                            }
                        }
                    }

                    cell.dataset.row = r;
                    cell.dataset.col = c;
                    cell.addEventListener('click', (event) => {
                        const row = parseInt(event.currentTarget.dataset.row);
                        const col = parseInt(event.currentTarget.dataset.col);
                        onCellClick(row, col);
                    });
                    gridEl.appendChild(cell);
                }
            }

            // 更新UI文字
            document.getElementById('blueFunds').innerText = blueFunds;
            document.getElementById('redFunds').innerText = redFunds;
            document.getElementById('blueVP').innerText = blueVP;
            document.getElementById('redVP').innerText = redVP;
            document.getElementById('turnDisplay').innerText = currentPlayer === 'blue' ? '🔵 蓝方回合' : '🔴 红方回合';

            // 显示选中信息
            let selectedInfo = '';
            if (selectedRow !== -1 && selectedCol !== -1 && board[selectedRow][selectedCol]) {
                const u = board[selectedRow][selectedCol];
                if (u.player === currentPlayer) {
                    if (u.type === 'infantry' && !u.defeated) {
                        selectedInfo = `选中步兵 ${attackMode ? '(攻击模式)' : ''}`;
                    } else if (u.type === 'jeep') {
                        selectedInfo = '选中运兵车';
                    }
                }
            }
            document.getElementById('selectedInfo').innerText = selectedInfo;

            // 移动次数显示
            if (selectedRow !== -1 && selectedCol !== -1 && board[selectedRow][selectedCol]) {
                const u = board[selectedRow][selectedCol];
                if (u.player === currentPlayer && !u.defeated) {
                    const nextMove = 2 * (u.moveCountThisTurn + 1) - 1;
                    document.getElementById('moveCountDisplay').innerText = `${u.moveCountThisTurn} 次 / 下次${nextMove}费`;
                } else {
                    document.getElementById('moveCountDisplay').innerText = `0`;
                }
            } else {
                document.getElementById('moveCountDisplay').innerText = `0`;
            }

            document.getElementById('endTurnBtn').disabled = gameOver;
        }

        function updateUI() {
            renderBoard();
        }

        function startGame() {
            initBoard();
            currentPlayer = 'blue';
            blueFunds = getRandomFunds();
            redFunds = 0;
            blueVP = 0;
            redVP = 0;
            gameOver = false;
            document.getElementById('victoryMessage').style.display = 'none';
            resetMoveCountsForTurn();
            resetProducedSet();
            selectedRow = -1; selectedCol = -1;
            attackMode = false;

            hasAddedVPGainThisTurn = false;
            ensureVPGainAdded();

            updateUI();
        }

        document.getElementById('endTurnBtn').addEventListener('click', () => {
            endTurn();
        });

        startGame();
    })();
</script>
</body>
</html>