Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Created April 24, 2017 19:47
Show Gist options
  • Save joanmolinas/a936afb8aa4a6472d5a970ac1ad97fee to your computer and use it in GitHub Desktop.
Save joanmolinas/a936afb8aa4a6472d5a970ac1ad97fee to your computer and use it in GitHub Desktop.
Random groups for a tournament
import java.util.ArrayList;
import java.util.Random;
class Team {
private String name;
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "{ Team " + name + " }";
}
}
class Pool {
private ArrayList<Team> teams = new ArrayList<>();
private ArrayList<ArrayList<Team>> schema;
private int numberOfGroups = 0, teamsGroup = 0;
public Pool(int numberOfGroups, int teamsGroup) {
this.numberOfGroups = numberOfGroups;
this.teamsGroup = teamsGroup;
schema = new ArrayList<>();
for (int i = 0; i < numberOfGroups; i++) {
ArrayList<Team> group = new ArrayList<>();
schema.add(group);
}
}
public void scheduleTeams() {
ArrayList<Team> copy = new ArrayList<>();
copy.addAll(teams);
Random random = new Random();
while (!copy.isEmpty()) {
int t = random.nextInt(copy.size());
Team team = copy.get(t);
int g = random.nextInt(numberOfGroups);
ArrayList<Team> group = schema.get(g);
if (group.size() < teamsGroup) {
group.add(team);
copy.remove(t);
}
}
System.out.println(schema);
}
public void createTeams() {
teams.add(new Team("Team 1"));
teams.add(new Team("Team 2"));
teams.add(new Team("Team 3"));
teams.add(new Team("Team 4"));
teams.add(new Team("Team 5"));
teams.add(new Team("Team 6"));
teams.add(new Team("Team 7"));
teams.add(new Team("Team 8"));
teams.add(new Team("Team 9"));
teams.add(new Team("Team 10"));
teams.add(new Team("Team 11"));
teams.add(new Team("Team 12"));
}
}
public class Main {
public static void main(String[] args) {
Pool p = new Pool(4, 3);
p.createTeams();
p.scheduleTeams();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment