1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Selecting a target for everyone

Discussion in 'Development' started by wolfdale, Sep 29, 2017.

  1. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    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:
    <?php
    class 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;
        }
    }
    }
     
    Last edited: Sep 29, 2017
  2. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    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?
     
    Last edited: Sep 30, 2017
    Thouv, wolfdale and jasonwynn10 like this.
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Wooohooo it works, thanks so much!
     
    Awzaw likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.