Jeg kan jo ikke lave din opgave.
Men her er noget inspiration:
package may;
public class Team {
private String name;
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
}
package may;
public enum Result {
NotPlayed,
HomeWin,
AwayWin
}
package may;
public class Match {
private Team home;
private Team away;
private Result result;
public Match(Team home, Team away) {
this.home = home;
this.away = away;
this.result = Result.NotPlayed;
}
public Team getHome() {
return home;
}
public Team getAway() {
return away;
}
public Result getResult() {
return result;
}
public void homeWin() {
this.result = Result.HomeWin;
}
public void awayWin() {
this.result = Result.AwayWin;
}
@Override
public String toString() {
return String.format("%s-%s:%s", home, away, result);
}
}
package may;
import java.util.ArrayList;
import java.util.List;
public class Tournament {
private List<Team> startTeams;
private List<Team> activeTeams;
private List<Match> round;
private void initActiveTeams() {
if(activeTeams == null) {
activeTeams = new ArrayList<Team>(startTeams);
}
}
public Tournament() {
this.startTeams = new ArrayList<Team>();
this.activeTeams = null;
this.round = null;
}
public List<Team> getStartTeams() {
return startTeams;
}
public List<Team> getActiveTeams() {
initActiveTeams();
return activeTeams;
}
public List<Match> getRound() {
return round;
}
public void planRound() {
initActiveTeams();
round = new ArrayList<Match>();
for(int i = 0; i < activeTeams.size() / 2; i++) {
round.add(new Match(activeTeams.get(i), activeTeams.get(activeTeams.size() - 1 - i)));
}
}
public void processRound() {
activeTeams = new ArrayList<Team>();
for(Match m : round) {
switch(m.getResult()) {
case NotPlayed:
throw new RuntimeException("Round not played");
case HomeWin:
activeTeams.add(m.getHome());
break;
case AwayWin:
activeTeams.add(m.getAway());
break;
}
}
}
}
package may;
import java.util.Random;
public class Test {
private static Random rng = new Random();
public static void main(String[] args) {
Tournament t = new Tournament();
t.getStartTeams().add(new Team("A"));
t.getStartTeams().add(new Team("B"));
t.getStartTeams().add(new Team("C"));
t.getStartTeams().add(new Team("D"));
t.getStartTeams().add(new Team("E"));
t.getStartTeams().add(new Team("F"));
t.getStartTeams().add(new Team("G"));
t.getStartTeams().add(new Team("H"));
while(t.getActiveTeams().size() > 1) {
t.planRound();
System.out.println(t.getRound());
for(Match m : t.getRound()) {
if(rng.nextDouble() < 0.5) {
m.homeWin();
} else {
m.awayWin();
}
}
t.processRound();
System.out.println(t.getRound());
}
System.out.printf("%s won\n", t.getActiveTeams().get(0).getName());
}
}