Alghoritm for a league calendar generation

We are lookin' for an algorithm that permits to create a combination of matches like a soccer league calendar. (not home, away, only home)Example for 4 teamsTeams: A B C DTurn1: AB CDTurn2: AC BDTurn3: AD BC
[257 byte] By [frittomistico] at [2007-9-30 10:35:26]
# 1
Only for 4 teams right ? beacusa if I had 6 teams... Example for 6 teamsTeams: A B C D E FTurn1: AB CD EFTurn2: AC BD EFTurn3: AD BC EF
jfreebsd at 2007-7-3 19:29:31 > top of Java-index,Other Topics,Algorithms...
# 2
sorry, i got it..
jfreebsd at 2007-7-3 19:29:31 > top of Java-index,Other Topics,Algorithms...
# 3

Try this code... i didn't execute it but seems to be ok.

public Vector getTurns(String [] teams) {

Vector turns = new Vector();

fillTurns(teams, turns);

return turns;

}

public void fillTurns(String [] teams, Vector turns) {

for(int i = 0 ; i < teams.length ; i++) {

fillTurns(teams, teams [i], turns);

}

}

public void fillTurns(String [] teams, String currentTeam, Vector turns) {

for(int i = 0, j = 0 ; i < teams.length ; i++) {

if(!(teams [i].equals(currentTeam))) {

if(!(alreadyStored(teams [i], currentTeam, j, turns))) {

fillTurn(currentTeam, teams [i], turns, j);

}

j++;

}

}

}

private void fillTurn(String currentTeam, String aTeam, Vector turns, int j) {

Vector turn = (Vector) turns.elementAt(j);

if(turn == null) {

turn = new Vector();

}

turn.add(new String [] {currentTeam, aTeam});

}

private boolean alreadyStored(String aTeam, String currentTeam, int j, Vector turns) {

Vector turn = (Vector) turns.elementAt(j);

if(turn != null) {

for(int i = 0 ; i < turns.size() ; i++) {

String [] game = (String []) turns.elementAt(i);

if(game [0].equals(currentTeam) || game [1].equals(currentTeam)) {

return true;

}

}

}

return false;

}

}

jfreebsd at 2007-7-3 19:29:31 > top of Java-index,Other Topics,Algorithms...
# 4

Here's something I posted a while back, modify to suit

import java.io.*;

class RoundRobin

{

String[] team;

public RoundRobin()

{

try

{

BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number of teams: ");

int number = Integer.parseInt(input.readLine());

input.close();

setTheTeams(number);

printTheMatches();

}

catch(Exception e){System.out.println(e.getMessage());}

System.exit(0);

}

public void setTheTeams(int numberOfTeams)

{

int marker = 0;

if(numberOfTeams % 2 == 1) marker++;

team = new String[numberOfTeams + marker];

if(marker > 0) team[0] = " Bye ";

for(int x = 0; x < team.length-marker; x++) team[x+marker] = "Team"+(x+1);

}

public void printTheMatches()

{

int matches = team.length-1;

String temp = "";

for(int x = 0; x < matches; x++)

{

System.out.println("\nRound "+(x+1)+"...");

for(int y = 0, z = matches; y < (matches+1)/2; y++,z--)

{

System.out.println(team[y]+" vs "+team[z]);

}

temp = team[matches];

for(int z = matches; z > 1; z--) team[z] = team[z-1];

team[1] = temp;

}

}

public static void main(String[] args){new RoundRobin();}

}

Michael_Dunn at 2007-7-3 19:29:31 > top of Java-index,Other Topics,Algorithms...
# 5
Tnks to everyone!
frittomistico at 2007-7-3 19:29:32 > top of Java-index,Other Topics,Algorithms...