1

我正在为我的网站做一个简单的模拟,我环顾四周,但找不到任何关于如何实现这一点的信息。有人可以提供我将如何使用代码实现以下输出:


有两个玩家,都是2级。

玩家 1

攻击 - 1 防御 - 2 生命值 - 10

玩家 1

攻击 - 2 防御 - 1 生命值 - 10

我想在这两个玩家之间进行一场模拟回合制战斗。它会随机选择谁得到第一个命中。为此,我们会说玩家 1 先行。我用来计算命中的等式是($playerattack/$playerdefense) + rand(1,$level)(等式可能工作得很好,可能不行。只是为了测试目的)我想得到如下输出。

玩家 1(10 生命值) - 击中 2

玩家 2 (8 生命值) - 命中 1

. . .

玩家 1(1 生命值) - 击中 2

玩家 2 死亡,玩家 1 获胜!

**对不起那些回答旧例子的人!感谢您的反馈

4

3 回答 3

4

这只允许两个玩家。

$players = array(
  array('attack' => 1, 'defense' => 2, 'health' => 10, 'level' => 2, ), 
  array('attack' => 2, 'defense' => 1, 'health' => 10, 'level' => 2, ), 
);
$count = 0;
while (true) {
  $count++;
  $players_temp = $players;
  $attacker_key = array_rand($players);
  $attacker     =& $players[$attacker_key];
  unset($players_temp[$attacker_key]);
  $defender_key = array_rand($players_temp);
  $defender     =& $players[$defender_key];

  $hit = ($attacker['attack']/$defender['defense']) + rand(1, $attacker['level']);
  echo "{$count}. Player {$defender_key} ({$defender['health']} health) is hit a {$hit} by Player {$attacker_key} ({$attacker['health']} health)\n";
  $defender['health'] = $defender['health'] - $hit;

  if ($defender['health'] <= 0) {
    echo "Player {$defender_key} dies, Player {$attacker_key} is victorious!\n";
    break;
  }

}

[已编辑]
这允许两个以上的玩家并且是回合制的。

set_time_limit(1);
$players = array(
  'A' => array('attack' => 1, 'defense' => 2, 'health' => 10, 'level' => 2, ), 
  'B' => array('attack' => 2, 'defense' => 1, 'health' => 10, 'level' => 2, ), 
  'C' => array('attack' => 1, 'defense' => 1, 'health' => 10, 'level' => 2, ), 
  'D' => array('attack' => 2, 'defense' => 2, 'health' => 10, 'level' => 2, ), 
);
$player_ids = array_keys($players);
$no_of_players = sizeof($player_ids);
$count = 0;
while (true) {
  $defender_key=0;
  for ($attacker_key=0;$attacker_key<$no_of_players;$attacker_key++) {
    if (!isset($player_ids[$attacker_key])) continue;
    $count++;
    $attacker_id  = $player_ids[$attacker_key];
    $attacker     =& $players[$attacker_id];

    $defender_key = $attacker_key + 1;
    while (!isset($player_ids[$defender_key])) {
      $defender_key = ($defender_key+1 < $no_of_players) ?  $defender_key + 1 : 0;
    }
    $defender_id  = $player_ids[$defender_key];
    $defender     =& $players[$defender_id];

    $hit = round(($attacker['attack']/$defender['defense']) + rand(1, $attacker['level']), 2);
    echo "{$count}. Player {$attacker_id} ({$attacker['health']} health) has hit a {$hit} on Player {$defender_id} ({$defender['health']} health)<br />";
    $defender['health'] = $defender['health'] - $hit;

    if ($defender['health'] <= 0) {
      echo "Player {$defender_id} dies<br />";
      unset($players[$defender_id], $player_ids[$defender_key]);
      if (sizeof($player_ids) == 1) {
        echo "Player {$attacker_id} is victorious!<br />";
        break 2;
      }
    }

    //if ($count > 100) break 2;
  }
}
于 2012-10-08T04:46:12.367 回答
0

从数组或 aj 开始。运行一个循环 5 次(for 循环),选择一个随机值,然后从数组中删除该值。

$arr = array('a', 'b',..., 'j');

for ($i = 0; $i < 5; $i++) {
    $key = array_rand($arr);
    echo $arr[$key], "<br />";
    unset($arr[$key]);
}
于 2012-10-08T04:45:44.603 回答
0

这个 PHP 代码:

$arr = array('A','B','C','D','E');
shuffle($arr);
for ($i = 0; $i < 5; $i++) {
    echo ($i + 1) . '. ' . $arr[$i] . "<br/>";
}

将输出类似于:

  1. C
  2. 一种
  3. D

编辑:

要完成第二个示例,您将使用以下内容:

while ($player1->health > 0 && $player2->health > 0) {
    ...
    echo "Player X (health X)....";
}
if ($player1->health == 0) echo "Player 1 died!";
else echo "Player 2 died!";
于 2012-10-08T04:46:25.917 回答