I have a Single Round Robin Tournament script that was originally written in PHP to generate a playing schedule.(我有一个Single Round Robin Tournament脚本,该脚本最初是用PHP编写的,用于生成播放时间表。)
I have ported the code into Typescript, see below:(我已将代码移植到Typescript中,如下所示:)
export = class SingleRoundRobinTournament {
constructor(private teamsCount: number) {
// If teamsCount is an odd number, add one number
if (teamsCount % 2 == 1) teamsCount + 1;
this.teamsCount = teamsCount;
this.constructTournament();
}
// Function to create pairings
constructTournament() {
// Used in formula
const halfTeamsCount: number = this.teamsCount / 2;
// Create pairings array
let pairings: number[][][] = [];
// Assign home and away variables
let team1: number;
let team2: number;
// Set rounds initial value
let rounds: number = 0;
// Cycle through formula to create random team pairings
for (let teamsCounter = 1; teamsCounter < this.teamsCount; teamsCounter++) {
// Increment counter
rounds += 1;
console.log(rounds);
console.log(this.teamsCount);
console.log(teamsCounter);
if (teamsCounter % 2 === 0) {
pairings[rounds][0][0] = this.teamsCount;
pairings[rounds][0][1] = teamsCounter;
} else {
pairings[rounds][0][0] = teamsCounter;
pairings[rounds][0][1] = this.teamsCount;
}
for (
let matchesCounter = 1;
matchesCounter < halfTeamsCount;
matchesCounter++
) {
team1 = (teamsCounter + matchesCounter) % (this.teamsCount - 1);
if (team1 === 0) {
team1 = this.teamsCount - 1;
}
if (teamsCounter - matchesCounter <= 0) {
team2 = this.teamsCount - 1 + (teamsCounter - matchesCounter);
} else {
team2 = (teamsCounter - matchesCounter) % (this.teamsCount - 1);
}
if (matchesCounter % 2) {
pairings[rounds][matchesCounter][0] = team1;
pairings[rounds][matchesCounter][1] = team2;
} else {
pairings[rounds][matchesCounter][0] = team2;
pairings[rounds][matchesCounter][1] = team1;
}
}
}
return pairings;
}
};
However, I get the following error:(但是,出现以下错误:)
pairings[rounds][0][0] = teamsCounter;
^
TypeError: Cannot read property '0' of undefined
Has anyone got any ideas as to what I'm getting wrong here?(有谁对我在这里犯错有任何想法吗?) Any help would be greatly appreciated.(任何帮助将不胜感激。) Strangely enough I got the same error when I ported the code into python.(奇怪的是,当我将代码移植到python中时,我遇到了相同的错误。) I've posted the original and working PHP script below: Cheers(我在下面发布了原始且有效的PHP脚本:干杯)
<?php
class SingleRoundRobin
{
/**
* The number of teams entered for the single round robin tournament.
*
* @var string
*/
private $teamsCount;
/**
* The half number of teams entered for the single round robin tournament.
*
* @var string
*/
private $halfTeamsCount;
/**
* Pairings generated before being randomised.
*
* @var array
*/
private $pairings = array();
/**
* @param array $teamsCount
* @return void
*/
public function __construct($teamsCount)
{
// If the teams count is odd, an extra number is added.
if ($teamsCount % 2 == 1) $teamsCount++;
$this->teamsCount = $teamsCount;
$this->halfTeamsCount = $teamsCount / 2;
}
/**
* Create a single round robin teams array.
*
* @return array
*/
public function singleRoundRobinCreate()
{
// Half teams count
$halfTeamsCount = $this->teamsCount / 2;
// Teams Counter
$rounds = 0;
for ($teamsCounter = 1; $teamsCounter < $this->teamsCount; $teamsCounter++) {
// Rounds counter.
$rounds++;
if ($teamsCounter % 2 == 0) {
$pairings[$rounds][0][0] = $this->teamsCount;
$pairings[$rounds][0][1] = $teamsCounter;
} else {
$pairings[$rounds][0][0] = $teamsCounter;
$pairings[$rounds][0][1] = $this->teamsCount;
}
// Generate the other ((n / 2) -1 matches).
for ($matchesCounter = 1; $matchesCounter < $this->halfTeamsCount; $matchesCounter++) {
$team1 = ($teamsCounter + $matchesCounter) % ($this->teamsCount - 1);
if ($team1 == 0) {
$team1 = $this->teamsCount - 1;
}
if ($teamsCounter - $matchesCounter <= 0) {
$team2 = $this->teamsCount -1 + ($teamsCounter - $matchesCounter);
} else {
$team2 = ($teamsCounter - $matchesCounter) % ($this->teamsCount - 1);
}
if ($matchesCounter % 2) {
$pairings[$rounds][$matchesCounter][0] = $team1;
$pairings[$rounds][$matchesCounter][1] = $team2;
} else {
$pairings[$rounds][$matchesCounter][0] = $team2;
$pairings[$rounds][$matchesCounter][1] = $team1;
}
}
}
return $pairings;
}
}
ask by Jeoff Morris translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…