Skip to content

Instantly share code, notes, and snippets.

@lloc
Last active December 17, 2017 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lloc/5acf1dd1d0bad8bf0ce2073d52fde25e to your computer and use it in GitHub Desktop.
Save lloc/5acf1dd1d0bad8bf0ce2073d52fde25e to your computer and use it in GitHub Desktop.
<?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