在游戏中取得两队最大的领先优势

问题描述 投票:0回答:2

我有一个 Game 涉及2个团队。该 Game 有一个列表 ScoreEvents. 每一个ScoreEvent对得分的队伍来说都是1分,我需要知道每个队伍的最大领先分数是多少(如果他们从来没有领先过,则为0)。我需要知道每支队伍的最大领先分数是多少(如果他们从未领先过,则为0)。我需要知道每支队伍的最大领先分数是多少(如果他们从未领先过,则为0)。ScoreEvents 名单按以下顺序排列 TimeSinceStart.

public class ScoreEvent
{
    public int TeamId { get; set; }
    public TimeSpan TimeSinceStart { get; set; }
}

public void GetMaxScoreLead()
{
    var ScoreEvents = new List<ScoreEvent>
    {
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(100)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(200)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(300)},

        //Score at 300 ticks is 3-0 to TeamdId = 0

        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(400)},
        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(500)},
        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(600)},
        new ScoreEvent { TeamId = 1, TimeSinceStart = new TimeSpan(700)},

        //Score at 700 ticks is a 3-4 to TeamId = 1

        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(800)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(900)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(1000)},
        new ScoreEvent { TeamId = 0, TimeSinceStart = new TimeSpan(1100)}

        //Score at 1100 ticks is 7-4 to TeamId 0
    };
}

因此,对于上面的例子,每个团队的最大领先优势的答案将是。

  1. TeamId (0) = 3 最大的领先优势
  2. 团队ID(1)=1个最大的线索

EDIT:代码,我必须要。我知道我需要在某个地方记录当前的分数。

var teamZeroLargestLead = 0;
var teamOneLargestLead = 0;

var internalTeamZeroLargestLead = 0;
var internalTeamOneLargestLead = 0;

foreach (var scoreEvent in scoreEvents.OrderBy(x => x.TimeSinceStart))
{
    if (scoreEvent.TeamId == 0)
    {
        if (internalTeamOneLargestLead > teamOneLargestLead)
        {
             teamOneLargestLead = internalTeamOneLargestLead;
             internalTeamOneLargestLead = 0;
        }

        internalTeamZeroLargestLead += 1;
     }
     else
     {
        if(internalTeamZeroLargestLead > teamZeroLargestLead)
        {
            teamZeroLargestLead = internalTeamZeroLargestLead;
            internalTeamZeroLargestLead = 0;
        }

        internalTeamOneLargestLead += 1;
    }
}
c# grouping timespan
2个回答
2
投票

我稍稍地更新和简化了你的算法 foreach 循环,现在它返回了正确的结果 - teamZeroLead3, teamOneLead1.

var teamZeroLead = 0;
var teamOneLead = 0;
var teamZeroScore = 0;
var teamOneScore = 0;

foreach (var scoreEvent in scoreEvents.OrderBy(x => x.TimeSinceStart))
{
    if (scoreEvent.TeamId == 0)
    {
        teamZeroScore++;
        teamZeroLead = Math.Max(teamZeroLead, teamZeroScore - teamOneScore);
    }
    else
    {
         teamOneScore++;
         teamOneLead = Math.Max(teamOneLead, teamOneScore - teamZeroScore);
    }
}

在每一次循环迭代中,你都在计算每支队伍的当前得分,然后计算领先值,并将其赋值给结果值,如果它大于之前计算的值。

同样的逻辑可以用 Aggregate 方法和值元组,你可以选择对你来说更易读、更方便的方法。

var result = scoreEvents.Aggregate((teamZeroLead: 0, teamOneLead: 0, teamZeroScore: 0, teamOneScore: 0),
    (scores, scoreEvent) =>
    {
        if (scoreEvent.TeamId == 0)
        {
            scores.teamZeroScore++;
            scores.teamZeroLead = Math.Max(scores.teamZeroLead, scores.teamZeroScore - scores.teamOneScore);
        }
        else
        {
            scores.teamOneScore++;
            scores.teamOneLead = Math.Max(scores.teamOneLead, scores.teamOneScore - scores.teamZeroScore);
        }

        return scores;
    });

执行后,你可以使用 result.teamZeroLeadresult.teamOneLead


2
投票
var leftTeamId = ScoreEvents.First().TeamId
var res = ScoreEvents
  .OrderBy(x => x.TimeSinceStart)
  .Aggregate(
    (max: 0, min: 0, curr: 0), 
    (acc, currSE)  => {
        var  curr = currSE.TeamId == leftTeamId
            ? acc.curr +1
            : acc.curr - 1;
        if(curr > acc.max)
        {
            return (curr, acc.min, curr);
        }
        else if (curr < acc.min)
        {
            return (acc.max, curr, curr);
        }
        return (acc.max, acc.min, curr);
    });

而对于 "左 "的团队,其ID为 leftTeamId 你用 res.max 对于 "正确 "的团队,你用 Math.Abs(res.min):

  1. TeamId (0) = res.max 最大线索
  2. TeamId (1) = Math.Abs(res.min) 最大线索

我没有得到rightTeamId,因为理论上只有一支队伍可以得分(但假设至少有一支队伍得分了=)。


2
投票

由于只有两支队伍,看看这个方法是否能满足你的需求。

        int team1Score = 0;
        int team2Score = 0;
        int maximumLead = 0;
        int maximumLeadTeamId = -1;
        for (int i = 0; i < ScoreEvents.Count; i++)
        {
            if (ScoreEvents[i].TeamId == 0)
            {
                team1Score++;
            }
            else
            {
                team2Score++;
            }
            int currentLead = Math.Abs(team1Score - team2Score);
            if (currentLead > maximumLead)
            {
                maximumLead = currentLead;
                maximumLeadTeamId = ScoreEvents[i].TeamId;
            }
        }

maximumLeadTeamId是整场比赛中最大的领先球队的Id,maximumLead是两队之间的最大进球数差。


1
投票

你可以用这个来获取球队id的字典,在指定的时间段内进行得分。

public static Dictionary<int, int> GetMaxScoreLead(IEnumerable<ScoreEvent> scoreEvents, TimeSpan time)
{
    var scoreDictionary = new Dictionary<int, int>();
    var grouping = scoreEvents.Where(e => e.TimeSinceStart <= time).GroupBy(e => e.TeamId);

    foreach (var group in grouping)
    {
        scoreDictionary.Add(group.Key, group.Count());
    }

    return scoreDictionary;
}

这个代码不取决于球队的数量。你可以从这个字典结构中琐碎地确定获胜球队。比如说

var winningTeam = getScores.OrderByDescending(x => x.Value).FirstOrDefault();

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.