问题描述
我无法使计划生成器正常工作.按照我的程序结构方式,有32个团队,所有团队都是联盟"的一部分.对于时间表生成器,每个团队与另一个团队配对进行一场游戏16次(16周).具体的问题是每支球队的日程安排最多不超过5场比赛. (每队应该有16场比赛.)实际上,它从1场比赛到5场比赛不寻常.每个团队的时间表.
我不知道出什么问题了.也许有人可以为我指出我的问题.
这是在我的团队课程中:
[code]
私人列表< Game> games = new List< Game>();
公众ReadOnlyCollection< Game>游戏{获得{返回游戏.AsReadOnly(); }}}
public void AssignGame(Game game)
{{br/>} game.Add(game);
}
public IEnumerable< Game> GamesWon {获得{返回游戏.Where(f => f.Played& f.Winner == this); }}
公用IEnumerable< Game> GamesLost {get {return games.Where(f => f.Played& f.Winner!= this); }}
[/code]
这是我的联赛课程:
[code]
private const int HOME = 0;
private const int AWAY = 1;
private List< Team> teamList;
公用ReadOnlyCollection< Team> Tms {get {return teamList.AsReadOnly(); }}}
public int GetRanking(Team team)
{
TeamRankingComparer trc = new TeamRankingComparer();
列表< Team> copyOfTeams = teamList.ToList();
copyOfTeams.Sort(trc);
返回copyOfTeams.IndexOf(team)+ 1;
列表<游戏> games = new List< Game>();
公众ReadOnlyCollection< Game>游戏{获得{返回游戏.AsReadOnly(); }
public void Schedule()
{
games.Clear();
随机r = RandomFactory.Create();
随机r =新Random();
整数周= 16; //Teams.Count()* Teams.Count(); ;
(int i = 0; i< =周; i ++)
对手=新的List< Team>(); //为每个团队创建对手列表
对手.AddRange(Teams.Where(t => t!= team)); //"
团队对手=对手[r.Next(0,对手.计数)]; //从列表中选择对手
if((games.Any(g => g.Home == team&& g.Away ==对手)&&!games.Any(g => ; g.Home == team || g.Away == team)) (!games.Any(g => g.Home == team || g.Away == team))
{{ />}
}
}
}
[/code]
其他课程:
[code]
静态类RandomFactory
{
私有静态Random globalRandom = new Random();
public static Random Create()
{{锁(globalRandom)
{{br/>)Random newRandom = new Random(globalRandom.Next());
/p>
公共类游戏
{
公共游戏(团队主场,离队)
{
回家=主场;
离开=离场;
.AssignGame(this);
Away.AssignGame(this);
}
public Team Home {get;私人套装; }
公众团队离开{get;私人套装; }
公开团队获胜者{get;私人套装; }
公众布尔玩过{get {return Winner!= null; }}
public void OnPlayed(团队优胜者)
优胜者=优胜者;
}
}
内部类TeamRankingComparer:IComparer< Team< {{br/>)public int Compare(Team team1,Team team2)
{{br/> int team1Wins = Team1.GameW.
int team2Wins = team2.GamesWon.Count();
如果if(team1Wins> team2Wins)
team2Wins> team1Wins)
{
传回-1; />返回String.Compare(team1.Name,team2.Name,true);
}
public static class PrintScheduleStatistics
{
public static void PrintTeamStatistics(Team team)
{
foreach(team.Games中的游戏g)
{
字符串会场=``家'';
团队对手= g.Away;
如果(g.Home!=场地)离开'';
对手= g.Home;
}
控制台 WriteLine(String.Format({0}到{1}''),''.
}
}
}
[/code]
此调用在我的Main()方法中:
[code]
League League = new League();
LoadRosters(联赛);
League.Schedule() ;
将以下代码放置在该行之后
Team对手=对手[r.Next(0,手.Counts)];
在联赛类的Schedule方法中,我能够进行测试来查看到底有多少个团队被添加到一个团队的日程安排中.
[code]
如果(team == Teams [1])
{{ //test
Console.WriteLine(opponent.Name); //test
[/code]
测试证明该团队(第1团队)的日程安排将16个团队添加到他们的日程中.在该代码之后,我将相同的代码放置在每个条件添加游戏"中.嵌套如下:
[代码]
团队对手=对手[r.Next(0,对手.计数)]; //从列表中选择对手
/* if(team == Teams [1])
//test
Console.WriteLine(opponent.Name); //test
} */
if if(!games.Any(g => g.Home == team& g.Away ==对手)&&!games.Any(g => g.Home == team || g.Away == team))
{{br/> {
Console.WriteLine("+ team.Name); //test
控制台(Console.WriteLine(opponent.Name); //测试
Console.WriteLine(QUOT;家庭及QUOT);
}
}
否则如果(games.Any(G =>!g.Home == team || g.Away == team))
{{br/>)games.Add(new Game(opponent,team));
<{<<<<{{<{{{{<{{<{{{{{{{{{<<{{{控制台(Console.WriteLine(opponent.Name); //test
控制台(Console.WriteLine(``away'');)
[/code]
结果只在控制台上产生了一个游戏(第一个IF通过,我得到了"home"消息).
所以这告诉我问题出在这里(据我了解) .我只是无法确定问题所在.
I'm having trouble getting a schedule generator working properly. The way I have my program structured, there are 32 teams, all of which are a part of the "league". For the schedule generator, each team gets paired with another team for a game for 16 times (16 weeks). The specific problem is each team's schedule doesn't consist of more than 5 games maximum. (Each team should have 16 games.) In fact, it unusually varies from 1 game to 5 games. for each team's schedule.
I can't figure out what is wrong. Perhaps someone can point out my problem for me.
This is in my Team class:
[code]
private List<Game> games = new List<Game>();
public ReadOnlyCollection<Game> Games { get { return games.AsReadOnly(); } }
public void AssignGame(Game game)
{
games.Add(game);
}
public IEnumerable<Game> GamesWon { get { return games.Where(f => f.Played && f.Winner == this); } }
public IEnumerable<Game> GamesLost { get { return games.Where(f => f.Played && f.Winner != this); } }
[/code]
This is in my league class:
[code]
private const int HOME = 0;
private const int AWAY = 1;
private List<Team> teamList;
public ReadOnlyCollection<Team> Tms { get { return teamList.AsReadOnly(); } }
public int GetRanking(Team team)
{
TeamRankingComparer trc = new TeamRankingComparer();
List<Team> copyOfTeams = teamList.ToList();
copyOfTeams.Sort(trc);
return copyOfTeams.IndexOf(team) + 1;
}
//test
private List<Game> games = new List<Game>();
public ReadOnlyCollection<Game> Games { get { return games.AsReadOnly(); } }
public void Schedule()
{
games.Clear();
Random r = RandomFactory.Create();
//Random r = new Random();
int weeks = 16; // Teams.Count() * Teams.Count(); ;
for (int i = 0; i <= weeks; i++)
{
foreach (Team team in Teams)
{
List<Team> opponents = new List<Team>(); //create list of opponents for each team
opponents.AddRange(Teams.Where(t => t != team)); //""
Team opponent = opponents[r.Next(0, opponents.Count)]; //choose opponent from list
if (!games.Any(g => g.Home == team && g.Away == opponent) && !games.Any(g => g.Home == team || g.Away == team))
{
games.Add(new Game(team, opponent));
}
else if (!games.Any(g => g.Home == team || g.Away == team))
{
games.Add(new Game(opponent, team));
}
}
}
}
[/code]
Other classes:
[code]
static class RandomFactory
{
private static Random globalRandom = new Random();
public static Random Create()
{
lock (globalRandom)
{
Random newRandom = new Random(globalRandom.Next());
return newRandom;
}
}
}
public class Game
{
public Game(Team home, Team away)
{
Home = home;
Away = away;
Home.AssignGame(this);
Away.AssignGame(this);
}
public Team Home { get; private set; }
public Team Away { get; private set; }
public Team Winner { get; private set; }
public bool Played { get { return Winner != null; } }
public void OnPlayed(Team winner)
{
Winner = winner;
}
}
internal class TeamRankingComparer : IComparer<Team>
{
public int Compare(Team team1, Team team2)
{
int team1Wins = team1.GamesWon.Count();
int team2Wins = team2.GamesWon.Count();
if (team1Wins > team2Wins)
{
return 1;
}
else if (team2Wins > team1Wins)
{
return -1;
}
else if (team1Wins == 0 && team2Wins == 0)
{
return String.Compare(team1.Name, team2.Name, true);
}
else
{
return 0;
}
}
}
public static class PrintScheduleStatistics
{
public static void PrintTeamStatistics(Team team)
{
foreach (Game g in team.Games)
{
string venue = "Home";
Team opponent = g.Away;
if (g.Home != team)
{
venue = "Away";
opponent = g.Home;
}
Console.WriteLine(String.Format("{0} to {1}", venue, opponent.Name));
}
}
}
[/code]
This call is in my Main() method:
[code]
League league = new League();
LoadRosters(league);
league.Schedule();
PrintScheduleStatistics.PrintTeamStatistics(league.Teams[1]);
[/code]
With the following code placed after the line
Team opponent = opponents[r.Next(0, opponents.Count)];
in the Schedule method of the League class, I was able to test to see exactly how many teams are being added to one team's schedule.
[code]
if (team == Teams[1])
{
Console.WriteLine(team.Name); //test
Console.WriteLine(opponent.Name); //test
}
[/code]
The test proved that the schedule for this team, team 1, was getting 16 teams added to their schedule. After that code, I placed the same code in each conditional "add game" nest as follows:
[code]
Team opponent = opponents[r.Next(0, opponents.Count)]; //choose opponent from list
/*if (team == Teams[1])
{
Console.WriteLine(" " + team.Name); //test
Console.WriteLine(opponent.Name); //test
}*/
if (!games.Any(g => g.Home == team && g.Away == opponent) && !games.Any(g => g.Home == team || g.Away == team))
{
games.Add(new Game(team, opponent));
if (team == Teams[1])
{
Console.WriteLine(" " + team.Name); //test
Console.WriteLine(opponent.Name); //test
Console.WriteLine("home");
}
}
else if (!games.Any(g => g.Home == team || g.Away == team))
{
games.Add(new Game(opponent, team));
if (team == Teams[1])
{
Console.WriteLine(" " + team.Name); //test
Console.WriteLine(opponent.Name); //test
Console.WriteLine("away");
}
}
[/code]
The results yielded only one game in the console (the first IF went through and I got the "home" message).
So that tells me that the problem lies here (from what I understand of it). I just can't pinpoint the problem.
这篇关于代码无法正常工作/发布时间过长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!