C++<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>三国杀 - 在线对战版 (修复版)</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;700&display=swap');
body {
font-family: 'Noto Serif SC', serif;
background-color: #1a1a1a;
color: #e5e5e5;
overflow: hidden;
}
/* 自定义滚动条 */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: #2d2d2d; }
::-webkit-scrollbar-thumb { background: #8b5cf6; border-radius: 4px; }
/* 卡牌样式 */
.card {
transition: transform 0.2s, box-shadow 0.2s;
cursor: pointer;
position: relative;
background-size: cover;
background-position: center;
user-select: none;
}
.card:hover {
transform: translateY(-10px) scale(1.05);
box-shadow: 0 10px 20px rgba(0,0,0,0.5);
z-index: 10;
}
.card.selected {
transform: translateY(-15px);
border: 2px solid #fbbf24;
box-shadow: 0 0 15px #fbbf24;
z-index: 20;
}
/* 玩家区域 */
.player-area {
transition: all 0.3s ease;
}
.player-area.active-turn {
box-shadow: 0 0 20px rgba(139, 92, 246, 0.5);
border-color: #8b5cf6;
}
/* 动画效果 */
@keyframes damage-shake {
0% { transform: translate(1px, 1px) rotate(0deg); }
10% { transform: translate(-1px, -2px) rotate(-1deg); }
20% { transform: translate(-3px, 0px) rotate(1deg); }
30% { transform: translate(3px, 2px) rotate(0deg); }
40% { transform: translate(1px, -1px) rotate(1deg); }
50% { transform: translate(-1px, 2px) rotate(-1deg); }
60% { transform: translate(-3px, 1px) rotate(0deg); }
70% { transform: translate(3px, 1px) rotate(-1deg); }
80% { transform: translate(-1px, -1px) rotate(1deg); }
90% { transform: translate(1px, 2px) rotate(0deg); }
100% { transform: translate(1px, -2px) rotate(-1deg); }
}
.damage-effect {
animation: damage-shake 0.5s;
color: #ef4444;
font-weight: bold;
text-shadow: 0 0 5px #ef4444;
}
@keyframes heal-pulse {
0% { transform: scale(1); color: #10b981; }
50% { transform: scale(1.2); color: #34d399; }
100% { transform: scale(1); color: #10b981; }
}
.heal-effect {
animation: heal-pulse 0.5s;
}
.bg-texture {
background-image: radial-gradient(#2d2d2d 1px, transparent 1px);
background-size: 20px 20px;
}
</style>
</head>
<body class="h-screen w-screen flex flex-col bg-texture">
<!-- 顶部导航栏 -->
<header class="h-16 bg-gray-900 border-b border-gray-700 flex items-center justify-between px-6 shadow-lg z-20">
<div class="flex items-center gap-3">
<i class="fa-solid fa-dragon text-purple-500 text-2xl"></i>
<h1 class="text-xl font-bold tracking-wider text-gray-100">三国杀 <span class="text-xs text-gray-400 font-normal">Web Demo</span></h1>
</div>
<div class="flex items-center gap-4">
<div id="game-status" class="px-3 py-1 rounded-full bg-gray-800 text-sm text-gray-300 border border-gray-600">
等待开始...
</div>
<button onclick="game.initGame()" class="px-4 py-2 bg-purple-600 hover:bg-purple-700 text-white rounded-md transition shadow-md text-sm font-bold">
<i class="fa-solid fa-play mr-2"></i>新游戏
</button>
</div>
</header>
<!-- 游戏主区域 -->
<main class="flex-1 relative flex flex-col p-4 gap-4 overflow-hidden">
<!-- 对手区域 (上方) -->
<div id="opponents-container" class="flex-1 flex justify-center items-start gap-8 pt-2">
<!-- 动态生成对手 -->
</div>
<!-- 中央战场/日志区 -->
<div class="h-32 bg-gray-800/50 rounded-lg border border-gray-700 backdrop-blur-sm flex flex-col relative overflow-hidden">
<div class="absolute top-0 left-0 w-full h-full bg-black/20 pointer-events-none"></div>
<div class="p-2 text-xs text-gray-500 border-b border-gray-700/50 flex justify-between">
<span><i class="fa-solid fa-scroll mr-1"></i>战况记录</span>
<span id="deck-info">牌堆剩余: 0</span>
</div>
<div id="game-log" class="flex-1 overflow-y-auto p-3 text-sm space-y-1 font-mono">
<div class="text-gray-400 italic">欢迎来到三国杀,点击“新游戏”开始对局...</div>
</div>
</div>
<!-- 玩家区域 (下方) -->
<div id="player-container" class="h-64 bg-gray-900/80 rounded-xl border border-gray-600 p-4 flex gap-6 shadow-2xl relative player-area">
<!-- 玩家信息 -->
<div class="w-48 flex flex-col items-center justify-center border-r border-gray-700 pr-4">
<div class="relative w-24 h-24 mb-2">
<img id="player-avatar" src="" alt="玩家头像" class="w-full h-full rounded-full object-cover border-4 border-gray-700 shadow-lg">
<div id="player-role-badge" class="absolute -bottom-2 -right-2 bg-gray-800 text-xs px-2 py-1 rounded border border-gray-600">身份</div>
</div>
<h2 id="player-name" class="text-lg font-bold text-white">主公</h2>
<div class="flex items-center gap-1 mt-2 text-red-500">
<i class="fa-solid fa-heart"></i>
<span id="player-hp" class="text-xl font-bold">4</span>
<span class="text-gray-500 text-sm">/ <span id="player-max-hp">4</span></span>
</div>
</div>
<!-- 手牌区域 -->
<div class="flex-1 flex flex-col">
<div class="flex justify-between items-center mb-2">
<span class="text-gray-400 text-sm"><i class="fa-solid fa-hand mr-1"></i>手牌 (<span id="hand-count">0</span>)</span>
<div id="action-buttons" class="hidden gap-2">
<button onclick="game.playSelectedCard()" class="px-3 py-1 bg-yellow-600 hover:bg-yellow-500 text-white text-xs rounded shadow transition">出牌</button>
<button onclick="game.discardSelectedCard()" class="px-3 py-1 bg-gray-600 hover:bg-gray-500 text-white text-xs rounded shadow transition">弃置</button>
</div>
</div>
<div id="hand-cards" class="flex-1 flex items-end justify-center gap-[-20px] pb-2 overflow-x-auto px-4">
<!-- 手牌动态生成 -->
</div>
</div>
<!-- 控制区 -->
<div class="w-32 flex flex-col justify-center items-center gap-3 border-l border-gray-700 pl-4">
<div id="phase-indicator" class="text-center">
<div class="text-xs text-gray-500 uppercase">当前阶段</div>
<div id="current-phase" class="text-purple-400 font-bold">准备阶段</div>
</div>
<button id="end-turn-btn" onclick="game.endTurn()" disabled class="w-full py-2 bg-gray-700 text-gray-500 rounded cursor-not-allowed transition font-bold">
结束回合
</button>
</div>
</div>
</main>
<!-- 模态框: 游戏结束 -->
<div id="game-over-modal" class="fixed inset-0 bg-black/80 z-50 hidden flex items-center justify-center backdrop-blur-sm">
<div class="bg-gray-800 p-8 rounded-xl border border-purple-500 shadow-2xl max-w-md w-full text-center transform transition-all scale-100">
<i class="fa-solid fa-trophy text-5xl text-yellow-500 mb-4"></i>
<h2 id="game-over-title" class="text-3xl font-bold text-white mb-2">游戏结束</h2>
<p id="game-over-message" class="text-gray-300 mb-6">胜负已分</p>
<button onclick="location.reload()" class="px-6 py-3 bg-purple-600 hover:bg-purple-700 text-white rounded-lg font-bold shadow-lg transition">
再来一局
</button>
</div>
</div>
<script>
/**
* 三国杀简化版核心逻辑
* 包含:卡牌系统、武将系统、回合流程、基础AI
*/
// --- 数据定义 ---
const CARD_TYPES = {
BASIC: 'basic',
SCROLL: 'scroll',
EQUIP: 'equip'
};
const CARDS_DB = [
{ id: 'sha', name: '杀', type: CARD_TYPES.BASIC, suit: 'spade', point: 7, img: 'https://picsum.photos/100/150?random=sha', desc: '对一名角色造成1点伤害' },
{ id: 'shan', name: '闪', type: CARD_TYPES.BASIC, suit: 'heart', point: 5, img: 'https://picsum.photos/100/150?random=shan', desc: '抵消一张杀' },
{ id: 'tao', name: '桃', type: CARD_TYPES.BASIC, suit: 'heart', point: 10, img: 'https://picsum.photos/100/150?random=tao', desc: '回复1点体力' },
{ id: 'juedou', name: '决斗', type: CARD_TYPES.SCROLL, suit: 'spade', point: 1, img: 'https://picsum.photos/100/150?random=juedou', desc: '轮流出杀,先不出者受伤' },
{ id: 'nanman', name: '南蛮入侵', type: CARD_TYPES.SCROLL, suit: 'spade', point: 7, img: 'https://picsum.photos/100/150?random=nanman', desc: '所有其他角色需出杀' },
{ id: 'wanjian', name: '万箭齐发', type: CARD_TYPES.SCROLL, suit: 'heart', point: 1, img: 'https://picsum.photos/100/150?random=wanjian', desc: '所有其他角色需出闪' },
{ id: 'wuzhong', name: '无中生有', type: CARD_TYPES.SCROLL, suit: 'heart', point: 11, img: 'https://picsum.photos/100/150?random=wuzhong', desc: '摸两张牌' },
{ id: 'guohe', name: '过河拆桥', type: CARD_TYPES.SCROLL, suit: 'spade', point: 12, img: 'https://picsum.photos/100/150?random=guohe', desc: '拆除一名角色的一张牌' },
{ id: 'shunshou', name: '顺手牵羊', type: CARD_TYPES.SCROLL, suit: 'spade', point: 3, img: 'https://picsum.photos/100/150?random=shunshou', desc: '获得一名角色的一张牌' },
{ id: 'bagua', name: '八卦阵', type: CARD_TYPES.EQUIP, subType: 'armor', img: 'https://picsum.photos/100/150?random=bagua', desc: '出闪时判定红色视为出闪' },
{ id: 'chitu', name: '赤兔', type: CARD_TYPES.EQUIP, subType: 'horse', img: 'https://picsum.photos/100/150?random=chitu', desc: '-1马' }
];
const GENERALS = [
{ id: 'caocao', name: '曹操', hp: 4, maxHp: 4, avatar: 'https://picsum.photos/200/200?random=caocao', skill: '奸雄' },
{ id: 'liubei', name: '刘备', hp: 4, maxHp: 4, avatar: 'https://picsum.photos/200/200?random=liubei', skill: '仁德' },
{ id: 'sunquan', name: '孙权', hp: 4, maxHp: 4, avatar: 'https://picsum.photos/200/200?random=sunquan', skill: '制衡' },
{ id: 'zhugeliang', name: '诸葛亮', hp: 3, maxHp: 3, avatar: 'https://picsum.photos/200/200?random=zhuge', skill: '观星' },
{ id: 'lvbu', name: '吕布', hp: 5, maxHp: 5, avatar: 'https://picsum.photos/200/200?random=lvbu', skill: '无双' }
];
const ROLES = {
MASTER: { name: '主公', color: 'text-yellow-500' },
LOYALIST: { name: '忠臣', color: 'text-red-500' },
REBEL: { name: '反贼', color: 'text-green-500' },
RENEGADE: { name: '内奸', color: 'text-purple-500' }
};
// --- 类定义 ---
class Card {
constructor(template) {
this.id = template.id;
this.name = template.name;
this.type = template.type;
this.suit = template.suit || '';
this.point = template.point || 0;
this.img = template.img;
this.desc = template.desc;
this.subType = template.subType; // for equip
this.uuid = Math.random().toString(36).substr(2, 9);
}
}
class Player {
constructor(id, general, isHuman = false) {
this.id = id;
this.general = general;
this.hp = general.hp;
this.maxHp = general.maxHp;
this.isHuman = isHuman;
this.hand = [];
this.equips = { weapon: null, armor: null, horse: null };
this.role = null;
this.isDead = false;
this.status = { poisoned: false, chained: false };
}
drawCards(count, deck) {
for (let i = 0; i < count; i++) {
if (deck.length > 0) {
this.hand.push(deck.pop());
}
}
game.ui.updateHand(this);
}
loseHp(amount, source) {
this.hp -= amount;
game.ui.showDamageEffect(this.id, amount);
game.log(`${this.general.name} 受到 ${amount} 点伤害`);
if (this.hp <= 0) {
this.dying(source);
} else {
game.ui.updatePlayerInfo(this);
}
}
recoverHp(amount) {
if (this.hp < this.maxHp) {
this.hp = Math.min(this.hp + amount, this.maxHp);
game.ui.showHealEffect(this.id);
game.log(`${this.general.name} 回复了 ${amount} 点体力`);
game.ui.updatePlayerInfo(this);
}
}
dying(source) {
// 简化版:直接死亡,不处理求桃流程
game.log(`${this.general.name} 阵亡!身份是:${this.role.name}`);
this.isDead = true;
game.ui.updatePlayerInfo(this);
game.checkWinCondition();
}
}
class GameEngine {
constructor() {
this.players = [];
this.deck = [];
this.discardPile = [];
this.currentPlayerIndex = 0;
this.phase = 'ready'; // ready, judge, draw, play, discard, end
this.selectedCardIndex = -1;
this.isGameOver = false;
this.ui = new UIManager(this);
}
initGame() {
this.isGameOver = false;
this.players = [];
this.deck = this.createDeck();
this.shuffleDeck();
// 1. 分配身份 (简化:1主 1忠 2反 1内 -> 5人局)
const roles = [ROLES.MASTER, ROLES.LOYALIST, ROLES.REBEL, ROLES.REBEL, ROLES.RENEGADE];
roles.sort(() => Math.random() - 0.5);
// 2. 选将 (随机)
const generals = [...GENERALS].sort(() => Math.random() - 0.5);
// 3. 创建玩家
for (let i = 0; i < 5; i++) {
const isHuman = i === 0;
const p = new Player(i, generals[i], isHuman);
p.role = roles[i];
this.players.push(p);
}
// 4. 发初始手牌 (4张)
this.players.forEach(p => p.drawCards(4, this.deck));
// 5. UI初始化
this.ui.renderOpponents(this.players.slice(1));
this.ui.updatePlayerInfo(this.players[0]);
this.log("游戏开始!");
// 6. 开始第一回合
this.startTurn(0);
}
createDeck() {
let deck = [];
// 简单模拟牌堆数量
for(let i=0; i<4; i++) { // 4副牌
CARDS_DB.forEach(cardTemplate => {
// 基本牌多放点
if(cardTemplate.type === CARD_TYPES.BASIC) {
deck.push(new Card(cardTemplate));
deck.push(new Card(cardTemplate));
} else {
deck.push(new Card(cardTemplate));
}
});
}
return deck;
}
shuffleDeck() {
for (let i = this.deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.deck[i], this.deck[j]] = [this.deck[j], this.deck[i]];
}
this.ui.updateDeckInfo(this.deck.length);
}
startTurn(playerIndex) {
if (this.isGameOver) return;
this.currentPlayerIndex = playerIndex;
const player = this.players[playerIndex];
if (player.isDead) {
this.nextTurn();
return;
}
this.phase = 'start';
this.ui.highlightActivePlayer(playerIndex);
this.log(`--- ${player.general.name} 的回合开始 ---`);
// 判定阶段 (简化跳过)
// 摸牌阶段
setTimeout(() => {
this.phase = 'draw';
player.drawCards(2, this.deck);
this.log(`${player.general.name} 摸了两张牌`);
// 出牌阶段
setTimeout(() => {
this.phase = 'play';
this.ui.setPhaseText("出牌阶段");
if (player.isHuman) {
this.ui.enableControls(true);
} else {
this.ui.enableControls(false);
this.aiPlay(player);
}
}, 800);
}, 800);
}
aiPlay(player) {
// 极简AI:有杀出杀,有桃吃桃,无中生有
let acted = true;
while(acted && !player.isDead) {
acted = false;
// 1. 吃桃
const taoIndex = player.hand.findIndex(c => c.id === 'tao');
if (taoIndex !== -1 && player.hp < player.maxHp) {
this.useCard(player, taoIndex, player);
acted = true;
continue;
}
// 2. 无中生有
const wuzhongIndex = player.hand.findIndex(c => c.id === 'wuzhong');
if (wuzhongIndex !== -1) {
this.useCard(player, wuzhongIndex, player);
acted = true;
continue;
}
// 3. 出杀 (攻击下家)
const shaIndex = player.hand.findIndex(c => c.id === 'sha');
if (shaIndex !== -1) {
// 找活着的下家
let target = this.getNextAlivePlayer(player.id);
if (target) {
this.useCard(player, shaIndex, target);
acted = true;
continue;
}
}
// 4. 南蛮/万箭 (简化:直接对所有其他人造成伤害,不处理响应)
const aoeIndex = player.hand.findIndex(c => c.id === 'nanman' || c.id === 'wanjian');
if (aoeIndex !== -1) {
this.useCard(player, aoeIndex, null); // AOE特殊处理
acted = true;
continue;
}
}
// AI结束回合
setTimeout(() => {
this.endTurn();
}, 1000);
}
getNextAlivePlayer(currentId) {
for(let i=1; i<this.players.length; i++) {
let idx = (currentId + i) % this.players.length;
if (!this.players[idx].isDead) return this.players[idx];
}
return null;
}
// --- 新增/修复的方法 ---
playSelectedCard() {
if (this.selectedCardIndex === -1) return;
const player = this.players[this.currentPlayerIndex];
if (!player.isHuman) return;
const card = player.hand[this.selectedCardIndex];
// 简单的合法性检查
if (card.id === 'shan') {
this.log("【闪】不能主动使用");
return;
}
// 确定目标
let target = null;
if (card.id === 'sha') {
target = this.getNextAlivePlayer(player.id);
if (!target) {
this.log("没有可攻击的目标");
return;
}
} else if (['tao', 'wuzhong'].includes(card.id)) {
target = player;
} else if (['nanman', 'wanjian'].includes(card.id)) {
target = null; // AOE
} else {
// 其他锦囊默认对下家使用(简化)
target = this.getNextAlivePlayer(player.id);
}
this.useCard(player, this.selectedCardIndex, target);
this.selectedCardIndex = -1;
this.ui.enableControls(true); // 刷新状态
}
discardSelectedCard() {
if (this.selectedCardIndex === -1) return;
const player = this.players[this.currentPlayerIndex];
if (!player.isHuman) return;
const card = player.hand.splice(this.selectedCardIndex, 1)[0];
this.discardPile.push(card);
this.log(`${player.general.name} 弃置了 ${card.name}`);
this.selectedCardIndex = -1;
this.ui.updateHand(player);
this.ui.enableControls(true);
}
useCard(user, cardIndex, target) {
if (cardIndex >= user.hand.length) return;
const card = user.hand[cardIndex];
// 移除手牌
user.hand.splice(cardIndex, 1);
this.discardPile.push(card);
this.ui.updateHand(user);
this.log(`${user.general.name} 使用了 【${card.name}】`);
// 效果结算
switch(card.id) {
case 'sha':
if (target) {
this.log(`目标:${target.general.name}`);
// 简化:目标50%概率出闪
const hasShan = target.hand.findIndex(c => c.id === 'shan');
if (hasShan !== -1 && Math.random() > 0.3) {
target.hand.splice(hasShan, 1);
this.log(`${target.general.name} 打出 【闪】 抵消了伤害`);
this.ui.updateHand(target);
} else {
target.loseHp(1, user);
}
}
break;
case 'tao':
user.recoverHp(1);
break;
case 'wuzhong':
user.drawCards(2, this.deck);
break;
case 'nanman':
case 'wanjian':
this.log("全场受到伤害(简化版)");
this.players.forEach(p => {
if (p.id !== user.id && !p.isDead) {
p.loseHp(1, user);
}
});
break;
case 'guohe':
if(target && target.hand.length > 0) {
const removed = target.hand.pop();
this.log(`拆除了 ${target.general.name} 的 ${removed.name}`);
this.ui.updateHand(target);
}
break;
default:
this.log("该卡牌效果暂未实装");
}
}
endTurn() {
this.ui.enableControls(false);
this.ui.setPhaseText("弃牌阶段");
const player = this.players[this.currentPlayerIndex];
// 弃牌逻辑
while(player.hand.length > player.hp) {
// 自动弃置最后一张
const card = player.hand.pop();
this.discardPile.push(card);
this.log(`${player.general.name} 弃置了 ${card.name}`);
}
this.ui.updateHand(player);
this.nextTurn();
}
nextTurn() {
let nextIndex = (this.currentPlayerIndex + 1) % this.players.length;
// 跳过死人
while(this.players[nextIndex].isDead) {
nextIndex = (nextIndex + 1) % this.players.length;
}
this.startTurn(nextIndex);
}
checkWinCondition() {
const master = this.players.find(p => p.role === ROLES.MASTER);
const rebels = this.players.filter(p => p.role === ROLES.REBEL);
const loyalists = this.players.filter(p => p.role === ROLES.LOYALIST);
const renegade = this.players.find(p => p.role === ROLES.RENEGADE);
// 主公死
if (master.isDead) {
// 如果内奸也死了或者还有其他反贼,反贼胜?简化:只要主公死,反贼胜(除非只剩内奸)
const aliveRebels = rebels.filter(p => !p.isDead).length;
const aliveLoyalists = loyalists.filter(p => !p.isDead).length;
const isRenegadeAlive = !renegade.isDead;
if (aliveRebels > 0 || aliveLoyalists > 0) {
this.gameOver("反贼胜利!");
} else if (isRenegadeAlive) {
this.gameOver("内奸胜利!"); // 实际上内奸要先杀光其他人再单挑主公,这里简化
}
} else {
// 反贼全死
const aliveRebels = rebels.filter(p => !p.isDead).length;
if (aliveRebels === 0) {
this.gameOver("主公方胜利!");
}
}
}
gameOver(msg) {
this.isGameOver = true;
this.ui.showGameOver(msg);
}
log(msg) {
const logEl = document.getElementById('game-log');
const line = document.createElement('div');
line.className = "border-l-2 border-gray-600 pl-2 py-1";
line.innerHTML = `<span class="text-gray-500 text-xs">[${new Date().toLocaleTimeString()}]</span> ${msg}`;
logEl.appendChild(line);
logEl.scrollTop = logEl.scrollHeight;
}
}
class UIManager {
constructor(game) {
this.game = game;
}
renderOpponents(opponents) {
const container = document.getElementById('opponents-container');
container.innerHTML = '';
opponents.forEach(p => {
const div = document.createElement('div');
div.id = `player-${p.id}`;
div.className = "player-area w-32 bg-gray-800 rounded-lg p-2 border border-gray-700 flex flex-col items-center opacity-90";
div.innerHTML = `
<div class="relative w-16 h-16 mb-1">
<img src="${p.general.avatar}" alt="${p.general.name}头像" class="w-full h-full rounded-full object-cover border-2 border-gray-600">
<div class="absolute -bottom-1 -right-1 bg-gray-900 text-[10px] px-1 rounded border border-gray-600 text-gray-400">???</div>
</div>
<div class="text-sm font-bold text-gray-300">${p.general.name}</div>
<div class="text-xs text-red-400 mt-1">HP: <span id="hp-${p.id}">${p.hp}</span></div>
<div class="text-[10px] text-gray-500 mt-1">手牌: <span id="hand-count-${p.id}">${p.hand.length}</span></div>
`;
container.appendChild(div);
});
}
updatePlayerInfo(player) {
document.getElementById('player-name').innerText = player.general.name;
document.getElementById('player-avatar').src = player.general.avatar;
document.getElementById('player-hp').innerText = player.hp;
document.getElementById('player-max-hp').innerText = player.maxHp;
document.getElementById('player-role-badge').innerText = player.role.name;
document.getElementById('player-role-badge').className = `absolute -bottom-2 -right-2 text-xs px-2 py-1 rounded border border-gray-600 ${player.role.color} bg-gray-900`;
// 更新对手UI
if (!player.isHuman) {
const el = document.getElementById(`player-${player.id}`);
if(el) {
document.getElementById(`hp-${player.id}`).innerText = player.hp;
document.getElementById(`hand-count-${player.id}`).innerText = player.hand.length;
if(player.isDead) el.style.opacity = '0.3';
}
}
}
updateHand(player) {
if (player.isHuman) {
const container = document.getElementById('hand-cards');
container.innerHTML = '';
document.getElementById('hand-count').innerText = player.hand.length;
player.hand.forEach((card, index) => {
const cardEl = document.createElement('div');
cardEl.className = `card w-24 h-36 bg-gray-200 rounded-lg shadow-md flex-shrink-0 ml-[-20px] first:ml-0 relative overflow-hidden group`;
cardEl.style.backgroundImage = `url(${card.img})`;
cardEl.onclick = () => this.selectCard(index, cardEl);
// 卡牌名称标签
const label = document.createElement('div');
label.className = "absolute bottom-0 w-full bg-black/60 text-white text-xs text-center py-1 truncate";
label.innerText = card.name;
cardEl.appendChild(label);
container.appendChild(cardEl);
});
}
}
selectCard(index, element) {
if (!this.game.players[0].isHuman || this.game.phase !== 'play') return;
// 清除其他选中
document.querySelectorAll('.card').forEach(c => c.classList.remove('selected'));
if (this.game.selectedCardIndex === index) {
this.game.selectedCardIndex = -1; // 取消选中
document.getElementById('action-buttons').classList.add('hidden');
document.getElementById('action-buttons').classList.remove('flex');
} else {
this.game.selectedCardIndex = index;
element.classList.add('selected');
document.getElementById('action-buttons').classList.remove('hidden');
document.getElementById('action-buttons').classList.add('flex');
}
}
enableControls(enable) {
const btn = document.getElementById('end-turn-btn');
if (enable) {
btn.disabled = false;
btn.classList.remove('bg-gray-700', 'text-gray-500', 'cursor-not-allowed');
btn.classList.add('bg-purple-600', 'text-white', 'hover:bg-purple-700');
} else {
btn.disabled = true;
btn.classList.add('bg-gray-700', 'text-gray-500', 'cursor-not-allowed');
btn.classList.remove('bg-purple-600', 'text-white', 'hover:bg-purple-700');
document.getElementById('action-buttons').classList.add('hidden');
document.querySelectorAll('.card').forEach(c => c.classList.remove('selected'));
this.game.selectedCardIndex = -1;
}
}
setPhaseText(text) {
document.getElementById('current-phase').innerText = text;
}
highlightActivePlayer(index) {
document.querySelectorAll('.player-area').
forEach(el => el.classList.remove('activ
e-turn'));
if (index === 0) {
document.getElementById('player-container').classList.add('active-turn');
} else {
const el = document.getElementById(player-${index});
if(el) el.classList.add('active-turn');
text
}
}
showDamageEffect(playerId, amount) {
// 简单震动效果,实际项目中可用Canvas或更复杂的DOM动画
const el = playerId === 0 ? document.getElementById('player-container') : document.getElementById(`player-${playerId}`);
if(el) {
el.classList.add('damage-effect');
setTimeout(() => el.classList.remove('damage-effect'), 500);
}
text
}