【问题标题】:How to get the Total points for each team in a Tournament Game and get the winner如何在锦标赛游戏中获得每支球队的总分并获得冠军
【发布时间】:2018-09-27 16:35:35
【问题描述】:

我们想要设计一个简单的锦标赛,由具有名称和国籍的团队组成。在本次锦标赛中,受邀球队之间组织了一组比赛,每场比赛对阵两支球队。得分最高的球队赢得比赛。如果比赛结果是平局,每队得 1 分,胜方得 2 分,负方得 2 分。我们想知道一支球队在锦标赛中的总分,以知道获胜者。获胜者是得分最高的人。

所以我们设法创建了三个类:Team、Match 和 Tournament 以及主类。

在主类中我们有这个

public class ProgramTournaments {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    //Defining each team
    Team frTeam, inTeam, cnTeam;

    //Creation of three objects (Teams)
    frTeam = new Team("French Blue Team", "French"); // New Means I want to create an Object (frTeams)
    inTeam = new Team("Indian Blue Team", "India");
    cnTeam = new Team("Chinese Red Team", "China");

    //Create a new Tournament
    Tournament tournament = new Tournament();

    //Invite teams to the tourname
    tournament.inviteTeam(frTeam);
    tournament.inviteTeam(inTeam);
    tournament.inviteTeam(cnTeam);

    //Add matches to Tournament
    Match m1 = new Match(frTeam, inTeam, true);
    Match m2 = new Match(frTeam, cnTeam, true);
    Match m3 = new Match(inTeam, cnTeam, true);

    tournament.addMatch(m1);
    tournament.addMatch(m2);
    tournament.addMatch(m3);

    //Check If all matches Have been Pleayed
    tournament.allMatchPlayed();
}
  }

在团队课中我们这样做了

public class Team {

//Defining the attributes
private String name;  //Private means it is limited only to this Class (team)
private String citizenship;

public String getName() {
    return name;
}

public String getCitizenship() {
    return citizenship;
}

// Constructor inorder to initialized values
public Team (String name, String citizenship){
    this.name = name; //Initializing name of team
    this.citizenship = citizenship; //Initializing name of Citizenship of team

}

//Printing to strings
@Override
public String toString() {
    return "Team{" + "name=" + name + ", citizenship=" + citizenship + '}';
} 
  }

在 Match 类中我们这样做了

public class Match {

private Team team1, team2;
private int scoreTeam1;
private int scoreTeam2;
private int pointTeam1, pointTeam2;
boolean play;

//Constructor
public Match(Team team1, Team team2, boolean play) {
    this.team1 = team1;
    this.team2 = team2;
    this.scoreTeam1 = generateRandomScore();
    this.scoreTeam2 = generateRandomScore();
    this.play = play;
}

//All Methods
public int getScoreTeam1() {
    return scoreTeam1;
}

public void setScoreTeam1(int scoreTeam1) {
    this.scoreTeam1 = scoreTeam1;
}

public int getScoreTeam2() {
    return scoreTeam2;
}

public void setScoreTeam2(int scoreTeam2) {
    this.scoreTeam2 = scoreTeam2;
}

public Team getTeam1() {
    return team1;
}

public void setTeam1(Team team1) {
    this.team1 = team1;
}

public Team getTeam2() {
    return team2;
}

public void setTeam2(Team team2) {
    this.team2 = team2;
}

public boolean isPlay() {
    return play;
}

public void setPlay(boolean play) {
    this.play = play;
}

//Generate Random Score
private int generateRandomScore() {
    Random random = new Random();
    return random.nextInt(5);
}

public boolean draw() {
    if (scoreTeam1 == scoreTeam2) {
        pointTeam1 = 1;
        pointTeam2 = 1;
        return true;
    }

    return false;
}

public Team matchWinner() {
    if (scoreTeam1 > scoreTeam2) {
        pointTeam1 = 2;
        pointTeam2 = 0;
        return team1;
    } else {
        pointTeam2 = 2;
        pointTeam1 = 0;
        return team2;
    }
}
  }

在锦标赛课上我们这样做了

public class Tournament {

private List<Team> ListOfTeams = new ArrayList<>();
private List<Match> ListOfMatches = new ArrayList<>();

//Methods
public void inviteTeam(Team team) { //Inviting Teams
    ListOfTeams.add(team);
}

public void addMatch(Match m) {
    ListOfMatches.add(m);
}

public boolean allMatchPlayed() {
    for (Match match : ListOfMatches) {
        if (match.isPlay() == false) {
            return false;
        }
    }

    return true;

}
 public void tournamentWinner(){
   for (Match match : ListOfMatches){
     match.decideResult();
  }
 Comparator <Team> team = new Comparator<Team>(){
    @override
       public int compare(Team t1, Team t2){
         return t1.getScore() - t2.getScore(); 
        }
   };

  Collections.sort(ListOfTeams, t);
  System.out.println("The winner of the tournament is: " + ListOfTeams);
 }


   }

所以拜托,我们一直在尝试为每支球队实施总分,并根据总分获得胜利者

【问题讨论】:

  • 为什么不将points成员变量从Match移动到Team呢?那里更有意义,因为每个团队都会有一些分数。
  • 我们所做的是,在 Match 类中,对于一场比赛,一支球队可以得到 0、1 或 2 分。我们现在要做的是,通过我们创建的比赛,为每支球队和每场比赛增加分数,但我们不知道在哪里存储增加的变量来计算每支球队的总分

标签: java class oop


【解决方案1】:

我建议将 points 成员变量从 Match 移动到 Team。原因是每个团队在任何时间点都会有一些积分,所以每个团队都有一个积分字段是有道理的。

现在您将对方法进行以下更改

Team.java

public class Team {
   private int points;
   // getters and setters for points

   /* Rest of your class */
}

Match.java

我们应该将您的draw()matchWinner() 组合成一种方法,例如decideResult(),因为它们自己没有任何意义。

public void decideResult() {
    if (scoreTeam1 == scoreTeam2) {
        team1.setPoints(team1.getPoints() + 1);
        team2.setPoints(team2.getPoints() + 1);
    } else if (scoreTeam1 > scoreTeam2) {
        team1.setPoints(team1.getPoints() + 2);
    } else {
        team2.setPoints(team2.getPoints() + 2);
    }
}

要找到获胜者,您只需从相应的 Team 对象中获取分数。例如:frTeam.getPoints() 并将其与其他国家/地区进行比较.getPoints()

【讨论】:

  • 非常感谢它非常有帮助。我们添加了 matchWinner,看起来没问题。目前在主班,我们只是创建了三个团队来尝试我们的程序,但是以后我们会有很多团队,所以很难一一比较每个团队的分数。你有什么想法吗?
  • 如果您有很多团队,您可以将所有团队添加到集合中并按点数对其进行排序。这将消除您的手动工作。
  • 关于decisionResult(),我们更喜欢同时保留draw()和matchWinner(),因为稍后我们想在平局等情况下增加一个加时赛
  • 嗯,如果是直接来自decideResult()的平局,为什么不调用超时方法:)
  • 我们尝试按照您的建议进行比较以获得获胜者,但我们在这样做时遇到了问题。我们试图在锦标赛课上进行比较,但我们感到困惑?你能帮帮我们吗?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
相关资源
最近更新 更多