Last active
December 17, 2017 22:18
-
-
Save lloc/5acf1dd1d0bad8bf0ce2073d52fde25e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace lloc\tournament; | |
/** | |
* Class RoundRobin | |
* | |
* @package lloc\tournament | |
*/ | |
class RoundRobin { | |
/** | |
* @var int | |
*/ | |
protected $players; | |
/** | |
* RoundRobin constructor. | |
* | |
* @param int $players | |
*/ | |
public function __construct( int $players ) { | |
$this->players = $players % 2 == 0 ? $players : $players + 1; | |
} | |
/** | |
* Gets round robin table | |
* | |
* @return array | |
*/ | |
function get() { | |
$result = []; | |
for ( $rounds = 0; $rounds < $this->players - 1; $rounds++ ) { | |
for ( $i = 0; $i < $this->players / 2; $i++ ) { | |
$result[ $rounds ][] = [ | |
( $i == 0 ? 0 : ( $rounds + $i ) % ( $this->players - 1 ) + 1 ), | |
( $this->players - 1 + $rounds - $i ) % ( $this->players - 1 ) + 1, | |
]; | |
} | |
} | |
return $result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment