列表与LT;>包含问题

时间:2013-08-07 14:02:51

标签: c#

编辑 - 检查已移至其他位置,循环播放多个对象更改现在是首要问题

我需要5个团队对象互相对抗(每个对象必须与其他4个团队对战)

Check必须在ELSE语句中,现在唯一的问题是得分 同时加入和减去所有球队,我只玩一次,我需要他们循环 所以他们都对抗每个人

我就是这样开始的。但我不知道如何去做这个

//This is the list i check against and add players to when the team
//Played against them

List<string> played = new List<string>();

//Teamlist Holds Objects of type Team 
//Contructor - (TeamName,Points,Wins,Losses,Draws,totalpoints)
//string, rest are int type

foreach (var item in Teamlist)
{
    if (played.Contains(item.Teamname))
    {
    //Played against them already or is the same name as their own
    }
    else
    {
     //Add own Name on Check
    played.Add(item.Teamname);

        //Looping here against all Teams Entered to be played against
        //How do i go about doing this?

        //If Team 1 Wins Team 2 - This is just 2 Randoms between 1
        //and 10
        if (rand1 > rand2)
        {
            item.WinGame(); //Increments Team win Points
        }
        else if (rand1 == rand2)
        {
            item.DrawGame(); //Draw awards no points
        }
        else
        {
            item.LoseGame(); //Lose Points
        }
    }
}

foreach (var item in Teamlist)
{
    //This ToString() looks like this
    //return string.Format("Team Name : {0} \t {1} \t {2} \t {3} \t 
    //{4} \t {5}", teamname, points, win, loss, draw, totalpoints);
    Console.WriteLine(item.ToString());
}

8 个答案:

答案 0 :(得分:2)

也许是因为你犯了一个小错误:

首先添加item.Teamname

//Add own Name on Check
played.Add(item.Teamname);

然后检查先前添加的item.Teammname是否在列表中 - 始终为true:

if (played.Contains(item.Teamname))
{

答案 1 :(得分:2)

编辑:这是错误的。它会给你主场和客场比赛。

如何获得团队列表中的Cartesian Product来为您提供要播放的比赛列表。使用Linq非常简单:

static void Main(string[] args)
{
    var teams = new[]
    {
        new { Name = "Team 1"},
        new { Name = "Team 2"},
        new { Name = "Team 3"},
        new { Name = "Team 4"},
        new { Name = "Team 5"}
    };

    var matches = 
        // here we loop over all the items in the teams collection. 
        // "teamA" is our loop variable.
        from teamA in teams

        // here we loop over all the items in the teams collection again.
        // like a nested foreach (in) 
        // "teamB" is our loop variable.
        from teamB in teams

        // this is like an if(teamA.Name != teamB.Name) within our nested foreach loops
        where teamA.Name != teamB.Name

        // the select says how we want to create our collection of results.
        // Here we're creating a new anonymous object containing the two rival teams.
        select new { Team1 = teamA, Team2 = teamB };

    foreach (var match in matches)
    {
        var result = PlayMatch(match.Team1, match.Team2);
        Console.WriteLine("{0} played {1} The result was {2}", 
            match.Team1.Name, 
            match.Team2.Name, 
            result);
    }
}

private static string PlayMatch(object team1, object team2)
{
    // Left as an exercise for the OP.
    return "...";
}

答案 2 :(得分:1)

这就是你的问题:

played.Add(item.Teamname);

if (played.Contains(item.Teamname))

第一行将团队名称添加到播放列表,第二行检查团队名称是否在列表中。这应该永远是真的,所以你永远不会进入其他路径......

答案 3 :(得分:1)

假设teamList is IList<Team>

for(int i = 0;     i < teamList.Count() - 1; i++)
for(int j = i + 1; j < teamList.Count();     j++)
{
   var team1 = teamList[i];
   var team2 = teamList[j];
   // will execute for each pair once
}

对于5支球队:

  • 0次1,2,3,4
  • 1次2,3,4
  • 2次3,4
  • 3次4

答案 4 :(得分:1)

要修复您的代码,您需要在其他分支中移动played.Add(item.Teamname);,因此如果他们没有播放,则您只需将团队名称添加到played集合中。

foreach (var item in Teamlist)
{
    //Add own Name on Check
    // played.Add(item.Teamname); // <---- Move from here

    if (played.Contains(item.Teamname))
    {
    //Played against them already or is the same name as their own
    }
    else
    {
        //Add own Name on Check
        played.Add(item.Teamname); // <---- To here

        ...

答案 5 :(得分:1)

编辑:方法最初为每个团队播放两次。实际上你需要一个播放列表的概念。见下文。

编辑:这是另一种可能更快的方法。

void PlayRound(Team[] teams)
{
    for (int i = 0; i < teams.Length; i++)
        for (int j = i + 1; j < teams.Length; j++)
            PlayMatch(teams[i], teams[j]);
}

/// <summary>
/// Plays a full round, one match per team.
/// </summary>
void PlayRound(List<Team> teams)
{
    List<Team> played = new List<Team>(); // keep track of teams in the outer loop

    foreach (Team teamA in teams)
    {
        foreach (Team teamB in teams)
        {
            if (teamA == teamB || played.Contains(teamB))
                continue;
            else
                PlayMatch(teamA, teamB);
        }
        played.Add(teamA); // add outer loop team to ensure one match per pairing
    }
}

/// <summary>
/// Plays a match.
/// </summary>
void PlayMatch(Team teamA, Team teamB)
{
    int scoreA = rand.Next();
    int scoreB = rand.Next();

    if (scoreA > scoreB)
    {
        teamA.Win();
        teamB.Lose();
    }
    else if (scoreA == scoreB)
    {
        teamA.Draw();
        teamB.Draw();
    }
    else
    {
        teamB.Win();
        teamA.Lose();
    }
}

答案 6 :(得分:0)

foreach (var item in Teamlist)
{
    //Add own Name on Check
    played.Add(item.Teamname);

    if (played.Contains(item.Teamname))
    {
        //Played against them already or is the same name as their own
    }

这总是如此吗?你添加了名字,然后你会尝试找到它(你刚刚添加它......)

played.Add(item.Teamname);

我应该在foreach范围的末尾

答案 7 :(得分:0)

你的逻辑中缺少一个循环。

根据您提供的代码,会发生以下情况:

  1. 将当前的队名添加到播放列表
  2. 检查当前Teamname是否在播放列表中
  3. 你应该在步骤1和2之间添加另一个循环来循环所有团队并执行游戏逻辑。