Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# trivia game: What to do in case of a tie?

Tags:

arrays

c#

loops

I'm currently working on a trivia game. I have written a Team class, a Question class and a Round class.

This is my Team class (I wont post properties, constructors, and methods since they are not relevant to my question).

public class Team
{
    private int _teamNumber = 0;
    private int _score = 0;
}

And this is my Round class:

public class Round
{
    Team[] _teams = new Team[4];
    Question[] _questions = new Clue[30];
    bool _done = true; 
}

The problem I'm having is what to do in case of a tie. There are 8 teams. Two winners from each of the first two Rounds (4 teams each) will qualify for the 3rd and final round.

So in case something like this happens:

currentRound.Teams[0].Score = 300;
currentRound.Teams[1].Score = 300;
currentRound.Teams[2].Score = 100;
currentRound.Teams[3].Score = 350;

As you can see there's a tie for the 2nd place.

I know I can check for repeats, but what if the teams have scores like

500, 400, 200, 200

or

500, 500, 200, 100

In this case there is no need for a tie break, since only the top two teams advance to the next round.

So i was wondering if anyone can help me come up with an algorithm that can help determine if I need a tie-Breaker round or not. And if I do, which teams should we pick and finally what are the top two teams from each round.

Thanks for reading!

like image 860
rememberthecant Avatar asked Oct 09 '22 02:10

rememberthecant


2 Answers

How about using LINQ to determine if there are any teams that are tied for second?

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());

You could probably even remove the if and just do the RunTieBreaker if you use this implementation:

Team RunTieBreaker(List<Team> teamsToTieBreak)
{
    if(teamsToTieBreak.Count == 1)
        return teamsToTieBreak[0];
    //Do tiebreaker
}

Alternatively, you could do an OrderBy and Take(2). Then you could run the Where and/or Any against the 2nd team.

like image 149
Justin Pihony Avatar answered Oct 12 '22 12:10

Justin Pihony


Do something like this:

var result = currentRound.Teams.OrderBy(t => t.Score).GroupBy(t => t.Score).Take(2);
if (result.Sum(m => m.Count()) > 2)
{
    //Need extra round
}
else
{
    //No extra round
}
like image 26
hwcverwe Avatar answered Oct 12 '22 10:10

hwcverwe