{"team1":[{"id":1,"name":"张飞","hp":1000,"attack":200,"rage":10,"fight_force":1000},{"id":2,"name":"赵云","hp":1500,"attack":200,"rage":10,"fight_force":1500}],"team2":[{"id":3,"name":"张辽","hp":1100,"attack":200,"rage":10,"fight_force":1100},{"id":4,"name":"贾诩","hp":1200,"attack":200,"rage":10,"fight_force":1200}]},这个数据是2个队伍team1和team2的英雄数据,合并team1和team2到一个数组,然后根据每个英雄的战力从高到低出手攻击,每个队伍的所有英雄都出手一次算一回合,英雄死亡后要移除队伍不再参与战斗,直到一个队伍的所有英雄死亡战斗结束,输出每回合战斗数据(记录哪个队伍的谁攻击哪个队伍的谁,伤害多少),使用php写出代码
时间: 2023-07-05 13:34:25 浏览: 72
好的,以下是PHP代码实现:
```php
<?php
$data = '{"team1":[{"id":1,"name":"张飞","hp":1000,"attack":200,"rage":10,"fight_force":1000},{"id":2,"name":"赵云","hp":1500,"attack":200,"rage":10,"fight_force":1500}],"team2":[{"id":3,"name":"张辽","hp":1100,"attack":200,"rage":10,"fight_force":1100},{"id":4,"name":"贾诩","hp":1200,"attack":200,"rage":10,"fight_force":1200}]}';
// 将json数据转换为数组
$arr = json_decode($data, true);
// 合并两个队伍为一个数组
$heroes = array_merge($arr['team1'], $arr['team2']);
// 根据战力对英雄进行排序
usort($heroes, function($a, $b) {
return $b['fight_force'] - $a['fight_force'];
});
// 记录每个英雄的生死状态
$hero_status = array_fill_keys(array_column($heroes, 'id'), 1);
// 记录每个队伍的存活状态
$team_status = array(
'team1' => 1,
'team2' => 1
);
// 记录每回合的攻击情况
$round_logs = array();
while ($team_status['team1'] && $team_status['team2']) {
// 每回合开始前,将所有英雄的怒气值清零
foreach ($heroes as &$hero) {
$hero['rage'] = 0;
}
// 队伍1出手
foreach ($arr['team1'] as $hero1) {
// 如果英雄已死亡,则跳过
if (!$hero_status[$hero1['id']]) {
continue;
}
// 寻找可以攻击的敌人
$enemy = null;
foreach ($heroes as $hero2) {
if ($hero2['id'] == $hero1['id'] || !$hero_status[$hero2['id']]) {
continue;
}
if ($enemy == null || $enemy['hp'] > $hero2['hp']) {
$enemy = $hero2;
}
}
// 如果找不到可以攻击的敌人,则跳过
if ($enemy == null) {
continue;
}
// 计算攻击伤害
$damage = $hero1['attack'] * ($hero1['rage'] + 100) / 100;
// 记录攻击情况
$round_logs[] = array(
'attacker' => $hero1['name'],
'defender' => $enemy['name'],
'damage' => $damage
);
// 扣除敌人的血量
$enemy['hp'] -= $damage;
// 如果敌人已死亡,则标记其生死状态,并更新队伍存活状态
if ($enemy['hp'] <= 0) {
$hero_status[$enemy['id']] = 0;
if (in_array($enemy, $arr['team1'])) {
$team_status['team2'] = array_sum($hero_status) > count($arr['team1']);
} else {
$team_status['team1'] = array_sum($hero_status) <= count($arr['team1']);
}
} else {
// 否则增加英雄的怒气值
$hero1['rage'] += 10;
}
}
// 队伍2出手
foreach ($arr['team2'] as $hero1) {
// 如果英雄已死亡,则跳过
if (!$hero_status[$hero1['id']]) {
continue;
}
// 寻找可以攻击的敌人
$enemy = null;
foreach ($heroes as $hero2) {
if ($hero2['id'] == $hero1['id'] || !$hero_status[$hero2['id']]) {
continue;
}
if ($enemy == null || $enemy['hp'] > $hero2['hp']) {
$enemy = $hero2;
}
}
// 如果找不到可以攻击的敌人,则跳过
if ($enemy == null) {
continue;
}
// 计算攻击伤害
$damage = $hero1['attack'] * ($hero1['rage'] + 100) / 100;
// 记录攻击情况
$round_logs[] = array(
'attacker' => $hero1['name'],
'defender' => $enemy['name'],
'damage' => $damage
);
// 扣除敌人的血量
$enemy['hp'] -= $damage;
// 如果敌人已死亡,则标记其生死状态,并更新队伍存活状态
if ($enemy['hp'] <= 0) {
$hero_status[$enemy['id']] = 0;
if (in_array($enemy, $arr['team1'])) {
$team_status['team2'] = array_sum($hero_status) > count($arr['team1']);
} else {
$team_status['team1'] = array_sum($hero_status) <= count($arr['team1']);
}
} else {
// 否则增加英雄的怒气值
$hero1['rage'] += 10;
}
}
}
// 输出每回合的攻击情况
foreach ($round_logs as $log) {
echo $log['attacker'] . ' 攻击了 ' . $log['defender'] . ',造成了 ' . $log['damage'] . ' 点伤害。' . PHP_EOL;
}
// 输出胜利队伍
if ($team_status['team1']) {
echo 'team1 获得了胜利!' . PHP_EOL;
} else {
echo 'team2 获得了胜利!' . PHP_EOL;
}
```
以上就是PHP代码实现,您可以在本地运行该代码,测试其输出结果。
阅读全文