我目前正在开展一个篮球模拟游戏的项目,我在为将要玩的游戏生成灯具时遇到问题,而且我完全忘记了导致问题的原因。
错误列表中没有出现错误,但由于某些原因应该生成灯具时,我认为发生了无限循环,但我不确定导致问题的原因。
我已经分离了算法中使用的数组以及从下面算法中添加到文本文件的初始文件:http://pastebin.com/cLXi0Hm3因为我觉得它会占用太多空间。
以下是创建这些灯具的算法:
public static void Fixtures()
{
List<string> SouthwestFixtures = new List<string>(); // Create a new list to store the games
while (SouthwestFixtures.Count < 410) // Whilst length of fixtures is less than 410
{
/// <summary>
/// Southwest Division Fixtures
/// </summmary>
Random rnd = new Random(); // Generate a new random
string SWTeam1 = Arrays.WCSouthwest[rnd.Next(Arrays.WCSouthwest.Length)]; // Select random home team
string SWTeam2 = Arrays.WCSouthwest[rnd.Next(Arrays.WCSouthwest.Length)]; // Select random away team
if (SWTeam1 == SWTeam2) // If they are the same teams
{
while (SWTeam1 == SWTeam2) // whilst they are equal to eachother
{
string newteam = Arrays.WCSouthwest[rnd.Next(Arrays.WCSouthwest.Length)];
SWTeam2 = newteam; // generate new away team
}
}
string fixture = string.Format("{0} v {1}", SWTeam1, SWTeam2); // convert it to a string
for (int i = 0; i <= 40; i++) // whilst i is 0
{
if (SouthwestFixtures.Contains(fixture)) // if fixture is already in fixtures stop
{ break; }
else
{
SouthwestFixtures.Add(fixture);
SouthwestFixtures.Add(fixture);
} // add it
}
/// <summary>
/// 4 Games Against 6 Out of Division Teams In Same Conference
/// </summary>
var WCNWList = Arrays.WCNorthwest.ToList();
var WCPList = Arrays.WCPacific.ToList();
var WCCombined = WCNWList.Concat(WCPList);
var WCCombined1 = WCCombined.ToList();
List<string> WCCombined2 = new List<string>(4);
for (int i = WCCombined1.Count(); i > 6; i--)
{
int WCRemoveTeam = rnd.Next(WCCombined1.Count());
WCCombined2.Add(WCCombined1[WCRemoveTeam]);
WCCombined1.RemoveAt(WCRemoveTeam);
}
var WCCombined1Array = WCCombined1.ToArray();
var WCCombined2Array = WCCombined2.ToArray();
string SWTeam = Arrays.WCSouthwest[rnd.Next(Arrays.WCSouthwest.Length)]; // Select random home team
string WCTeam = WCCombined1Array[rnd.Next(WCCombined1Array.Length)]; // Select random away team
string fixturehome = string.Format("{0} v {1}", SWTeam, WCTeam); // convert it to a string
string fixtureaway = string.Format("{0} v {1}", WCTeam, SWTeam);
for (int i = 0; i <= 30; i++) // whilst i is 0
{
if (SouthwestFixtures.Contains(fixturehome) || SouthwestFixtures.Contains(fixtureaway)) // if fixture is already in fixtures break
{ break; }
else
{
SouthwestFixtures.Add(fixturehome);
SouthwestFixtures.Add(fixturehome);
SouthwestFixtures.Add(fixtureaway);
SouthwestFixtures.Add(fixtureaway);
} // add it
}
/// <summary>
/// 3 Games vs Remaining Out of Divison Teams In Same Conference
/// </summary>
string WCTeam2 = WCCombined2Array[rnd.Next(WCCombined2Array.Length)]; //Grab team from list of teams to play 3 games against
string fixturehome2 = string.Format("{0} v {1}", SWTeam, WCTeam2); // convert it to a string
string fixtureaway2 = string.Format("{0} v {1}", WCTeam2, SWTeam);
for (int i = 0; i <= 20; i++)
{
if (SouthwestFixtures.Contains(fixturehome2) || SouthwestFixtures.Contains(fixtureaway2)) // if fixture is already in fixtures break
{ break; }
else
{
SouthwestFixtures.Add(fixturehome2);
SouthwestFixtures.Add(fixtureaway2);
}
int homeoraway = rnd.Next(1, 2);
if (homeoraway == 1)
{
SouthwestFixtures.Add(fixturehome2);
}
else
{
SouthwestFixtures.Add(fixtureaway2);
}
}
/// <summary>
/// Eastern Conference Fixtures
/// </summary>
string ECTeam = Arrays.ECTeams[rnd.Next(Arrays.ECTeams.Length)]; // Select random away team
string fixtureh = string.Format("{0} v {1}", SWTeam, ECTeam); // convert it to a string
string fixturea = string.Format("{0} v {1}", ECTeam, SWTeam);
for (int i = 0; i <= 75; i++) // whilst i is 0
{
if (SouthwestFixtures.Contains(fixtureh) || SouthwestFixtures.Contains(fixturea)) // if fixture is already in fixtures break
{ break; }
else
{
SouthwestFixtures.Add(fixtureh);
SouthwestFixtures.Add(fixturea);
} // add it
}
} // loop through till it is done
}
我很确定当它产生最后两个夹具部分时出现问题(对阵4支球队的3场比赛以及东部会议比赛之前的所有事情,但我仍然无法弄清楚它出错的地方)
如果有人帮忙指出哪里出错了,我将不胜感激。