<?php

/**
 * Created by PhpStorm.
 * User: Pablo García Sanz
 * Date: 22/02/2018
 * Time: 18:48
 */
//include "application/src/AlgoritmoEmparejamiento/IEmparejamiento.php";

class EmparejamientoAleatorio implements IEmparejamiento
{
    const PAR = 0;
    const IMPAR = 1;
    public function emparejar($competidores)
    {


            if(sizeof($competidores) == 2) {
                return array($competidores[0],$competidores[1]);
            }
            else {
                switch (sizeof($competidores) % 2) {
                    case self::PAR : {

                        while(sizeof($competidores) != 0) {

                            $id1= array_rand($competidores);
                            $cblue = $competidores[$id1];
                            unset($competidores[$id1]);

                            $id2= array_rand($competidores);
                            $cwhite = $competidores[$id2];
                            unset($competidores[$id2]);

                            $z = array($cwhite,$cblue);
                            sort($z);
                            $combates []= $z;
                        }
                        return $combates;
                    }
                    case self::IMPAR: {
                        unset($competidores[array_rand($competidores)]);
                        while(sizeof($competidores) != 0) {

                            $id1= array_rand($competidores);
                            $cblue = $competidores[$id1];
                            unset($competidores[$id1]);

                            $id2= array_rand($competidores);
                            $cwhite = $competidores[$id2];
                            unset($competidores[$id2]);

                            $z = array($cwhite,$cblue);
                            sort($z);
                            $combates []= $z;
                        }
                        return $combates;
                    }

                    }
                }

    }
}