2

编辑 - 检查移至 Else,循环和多个对象更改现在是主要问题

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

检查必须在 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());
}
4

8 回答 8

2

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

首先你添加item.Teamname

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

然后检查之前添加item.Teammname的是否在列表中——始终为真:

if (played.Contains(item.Teamname))
{
于 2013-08-07T14:06:58.927 回答
2

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

拿球队列表的笛卡尔积来给你要玩的比赛列表怎么样。使用 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 "...";
}
于 2013-08-07T14:21:08.303 回答
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
于 2013-08-07T14:23:22.270 回答
1

这是你的问题:

played.Add(item.Teamname);

if (played.Contains(item.Teamname))

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

于 2013-08-07T14:07:17.147 回答
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();
    }
}
于 2013-08-07T14:28:02.897 回答
1

要修复您的代码,您需要played.Add(item.Teamname);进入您的 else 分支,因此您只需将团队名称添加到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

        ...
于 2013-08-07T14:26:31.327 回答
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 范围的末尾

于 2013-08-07T14:07:24.973 回答
0

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

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

  1. 将当前团队名称添加到播放列表
  2. 检查当前 Teamname 是否在播放列表中

您应该在步骤 1 和 2 之间添加另一个循环以循环所有团队并执行游戏逻辑。

于 2013-08-07T14:10:32.573 回答