简单的PHP模拟脚本

时间:2012-10-08 04:31:11

标签: php simulation

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


有两名球员,均为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获胜!

**对那些回答旧例子的人表示抱歉!感谢您的反馈

3 个答案:

答案 0 :(得分:3)

这只允许两名球员。

$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;
  }
}

答案 1 :(得分:0)

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

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

for ($i = 0; $i < 5; $i++) {
    $key = array_rand($arr);
    echo $arr[$key], "<br />";
    unset($arr[$key]);
}

答案 2 :(得分:0)

这个PHP代码:

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

将输出类似于:

的内容
  
      
  1. 电子
  2.   
  3. C
  4.   
  5. A
  6.   
  7. d
  8.   
  9.   

修改

要完成第二个例子,你可以使用以下内容:

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!";