我正在制作一个琐事游戏.我写了一个Team类,一个Question类和一个Round类.
这是我的Team类(我不会发布属性,构造函数和方法,因为它们与我的问题无关).
public class Team
{
private int _teamNumber = 0;
private int _score = 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的Round课程:
public class Round
{
Team[] _teams = new Team[4];
Question[] _questions = new Clue[30];
bool _done = true;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是如果出现问题该怎么做.有8支球队.前两轮(每队4支)中的两名获胜者将有资格参加第三轮和最后一轮.
所以如果发生这样的事情:
currentRound.Teams[0].Score = 300;
currentRound.Teams[1].Score = 300;
currentRound.Teams[2].Score = 100;
currentRound.Teams[3].Score = 350;
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,第二名是平局.
我知道我可以检查重复,但是如果球队有得分的话会怎样
500,400,200,200
要么
500,500,200,100
在这种情况下,没有必要打破平局,因为只有前两支球队才能进入下一轮.
所以我想知道是否有人可以帮我提出一个算法,可以帮助确定我是否需要打破平局.如果我这样做,我们应该选择哪支球队,最后是每一轮的前两支球队.
谢谢阅读!
使用 LINQ 来确定是否有并列第二的团队怎么样?
var orderedResults = currentRound.Teams.OrderBy(x=>x.Score).ToList();
if(orderedResults.Any(x=>x.Score == orderedResults[1].Score))
var team2 = RunTieBreaker(
orderedResults.Where(x=>x.Score == orderedResults[1].Score).ToList());
Run Code Online (Sandbox Code Playgroud)
如果您使用此实现,您甚至可以删除 if 并只执行 RunTieBreaker:
Team RunTieBreaker(List<Team> teamsToTieBreak)
{
if(teamsToTieBreak.Count == 1)
return teamsToTieBreak[0];
//Do tiebreaker
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以执行OrderByand Take(2)。然后你可以Where和/或Any对抗第二支球队。