Advertisement
Davinel

mafiasim

Mar 8th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.18 KB | None | 0 0
  1. <?php
  2. class Player {
  3.     function __construct($number=null, $mafia = 'no', $can_kill = 'no', $blocker = 'no', $defence = 'no', $healer = 'no') {
  4.     $this->number = $number;
  5.     $this->mafia = $mafia;
  6.     $this->can_kill = $can_kill;
  7.     $this->blocker = $blocker;
  8.     $this->defence = $defence;
  9.     $this->healer = $healer;
  10.     }
  11.     public $alive = 'yes';
  12.     public $healed = 'no';
  13.     public $blocked = 'no';
  14.     public $moved = 'no';
  15.     public $kills = 0;
  16.     public $executed_rate = 0;
  17. }
  18. ?>
  19.  
  20. <!DOCTYPE html>
  21. <html>
  22.     <head>
  23.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  24.         <title>Result</title>
  25.     </head>
  26.     <body>
  27.         <form action="javascript:window.location.reload()">
  28.              <input type="submit" value="Repeat" name="submit"/><br/>
  29.         </form>
  30.         <?php
  31.             if (isset($_POST['execution']))
  32.                 $execution = 'yes';
  33.             else
  34.                 $execution = 'no';
  35.             if (isset($_POST['bad_executed']))
  36.                 $bad_executed = $_POST['bad_executed'];
  37.             else
  38.                 $bad_executed = 20;
  39.             if (!is_numeric($bad_executed))
  40.                 $bad_executed = 20;
  41.             if ($bad_executed > 100)
  42.                 $bad_executed = 100;
  43.        
  44.             if (isset($_POST['good_executed']))
  45.                 $good_executed = $_POST['good_executed'];
  46.             else
  47.                 $good_executed = 20;
  48.             if (!is_numeric($good_executed))
  49.                 $good_executed = 20;
  50.             if ($good_executed > 100)
  51.                 $good_executed = 100;
  52.        
  53.             if(isset($_POST['number_of_players']))
  54.                 $number_of_players = $_POST['number_of_players'];
  55.             else
  56.                 $number_of_players = 20;
  57.            
  58.             $players = array();
  59.             for ($i = 0; $i < $number_of_players; $i++) {
  60.                 $players[$i] = new Player($i);
  61.                 if (isset($_POST['players_input'][$i]['mafia']))
  62.                     $players[$i]->mafia = 'yes';
  63.                 else
  64.                     $players[$i]->mafia = 'no';
  65.                 if (isset($_POST['players_input'][$i]['can_kill']))
  66.                     $players[$i]->can_kill = 'yes';
  67.                 else
  68.                     $players[$i]->can_kill = 'no';
  69.                 if (isset($_POST['players_input'][$i]['blocker']))
  70.                     $players[$i]->blocker = 'yes';
  71.                 else
  72.                     $players[$i]->blocker = 'no';
  73.                 if (isset($_POST['players_input'][$i]['defence']))
  74.                     $players[$i]->defence = 'yes';
  75.                 else
  76.                     $players[$i]->defence = 'no';
  77.                 if (isset($_POST['players_input'][$i]['healer']))
  78.                     $players[$i]->healer = 'yes';
  79.                 else
  80.                     $players[$i]->healer = 'no';                
  81.             }
  82.            
  83.            
  84.             $days_count = 0;
  85.             $current_kill_count = 0;
  86.             while (True) {
  87.                 $killed_list = array();
  88.                 $alive_list = array();
  89.                 $maniac_list = array();
  90.                 $alive_count = 0;
  91.                 $mafia_count = 0;
  92.                 $maniac_count = 0;
  93.                 $player_count = 0;
  94.  
  95.                 foreach ($players as $player){
  96.                     if ($player->alive == 'yes'){
  97.                         $alive_count++;
  98.                         $alive_list[] = $player;
  99.                         if ($player->mafia == 'yes'){
  100.                             $mafia_count++;
  101.                         }
  102.                         if ($player->mafia == 'no' && $player->can_kill == 'yes'){
  103.                             $maniac_count++;
  104.                             $maniac_list[] = $player;
  105.                         }
  106.  
  107.                     }
  108.                 }
  109.                 echo '<hr/><br/>';
  110.                 echo $days_count, ' days passed <br/>';
  111.                 echo 'Alive: ', $alive_count, '<br/>';
  112.                 echo 'Town: ', ($alive_count - $mafia_count - $maniac_count), '<br/>';
  113.                 echo 'Mafia: ', $mafia_count, '<br/>';
  114.                 echo 'Maniac: ', $maniac_count, '<br/>';
  115.                 echo 'alive list: ';
  116.                 foreach ($alive_list as $alive){
  117.                     echo '[',$alive->number,'], ';
  118.                 }
  119.                 echo '<br/>';
  120.                 if (($alive_count - $mafia_count) <= $mafia_count && $maniac_count == 0 || ($alive_count - $mafia_count - $maniac_count) == 0 && ($mafia_count > $maniac_count)) {
  121.                     echo 'Mafia win';
  122.                     break;
  123.                 }
  124.                 if ($mafia_count == 0 && $maniac_count == 0){
  125.                     echo 'Town win';
  126.                     break;
  127.                 }
  128.                 if ($mafia_count == 0 && $maniac_count == 1 && $alive_count <= 2){
  129.                     echo 'Maniac win';
  130.                     break;
  131.                 }
  132.                 if ($days_count > 100){
  133.                     echo 'Time is up. Something is very wrong here...';
  134.                     break;
  135.                 }
  136.                 foreach ($alive_list as $player){
  137.                     $player->moved = 'no';
  138.                     $player->blocked = 'no';
  139.                     $player->healed = 'no';
  140.                     $player->executed_rate = 0;
  141.                 }
  142.  
  143.                 // blockers turn
  144.                 foreach ($alive_list as $player){
  145.                     if ($player->blocked == 'no' && $player->moved == 'no'){
  146.                         if($player->blocker == 'yes'){
  147.                             if($player->mafia == 'no'){
  148.                                 $target = target_not_self($alive_count, $alive_list, $player);
  149.                                 $alive_list[$target]->blocked = 'yes';
  150.                                 $player->moved = 'yes';
  151.                                 echo 'Town blocker ',$player->number,' blocked ', $alive_list[$target]->number, '<br/';
  152.  
  153.                             }
  154.                             elseif($player->mafia == 'yes'){
  155.                                 $target = target_not_mafia ($alive_count, $alive_list, $player);
  156.                                 $alive_list[$target]->blocked = 'yes';
  157.                                 if ($mafia_count != 1){
  158.                                     $player->moved = 'yes';
  159.                                 echo 'Mafia blocker ',$player->number,' blocked ', $alive_list[$target]->number,'<br/>';
  160.                                 }
  161.                             }
  162.                         }
  163.                     }
  164.                 }
  165.                
  166.    
  167.  
  168.                 // healers turn
  169.                 foreach ($alive_list as $player){
  170.                     if ($player->blocked == 'no' && $player->moved == 'no'){
  171.                         if($player->healer == 'yes'){
  172.                             if($player->mafia == 'no'){
  173.                                 $target = target_not_self($alive_count, $alive_list, $player);
  174.                                 $alive_list[$target]->healed = 'yes';
  175.                                 $player->moved = 'yes';
  176.                                 echo 'Town healer ',$player->number,' healed ', $alive_list[$target]->number,'<br/>';
  177.  
  178.                             }
  179.                             elseif($player->mafia == 'yes'){
  180.                                 $target = target_is_mafia ($alive_count, $alive_list, $player);                        
  181.                                 $alive_list[$target]->blocked = 'yes';
  182.                                 if ($mafia_count != 1){
  183.                                     $player->moved = 'yes';
  184.                                 echo 'Mafia healer ',$player->number,' healed ', $alive_list[$target]->number,'<br/>';
  185.                                 }
  186.                             }
  187.                         }
  188.                     }
  189.                 }
  190.  
  191.  
  192.  
  193.  
  194.                 // maniacs turn
  195.                 foreach ($maniac_list as $player){
  196.                     if ($player->blocked == 'no' && $player->moved == 'no'){
  197.                         $target = target_not_self($alive_count, $alive_list, $player);
  198.                         if ($alive_list[$target]->defence == 'no' && $alive_list[$target]->healed == 'no'){
  199.                             $killed_list[]= $alive_list[$target];
  200.                             $player->kills++;                          
  201.                         }
  202.                         if ($alive_list[$target]->defence == 'yes'){
  203.                             $alive_list[$target]->defence = 'no';
  204.                         }
  205.                         $player->moved = 'yes';
  206.                         echo 'Maniac ',$player->number,' trying to kill ', $alive_list[$target]->number,'<br/>';
  207.                     }
  208.                 }
  209.  
  210.  
  211.                 // mafia turn
  212.                 $mafia_kill = 0;
  213.                 $mafia_list = array();
  214.                 foreach ($alive_list as $player){
  215.                     if ($player->blocked == 'no' && $player->moved == 'no' && $player->mafia == 'yes' && $player->can_kill == 'yes'){
  216.                             $mafia_list[] = $player;
  217.                     }
  218.                 }
  219.                 if ($mafia_kill == 0){
  220.                     if (count($mafia_list) > 1){      
  221.                         usort($mafia_list, "cmp");
  222.                     }
  223.                     if (count($mafia_list) > 0){                        
  224.                         $target = target_not_mafia ($alive_count, $alive_list, $player);
  225.                         if ($alive_list[$target]->defence == 'no' && $alive_list[$target]->healed == 'no'){
  226.                             $killed_list[]= $alive_list[$target];
  227.                             $mafia_list[0]->kills++;                          
  228.                         }
  229.                         if ($alive_list[$target]->defence == 'yes'){
  230.                             $alive_list[$target]->defence = 'no';
  231.                         }
  232.                         $mafia_list[0]->moved = 'yes';
  233.                         echo 'Mafiosi ',$mafia_list[0]->number,' trying to kill ', $alive_list[$target]->number,'<br/>';    
  234.                         }
  235.                 }
  236.                 $kill_count = 0;
  237.                 echo 'death list: ';
  238.                 foreach ($killed_list as $killed){
  239.                     echo '[',$killed->number,'], ';
  240.                     if ($killed->alive == 'yes'){
  241.                         $killed->alive = 'no';
  242.                         $kill_count++;
  243.                         foreach ($alive_list as $key => $value)
  244.                             if ($alive_list[$key] == $killed)
  245.                                 unset ($alive_list[$key]);                        
  246.                     }
  247.                 }                
  248.                 echo '<br/>';
  249.                 echo 'end night alive list: ';
  250.                 foreach ($alive_list as $alive){
  251.                     echo '[',$alive->number,'], ';
  252.                 }
  253.                 $current_kill_count = $current_kill_count + $kill_count;
  254.                 echo '<br/>***<br/>';
  255.                 echo 'Kills at night: ', $kill_count,'<br/>';                  
  256.                 $days_count++;
  257.                 if ($execution == 'yes'){
  258.                     $current_kill_count = execute($number_of_players, $current_kill_count, $alive_count, $mafia_count, $days_count, $bad_executed, $good_executed, $alive_list);
  259.                 }
  260.                
  261.  
  262.  
  263.  
  264.  
  265.  
  266.         }
  267.         ?>
  268.         <form action="javascript:window.location.reload()">
  269.              <input type="submit" value="Repeat" name="submit"/><br/>
  270.         </form>
  271.     </body>
  272. </html>
  273. <?php
  274.  
  275. function execute ($number_of_players, $current_kill_count, $alive_count, $mafia_count, $days_count, $bad_executed, $good_executed, $alive_list){
  276.     if ($days_count <= 1){
  277.         echo '1st day... nothing happens<br/>';
  278.         return $current_kill_count;
  279.     }
  280.     if ($days_count == 2){
  281.         $bad_executed = $bad_executed * 0.6;
  282.         $good_executed = $good_executed * 0.6;
  283.     }
  284.     if ($mafia_count == 5)
  285.         $bad_executed = $bad_executed * 0.8;
  286.     if ($current_kill_count / $number_of_players >= 0.25){
  287.         $bad_executed = $bad_executed * 2;
  288.         if ($current_kill_count / $number_of_players >= 0.5){
  289.             $bad_executed = $bad_executed * 1.5;
  290.             if ($mafia_count >= 4){
  291.                 $bad_executed = $bad_executed * 0.7;
  292.                 $good_executed = $good_executed * 2;
  293.             }
  294.         }
  295.     }
  296.     $res = mt_rand(1, 100);
  297.     echo 'random execution number: ', $res,'<br/>';
  298.     echo '"bad" executed chance: ', $bad_executed,'<br/>';
  299.     echo '"good" executed chance: ', $good_executed,'<br/>';
  300.     echo 'current_kill_count / number_of_players: ', ($current_kill_count / $number_of_players), '<br/>';
  301.     echo 'current kill count: ', $current_kill_count, '<br/>';
  302.     $execution_list = array();
  303.     if ($res <= $bad_executed){
  304.         echo 'trying to execute `bad`<br/>';
  305.         foreach ($alive_list as $player){
  306.             if ($player->kills > 0)
  307.                 $execution_list[] = $player;
  308.         }
  309.        
  310.         if (count($execution_list) > 1){      
  311.             usort($execution_list, "cmp");
  312.         }
  313.         if (count($execution_list) > 0){
  314.             $killer_kill_count = 0;
  315.             foreach ($execution_list as $killer){
  316.                 echo 'killer number ',$killer->number,' has ', $killer->kills,' kills<br/>';
  317.                 $killer_kill_count = $killer_kill_count + $killer->kills;
  318.             }
  319.             $executed = mt_rand(0, mt_getrandmax()) /  mt_getrandmax();
  320.             echo 'executed random number: ',$executed,'<br/>';
  321.             $executed_range = 0;
  322.             foreach ($execution_list as $killer){
  323.                 $killer->executed_rate = $killer->kills / $killer_kill_count;
  324.                 $executed_range = $executed_range + $killer->executed_rate;
  325.                 echo 'executed rate: ',$killer->executed_rate,', executed range: ',$executed_range,'<br/>';
  326.                 if ($executed_range >= $executed){
  327.                     $killer->alive = 'no';
  328.                     echo 'Player number ', $killer->number,' executed. He killed ',$killer->kills,' times<br/>';
  329.                     $current_kill_count = $current_kill_count - $killer->kills;
  330.                     return $current_kill_count;
  331.                 }
  332.             }
  333.         }
  334.     }
  335.     elseif ($res >= 100 - $good_executed){
  336.         echo 'trying to execute `good`<br/>';
  337.         foreach ($alive_list as $player){
  338.             if ($player->kills == 0)
  339.                 $execution_list[] = $player;
  340.         }
  341.         if (count($execution_list) > 0){
  342.             $executed = mt_rand(0, count($execution_list)-1);
  343.             echo 'executed random number: ',$executed,'<br/>';
  344.             $execution_list[$executed]->alive = 'no';
  345.             echo 'Player number ', $execution_list[$executed]->number, ' executed. He killed ', $execution_list[$executed]->kills, ' times<br/>';
  346.             return $current_kill_count;
  347.         }
  348.     }
  349.     else {
  350.         echo 'nothing happens<br/>';
  351.         return $current_kill_count;
  352.     }
  353.     return $current_kill_count;
  354. }
  355.  
  356. function target_not_self ($alive_count, $alive_list, $player){
  357.     $action = 0;
  358.     $n = 0;
  359.     while ($action == 0 || $n > 1000){
  360.         $n++;
  361.         $target = mt_rand(0, ($alive_count-1));
  362.         if ($alive_list[$target] != $player){
  363.             $action = 1;
  364.         }                        
  365.     }
  366.     return $target;
  367. }
  368.  
  369. function target_not_mafia ($alive_count, $alive_list, $player){
  370.     $action = 0;
  371.     $n = 0;
  372.     while ($action == 0 || $n > 1000){
  373.         $n++;
  374.         $target = mt_rand(0, ($alive_count-1));
  375.         if ($alive_list[$target]->mafia == 'no'){
  376.             $action = 1;
  377.         }                        
  378.     }
  379.     return $target;
  380. }
  381.  
  382. function target_is_mafia ($alive_count, $alive_list, $player){
  383.     $action = 0;
  384.     $n = 0;
  385.     while ($action == 0 || $n > 1000){
  386.         $n++;
  387.         $target = mt_rand(0, ($alive_count-1));
  388.         if ($alive_list[$target]->mafia == 'yes'){
  389.             $action = 1;
  390.         }                        
  391.     }
  392.     return $target;
  393. }
  394.  
  395. function cmp ($p1,$p2){
  396.     if( $p1->kills == $p2->kills) {
  397.         return 0;
  398.     }
  399.     return ($p1->kills > $p2->kills) ? 1 : -1;
  400. }
  401.  
  402. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement