php项目统计地面对抗谁胜出?

wen PHP项目 3

本文目录导读:

php项目统计地面对抗谁胜出?

  1. 方案一:最简单的数值比较(基础属性比拼)
  2. 方案二:综合战力计算(多维度加权)
  3. 方案三:回合制模拟(实战推演)
  4. 方案四:数据库查询(基于历史战绩统计)
  5. 补充建议:架构设计

在PHP项目中统计“地面对抗”(Ground Combat)谁胜出,通常取决于业务规则的复杂程度,这不仅仅是简单的数字比较,可能涉及多种维度(攻击力、防御力、地形加成、装备、随机数等)。

我为你提供4种由浅入深的实现方案,你可以根据项目实际场景选择。

最简单的数值比较(基础属性比拼)

如果胜出规则就是“谁的攻击力高谁赢”,直接使用 max() 函数即可。

<?php
$player1 = ['name' => '吕布', 'attack' => 95];
$player2 = ['name' => '关羽', 'attack' => 97];
// 比拼攻击力
$winner = ($player1['attack'] >= $player2['attack']) ? $player1 : $player2;
echo $winner['name'] . ' 胜出!';
?>

综合战力计算(多维度加权)

如果规则包含防御、敏捷、体力等,需要计算一个“综合战力指数”。

<?php
class GroundCombat
{
    // 计算战力指数
    public static function calculatePower(array $unit): float
    {
        $power = ($unit['attack'] * 0.5) 
               + ($unit['defense'] * 0.3) 
               + ($unit['speed'] * 0.2);
        // 加入随机浮动(运气因素)
        $luck = rand(90, 110) / 100; // 90% - 110% 浮动
        return $power * $luck;
    }
    public static function getWinner(array $unitA, array $unitB): string
    {
        $scoreA = self::calculatePower($unitA);
        $scoreB = self::calculatePower($unitB);
        if ($scoreA > $scoreB) {
            return $unitA['name'];
        } elseif ($scoreB > $scoreA) {
            return $unitB['name'];
        } else {
            return '平局';
        }
    }
}
// 使用示例
$unitA = ['name' => '骑士', 'attack' => 80, 'defense' => 70, 'speed' => 60];
$unitB = ['name' => '野蛮人', 'attack' => 90, 'defense' => 50, 'speed' => 50];
echo GroundCombat::getWinner($unitA, $unitB);
?>

回合制模拟(实战推演)

如果项目需要展示“怎么赢的”(比如像游戏一样有回合),需要更复杂的模拟,这里用简单的“血量磨损”模型:

<?php
class CombatSimulator
{
    public static function fight(array $fighter1, array $fighter2): array
    {
        // 复制数据,避免修改原数组
        $hp1 = $fighter1['hp'];
        $hp2 = $fighter2['hp'];
        $round = 0;
        while ($hp1 > 0 && $hp2 > 0) {
            $round++;
            // 假设先手为速度快的
            $first = ($fighter1['speed'] >= $fighter2['speed']) ? 'A' : 'B';
            if ($first === 'A') {
                $damage = max(1, $fighter1['attack'] - $fighter2['defense'] / 2);
                $hp2 -= $damage;
                if ($hp2 <= 0) break;
                $damage = max(1, $fighter2['attack'] - $fighter1['defense'] / 2);
                $hp1 -= $damage;
            } else {
                // 反过来
                $damage = max(1, $fighter2['attack'] - $fighter1['defense'] / 2);
                $hp1 -= $damage;
                if ($hp1 <= 0) break;
                $damage = max(1, $fighter1['attack'] - $fighter2['defense'] / 2);
                $hp2 -= $damage;
            }
        }
        $winnerName = ($hp1 > 0) ? $fighter1['name'] : $fighter2['name'];
        return [
            'winner' => $winnerName,
            'rounds' => $round,
            'hp_remaining' => ($hp1 > 0) ? $hp1 : $hp2,
        ];
    }
}
// 例子
$result = CombatSimulator::fight(
    ['name' => '赵云', 'hp' => 100, 'attack' => 85, 'defense' => 40, 'speed' => 95],
    ['name' => '张飞', 'hp' => 120, 'attack' => 95, 'defense' => 30, 'speed' => 70]
);
print_r($result);
?>

数据库查询(基于历史战绩统计)

如果你的项目是统计“历史对战中谁胜率最高”,这属于聚合查询,用SQL处理最合理(在PHP中调用)。

表结构示例 battles

| battle_id | winner_id | loser_id | battle_date |

PHP + PDO 查询胜场统计

<?php
// 假设已经连接数据库 $pdo
$stmt = $pdo->query("
    SELECT 
        winner_id AS fighter_id,
        COUNT(*) AS win_count
    FROM battles
    WHERE battle_type = 'ground'
    GROUP BY winner_id
    ORDER BY win_count DESC
    LIMIT 1
");
$topFighter = $stmt->fetch(PDO::FETCH_ASSOC);
echo "地面战最强选手 ID: " . $topFighter['fighter_id'] . " 胜场: " . $topFighter['win_count'];
?>

补充建议:架构设计

在大型项目中,最好使用策略模式门面模式封装这些逻辑。

<?php
interface CombatStrategy {
    public function getWinner(array $attacker, array $defender): string;
}
class PowerStrategy implements CombatStrategy { /* ... */ }
class SimulationStrategy implements CombatStrategy { /* ... */ }
// 在业务控制器中切换策略
class CombatService {
    public function __construct(private CombatStrategy $strategy) {}
    public function doFight($a, $b) {
        return $this->strategy->getWinner($a, $b);
    }
}
?>

你需要我帮你写具体的哪一种?

  • 是纯后端逻辑计算?
  • 还是需要处理地形加成(丛林/沙漠/水中)?
  • 是用于排位赛积分计算,还是单挑判定?

告诉我具体业务,我可以把代码补全到可以直接跑的程度。

抱歉,评论功能暂时关闭!