Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Player {
- function __construct($number=null, $mafia = 'no', $can_kill = 'no', $blocker = 'no', $defence = 'no', $healer = 'no') {
- $this->number = $number;
- $this->mafia = $mafia;
- $this->can_kill = $can_kill;
- $this->blocker = $blocker;
- $this->defence = $defence;
- $this->healer = $healer;
- }
- public $alive = 'yes';
- public $healed = 'no';
- public $blocked = 'no';
- public $moved = 'no';
- public $kills = 0;
- public $executed_rate = 0;
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Result</title>
- </head>
- <body>
- <form action="javascript:window.location.reload()">
- <input type="submit" value="Repeat" name="submit"/><br/>
- </form>
- <?php
- if (isset($_POST['execution']))
- $execution = 'yes';
- else
- $execution = 'no';
- if (isset($_POST['bad_executed']))
- $bad_executed = $_POST['bad_executed'];
- else
- $bad_executed = 20;
- if (!is_numeric($bad_executed))
- $bad_executed = 20;
- if ($bad_executed > 100)
- $bad_executed = 100;
- if (isset($_POST['good_executed']))
- $good_executed = $_POST['good_executed'];
- else
- $good_executed = 20;
- if (!is_numeric($good_executed))
- $good_executed = 20;
- if ($good_executed > 100)
- $good_executed = 100;
- if(isset($_POST['number_of_players']))
- $number_of_players = $_POST['number_of_players'];
- else
- $number_of_players = 20;
- $players = array();
- for ($i = 0; $i < $number_of_players; $i++) {
- $players[$i] = new Player($i);
- if (isset($_POST['players_input'][$i]['mafia']))
- $players[$i]->mafia = 'yes';
- else
- $players[$i]->mafia = 'no';
- if (isset($_POST['players_input'][$i]['can_kill']))
- $players[$i]->can_kill = 'yes';
- else
- $players[$i]->can_kill = 'no';
- if (isset($_POST['players_input'][$i]['blocker']))
- $players[$i]->blocker = 'yes';
- else
- $players[$i]->blocker = 'no';
- if (isset($_POST['players_input'][$i]['defence']))
- $players[$i]->defence = 'yes';
- else
- $players[$i]->defence = 'no';
- if (isset($_POST['players_input'][$i]['healer']))
- $players[$i]->healer = 'yes';
- else
- $players[$i]->healer = 'no';
- }
- $days_count = 0;
- $current_kill_count = 0;
- while (True) {
- $killed_list = array();
- $alive_list = array();
- $maniac_list = array();
- $alive_count = 0;
- $mafia_count = 0;
- $maniac_count = 0;
- $player_count = 0;
- foreach ($players as $player){
- if ($player->alive == 'yes'){
- $alive_count++;
- $alive_list[] = $player;
- if ($player->mafia == 'yes'){
- $mafia_count++;
- }
- if ($player->mafia == 'no' && $player->can_kill == 'yes'){
- $maniac_count++;
- $maniac_list[] = $player;
- }
- }
- }
- echo '<hr/><br/>';
- echo $days_count, ' days passed <br/>';
- echo 'Alive: ', $alive_count, '<br/>';
- echo 'Town: ', ($alive_count - $mafia_count - $maniac_count), '<br/>';
- echo 'Mafia: ', $mafia_count, '<br/>';
- echo 'Maniac: ', $maniac_count, '<br/>';
- echo 'alive list: ';
- foreach ($alive_list as $alive){
- echo '[',$alive->number,'], ';
- }
- echo '<br/>';
- if (($alive_count - $mafia_count) <= $mafia_count && $maniac_count == 0 || ($alive_count - $mafia_count - $maniac_count) == 0 && ($mafia_count > $maniac_count)) {
- echo 'Mafia win';
- break;
- }
- if ($mafia_count == 0 && $maniac_count == 0){
- echo 'Town win';
- break;
- }
- if ($mafia_count == 0 && $maniac_count == 1 && $alive_count <= 2){
- echo 'Maniac win';
- break;
- }
- if ($days_count > 100){
- echo 'Time is up. Something is very wrong here...';
- break;
- }
- foreach ($alive_list as $player){
- $player->moved = 'no';
- $player->blocked = 'no';
- $player->healed = 'no';
- $player->executed_rate = 0;
- }
- // blockers turn
- foreach ($alive_list as $player){
- if ($player->blocked == 'no' && $player->moved == 'no'){
- if($player->blocker == 'yes'){
- if($player->mafia == 'no'){
- $target = target_not_self($alive_count, $alive_list, $player);
- $alive_list[$target]->blocked = 'yes';
- $player->moved = 'yes';
- echo 'Town blocker ',$player->number,' blocked ', $alive_list[$target]->number, '<br/';
- }
- elseif($player->mafia == 'yes'){
- $target = target_not_mafia ($alive_count, $alive_list, $player);
- $alive_list[$target]->blocked = 'yes';
- if ($mafia_count != 1){
- $player->moved = 'yes';
- echo 'Mafia blocker ',$player->number,' blocked ', $alive_list[$target]->number,'<br/>';
- }
- }
- }
- }
- }
- // healers turn
- foreach ($alive_list as $player){
- if ($player->blocked == 'no' && $player->moved == 'no'){
- if($player->healer == 'yes'){
- if($player->mafia == 'no'){
- $target = target_not_self($alive_count, $alive_list, $player);
- $alive_list[$target]->healed = 'yes';
- $player->moved = 'yes';
- echo 'Town healer ',$player->number,' healed ', $alive_list[$target]->number,'<br/>';
- }
- elseif($player->mafia == 'yes'){
- $target = target_is_mafia ($alive_count, $alive_list, $player);
- $alive_list[$target]->blocked = 'yes';
- if ($mafia_count != 1){
- $player->moved = 'yes';
- echo 'Mafia healer ',$player->number,' healed ', $alive_list[$target]->number,'<br/>';
- }
- }
- }
- }
- }
- // maniacs turn
- foreach ($maniac_list as $player){
- if ($player->blocked == 'no' && $player->moved == 'no'){
- $target = target_not_self($alive_count, $alive_list, $player);
- if ($alive_list[$target]->defence == 'no' && $alive_list[$target]->healed == 'no'){
- $killed_list[]= $alive_list[$target];
- $player->kills++;
- }
- if ($alive_list[$target]->defence == 'yes'){
- $alive_list[$target]->defence = 'no';
- }
- $player->moved = 'yes';
- echo 'Maniac ',$player->number,' trying to kill ', $alive_list[$target]->number,'<br/>';
- }
- }
- // mafia turn
- $mafia_kill = 0;
- $mafia_list = array();
- foreach ($alive_list as $player){
- if ($player->blocked == 'no' && $player->moved == 'no' && $player->mafia == 'yes' && $player->can_kill == 'yes'){
- $mafia_list[] = $player;
- }
- }
- if ($mafia_kill == 0){
- if (count($mafia_list) > 1){
- usort($mafia_list, "cmp");
- }
- if (count($mafia_list) > 0){
- $target = target_not_mafia ($alive_count, $alive_list, $player);
- if ($alive_list[$target]->defence == 'no' && $alive_list[$target]->healed == 'no'){
- $killed_list[]= $alive_list[$target];
- $mafia_list[0]->kills++;
- }
- if ($alive_list[$target]->defence == 'yes'){
- $alive_list[$target]->defence = 'no';
- }
- $mafia_list[0]->moved = 'yes';
- echo 'Mafiosi ',$mafia_list[0]->number,' trying to kill ', $alive_list[$target]->number,'<br/>';
- }
- }
- $kill_count = 0;
- echo 'death list: ';
- foreach ($killed_list as $killed){
- echo '[',$killed->number,'], ';
- if ($killed->alive == 'yes'){
- $killed->alive = 'no';
- $kill_count++;
- foreach ($alive_list as $key => $value)
- if ($alive_list[$key] == $killed)
- unset ($alive_list[$key]);
- }
- }
- echo '<br/>';
- echo 'end night alive list: ';
- foreach ($alive_list as $alive){
- echo '[',$alive->number,'], ';
- }
- $current_kill_count = $current_kill_count + $kill_count;
- echo '<br/>***<br/>';
- echo 'Kills at night: ', $kill_count,'<br/>';
- $days_count++;
- if ($execution == 'yes'){
- $current_kill_count = execute($number_of_players, $current_kill_count, $alive_count, $mafia_count, $days_count, $bad_executed, $good_executed, $alive_list);
- }
- }
- ?>
- <form action="javascript:window.location.reload()">
- <input type="submit" value="Repeat" name="submit"/><br/>
- </form>
- </body>
- </html>
- <?php
- function execute ($number_of_players, $current_kill_count, $alive_count, $mafia_count, $days_count, $bad_executed, $good_executed, $alive_list){
- if ($days_count <= 1){
- echo '1st day... nothing happens<br/>';
- return $current_kill_count;
- }
- if ($days_count == 2){
- $bad_executed = $bad_executed * 0.6;
- $good_executed = $good_executed * 0.6;
- }
- if ($mafia_count == 5)
- $bad_executed = $bad_executed * 0.8;
- if ($current_kill_count / $number_of_players >= 0.25){
- $bad_executed = $bad_executed * 2;
- if ($current_kill_count / $number_of_players >= 0.5){
- $bad_executed = $bad_executed * 1.5;
- if ($mafia_count >= 4){
- $bad_executed = $bad_executed * 0.7;
- $good_executed = $good_executed * 2;
- }
- }
- }
- $res = mt_rand(1, 100);
- echo 'random execution number: ', $res,'<br/>';
- echo '"bad" executed chance: ', $bad_executed,'<br/>';
- echo '"good" executed chance: ', $good_executed,'<br/>';
- echo 'current_kill_count / number_of_players: ', ($current_kill_count / $number_of_players), '<br/>';
- echo 'current kill count: ', $current_kill_count, '<br/>';
- $execution_list = array();
- if ($res <= $bad_executed){
- echo 'trying to execute `bad`<br/>';
- foreach ($alive_list as $player){
- if ($player->kills > 0)
- $execution_list[] = $player;
- }
- if (count($execution_list) > 1){
- usort($execution_list, "cmp");
- }
- if (count($execution_list) > 0){
- $killer_kill_count = 0;
- foreach ($execution_list as $killer){
- echo 'killer number ',$killer->number,' has ', $killer->kills,' kills<br/>';
- $killer_kill_count = $killer_kill_count + $killer->kills;
- }
- $executed = mt_rand(0, mt_getrandmax()) / mt_getrandmax();
- echo 'executed random number: ',$executed,'<br/>';
- $executed_range = 0;
- foreach ($execution_list as $killer){
- $killer->executed_rate = $killer->kills / $killer_kill_count;
- $executed_range = $executed_range + $killer->executed_rate;
- echo 'executed rate: ',$killer->executed_rate,', executed range: ',$executed_range,'<br/>';
- if ($executed_range >= $executed){
- $killer->alive = 'no';
- echo 'Player number ', $killer->number,' executed. He killed ',$killer->kills,' times<br/>';
- $current_kill_count = $current_kill_count - $killer->kills;
- return $current_kill_count;
- }
- }
- }
- }
- elseif ($res >= 100 - $good_executed){
- echo 'trying to execute `good`<br/>';
- foreach ($alive_list as $player){
- if ($player->kills == 0)
- $execution_list[] = $player;
- }
- if (count($execution_list) > 0){
- $executed = mt_rand(0, count($execution_list)-1);
- echo 'executed random number: ',$executed,'<br/>';
- $execution_list[$executed]->alive = 'no';
- echo 'Player number ', $execution_list[$executed]->number, ' executed. He killed ', $execution_list[$executed]->kills, ' times<br/>';
- return $current_kill_count;
- }
- }
- else {
- echo 'nothing happens<br/>';
- return $current_kill_count;
- }
- return $current_kill_count;
- }
- function target_not_self ($alive_count, $alive_list, $player){
- $action = 0;
- $n = 0;
- while ($action == 0 || $n > 1000){
- $n++;
- $target = mt_rand(0, ($alive_count-1));
- if ($alive_list[$target] != $player){
- $action = 1;
- }
- }
- return $target;
- }
- function target_not_mafia ($alive_count, $alive_list, $player){
- $action = 0;
- $n = 0;
- while ($action == 0 || $n > 1000){
- $n++;
- $target = mt_rand(0, ($alive_count-1));
- if ($alive_list[$target]->mafia == 'no'){
- $action = 1;
- }
- }
- return $target;
- }
- function target_is_mafia ($alive_count, $alive_list, $player){
- $action = 0;
- $n = 0;
- while ($action == 0 || $n > 1000){
- $n++;
- $target = mt_rand(0, ($alive_count-1));
- if ($alive_list[$target]->mafia == 'yes'){
- $action = 1;
- }
- }
- return $target;
- }
- function cmp ($p1,$p2){
- if( $p1->kills == $p2->kills) {
- return 0;
- }
- return ($p1->kills > $p2->kills) ? 1 : -1;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement