I am creating a minigame where everyone needs to have a target to kill However I have a issue that the target of a player is also the assassin of him, in other words A becomes the assassin of B and B becomes the assassin of A and that is not what I wanted What I wanted was A->B means A is supposed to kill B A->B->C->D->E->A players should be shuffled, like B->A->E->D->C->B Anyone thought of a good way to do this? The code Im using now is PHP: <?phpclass Test{ public $players = ['A','B','C','D','E'];public $assasins = [];public $targets = []; public $first = true;function chooseTargetfor($player){ $targets = []; $name = $player; if($this->first === true) $this->first = $name; foreach($this->players as $p){ if($p !== $name){ if(!isset($this->assasins[$p]) && ($this->targets[$p]??"") !== $name#prevent $name from becoming assasin of his assasin && $p !== $this->first#prevent anyone from closing the loop ){ $targets[] = $p; } } } if(!count($targets)){ #close the loop here $targets = [$this->first]; var_dump("forced target of $name to $this->first"); #return; } foreach($targets as $t) if($this->targetOf($t) === $name){ var_dump("target of $t is already $name, $name cannot be assasin of $t"); throw new Exception("LOOP DETECTED"); } $this->targets[$name] = $targets[mt_rand(0,count($targets)-1)]; $this->assasins[$this->targets[$name]] = $name; var_dump("Target for $name is ".$this->targetOf($name)." choosen from ".implode(' ',$targets)); } function targetOf($name){ return $this->targets[$name]??""; } function assassinOf($name){ return $this->assasins[$name]??""; }}for($i = 0; $i <= 50;$i++){$t = new Test;shuffle($t->players);foreach($t->players as $p){ try{ $t->chooseTargetfor($p); } catch(Exception $e){ var_dump($e->getMessage()." at $i"); break 2; }}}
Why can't you just shuffle the array of players and then define targets and assassins as you did in the example B->A->E->D->C? So each players target is simply the next element in the array (or the first if it's the last), and each players assassin is the previous one (or the last if it's the first). Or did I misunderstand something?