You are on page 1of 5

import java.util.

ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javafx.concurrent.Task;
import javafx.stage.Stage;

/**
* Purpose: This class represents a Round-Robin tournament where each team
* plays every other team.
* @author Team Green
*
*/
public class RoundRobinTournament extends Tournament
{
//Number of rounds, matchups, and matchups per round
private int numOfRounds;
private int numOfMatchups;
private int numOfMatchPerRound;
private ArrayList<Round> rounds; //The rounds in this tournament
private ArrayList<Team> matchupList; //Matchup list used to divide up matches

/**

* Purpose: Constructor for a RoundRobin tournament. Creates its own


* matchups and rounds.
* @param name
* @param teams
*/
public RoundRobinTournament(String name, ArrayList<Team> teams, int id )
{
super(name, teams, id );
createMatchups();
createRounds();
}

/*
* (non-Javadoc)
*
* @see Tournament#createMatchups()
*/
@Override
protected void createMatchups()
{
int numOfTeams = teams.size();
matchupList = new ArrayList<Team>();
//Determine number of rounds
if (numOfTeams % 2 == 0)
{
numOfRounds = numOfTeams - 1;
}
else
{
numOfRounds = numOfTeams;

teams.add( new Team() );


}
//Split the teams into two halves to combine into a matchup list
int halfSize = teams.size() / 2;
ArrayList<Team> firstHalf = new ArrayList<Team>();
ArrayList<Team> lastHalf = new ArrayList<Team>();
for ( int i = 0; i < halfSize; i++ )
{
firstHalf.add( teams.get( i ));
lastHalf.add( teams.get( halfSize + i ) );
}
Collections.reverse( lastHalf );
ArrayList<Team> combined = new ArrayList<Team>();
combined.addAll(firstHalf);
combined.addAll(lastHalf);
//Create the final matchup list
matchupList.addAll( combined );

//numOfMatchPerRound switched to even number if odd


numOfMatchups = (numOfTeams / 2) * numOfRounds;
if ( numOfTeams % 2 == 0 )
{
numOfMatchPerRound = numOfMatchups / numOfRounds;
}
else
{
numOfMatchPerRound = (numOfMatchups / numOfRounds) + 1;
}

@Override
protected void createRounds()
{
rounds = new ArrayList<Round>();
//For each round, create the required number of matchups
for ( int i = 0; i < numOfRounds; i++ )
{
ArrayList<Matchup> matchesInRound = new ArrayList<Matchup>();
//For each match, get corresponding teams
for ( int j = 0; j < numOfMatchPerRound; j++ )
{
Team teamA = matchupList.get(j);
Team teamB;
teamB = matchupList.get(j + matchupList.size() - 1 - (j * 2));

if ( teamA.getTeamID() == 0 || teamB.getTeamID() == 0 )
{
//Do nothing, bystander match
}
else
{
//Create the new matchup
Matchup match = new Matchup( teamA, teamB );
matchesInRound.add( match );
}
}
//Create a Round from the list of matchups
rounds.add( new Round(matchesInRound));
//Swap around list order to divide up the next matches
int endOfListIndex = matchupList.size() - 1;

Team teamToSwap = matchupList.get( endOfListIndex );


matchupList.remove( teamToSwap );
matchupList.add(1, teamToSwap );
}
}

/**
* Purpose: Retrieves the list of rounds for this tournament.
* @return List of rounds
*/
public ArrayList<Round> getRounds()
{
return rounds;
}

You might also like