Java 如何随机分配具有色散的阵列列表?
我正在尝试做一个应用程序,设置一个比赛计划 我有一个方法:Java 如何随机分配具有色散的阵列列表?,java,algorithm,Java,Algorithm,我正在尝试做一个应用程序,设置一个比赛计划 我有一个方法: public List<Match> creerMatchsTousContreTous(List<Equipe> lEquipe) { List<Match> lMatch = new ArrayList<>(); for (int i = 0; i < lEquipe.size(); i++) { for (int j =
public List<Match> creerMatchsTousContreTous(List<Equipe> lEquipe) {
List<Match> lMatch = new ArrayList<>();
for (int i = 0; i < lEquipe.size(); i++) {
for (int j = i + 1; j < lEquipe.size(); j++) {
Match match = new Match();
match.setEquA(lEquipe.get(i));
match.setEquB(lEquipe.get(j));
lMatch.add(match);
}
}
return lMatch;
}
但这是一个剧本列表,一个特马有可能连续上演两部剧本
我怎样才能做到这一点?
谢谢
致意
编辑:
例如:
该方法返回游戏列表:
creerMatchsTousContreTous()在本例中返回一个包含6个值的列表。但在这里,例如,在第一场比赛中,第2队正在比赛,在第二场比赛中,他也在比赛,这一定不是。我建议在TEAM类中添加一个布尔变量,例如justplay或hasplay。该变量将跟踪某个特定球队是否刚刚参加过比赛
Collections.shuffle(lMatch); // get two random teams using shuffle
while(Match.Team1.getHasPlayed() == True or Match.Team2.getHasPlayed() == True){
Collections.shuffle(lMatch); // try to find different teams
}
lMatch.play(); // you've found two teams, so now you can call your play method
for(Team t:lEquipe){ // go through the list of teams and
t.setHasPlayed(false); // reset the rest each team's boolean
}
Match.Team1.setHasPlayed(true);
Match.Team2.setHasPlayed(true); // set the boolean to true at the end of the turn
//
这显然是伪代码,因为我不知道您对Match和Team的实现。不过,考虑使用布尔实例字段并检查它是否在上一轮中被修改。 < P>下面的是递归方法。我认为它可以以比@Sonedring建议的更快的方式提供解决方案,因为每次随机化后都会应用约束 如果你的队伍少于4支,那么在拐角处也更安全。在这种情况下,您将无法找到解决方案,也无法运行无止境的循环 希望这有帮助
public static void randomize(List<Match> matches) {
List<Match> randomizedList = new ArrayList<>();
int numberOfAttempts = 256;
// tmpSubList is a temporary list that contains all matches
// (excluding unwanted) after n-th iteration (randomization).
List<Match> tmpSubList = new ArrayList<Match>(matches);
while (matches.size() > 0) {
// if tmpSubList contains - it means there is no match that can be added.
// Need to restart randomization algorithm.
if (tmpSubList.size() == 0) {
System.out.println("Restarting algorithm.");
if (--numberOfAttempts == 0) {
throw new ArithmeticException("Could not find solution.");
}
// Need to restart:
matches.addAll(randomizedList);
tmpSubList.addAll(randomizedList);
randomizedList.clear();
}
int randomIndex = (int) (tmpSubList.size() * Math.random());
Match match = tmpSubList.remove(randomIndex);
matches.remove(match); // remove also from the main list;
randomizedList.add(match);
Equipe lastTeam1 = match.getEquA();
Equipe lastTeam2 = match.getEquB();
tmpSubList.clear();
matches.stream()
.filter( x -> !x.getEquA().equals(lastTeam1) && !x.getEquB().equals(lastTeam1) )
.filter( x -> !x.getEquA().equals(lastTeam2) && !x.getEquB().equals(lastTeam2) )
.forEach( x -> tmpSubList.add(x));
}
matches.addAll(randomizedList);
}
publicstaticvoidrandomize(列表匹配){
List randomizedList=新建ArrayList();
int numberofthresents=256;
//tmpSubList是包含所有匹配项的临时列表
//第n次迭代(随机化)后(不包括不需要的)。
List tmpSubList=新阵列列表(匹配项);
while(匹配.size()>0){
//如果tmpSubList包含-则表示无法添加匹配项。
//需要重新启动随机化算法。
如果(tmpSubList.size()==0){
System.out.println(“重新启动算法”);
如果(--numberOfAttempts==0){
抛出新的算术异常(“找不到解决方案”);
}
//需要重新启动:
matches.addAll(随机化列表);
tmpSubList.addAll(随机化列表);
randomizedList.clear();
}
int randomIndex=(int)(tmpSubList.size()*Math.random());
Match=tmpSubList.remove(随机索引);
匹配项。删除(匹配);//也从主列表中删除;
随机列表。添加(匹配);
Equipe lastTeam1=match.getEquA();
Equipe lastTeam2=match.geteqb();
tmpSubList.clear();
matches.stream()
.filter(x->!x.getEquA().equals(lastTeam1)和&!x.getEquA().equals(lastTeam1))
.filter(x->!x.getEquA().equals(lastTeam2)和&!x.getEquA().equals(lastTeam2))
.forEach(x->tmpSubList.add(x));
}
matches.addAll(随机化列表);
}
我在问题中添加了一个示例。lMatch是一个游戏列表。基本上,我上面提供的实现应该仍然有效。您应该在team类中创建一个布尔变量。此变量确定一个队是否有资格参加比赛。由于您已经知道如何创建lMatch列表,所以只需检查两支随机选择的球队是否在上一场比赛中进行了比赛。
public static void randomize(List<Match> matches) {
List<Match> randomizedList = new ArrayList<>();
int numberOfAttempts = 256;
// tmpSubList is a temporary list that contains all matches
// (excluding unwanted) after n-th iteration (randomization).
List<Match> tmpSubList = new ArrayList<Match>(matches);
while (matches.size() > 0) {
// if tmpSubList contains - it means there is no match that can be added.
// Need to restart randomization algorithm.
if (tmpSubList.size() == 0) {
System.out.println("Restarting algorithm.");
if (--numberOfAttempts == 0) {
throw new ArithmeticException("Could not find solution.");
}
// Need to restart:
matches.addAll(randomizedList);
tmpSubList.addAll(randomizedList);
randomizedList.clear();
}
int randomIndex = (int) (tmpSubList.size() * Math.random());
Match match = tmpSubList.remove(randomIndex);
matches.remove(match); // remove also from the main list;
randomizedList.add(match);
Equipe lastTeam1 = match.getEquA();
Equipe lastTeam2 = match.getEquB();
tmpSubList.clear();
matches.stream()
.filter( x -> !x.getEquA().equals(lastTeam1) && !x.getEquB().equals(lastTeam1) )
.filter( x -> !x.getEquA().equals(lastTeam2) && !x.getEquB().equals(lastTeam2) )
.forEach( x -> tmpSubList.add(x));
}
matches.addAll(randomizedList);
}