Click here to Skip to main content
15,889,843 members
Home / Discussions / C#
   

C#

 
GeneralRe: Need to Learn Split Method Pin
OriginalGriff22-Nov-09 4:28
mveOriginalGriff22-Nov-09 4:28 
GeneralRe: Need to Learn Split Method Pin
Shameel22-Nov-09 23:24
professionalShameel22-Nov-09 23:24 
QuestionDataGridViewComBoBoxColumn Binding Problem Pin
Abdul Rahman Hamidy21-Nov-09 22:44
Abdul Rahman Hamidy21-Nov-09 22:44 
Questiondatagridview checkbox Pin
Yulianto.21-Nov-09 21:32
Yulianto.21-Nov-09 21:32 
AnswerMessage Closed Pin
21-Nov-09 22:41
stancrm21-Nov-09 22:41 
GeneralRe: datagridview checkbox Pin
Yulianto.21-Nov-09 23:04
Yulianto.21-Nov-09 23:04 
GeneralRe: datagridview checkbox Pin
dojohansen23-Nov-09 1:36
dojohansen23-Nov-09 1:36 
Questionschedule generator not working properly Pin
rooster215421-Nov-09 8:37
rooster215421-Nov-09 8:37 
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]
QuestionRe: schedule generator not working properly Pin
harold aptroot21-Nov-09 8:52
harold aptroot21-Nov-09 8:52 
AnswerRe: schedule generator not working properly Pin
dojohansen23-Nov-09 1:53
dojohansen23-Nov-09 1:53 
QuestionLoad a txt file in a richTextBox? Pin
ahlm21-Nov-09 8:34
ahlm21-Nov-09 8:34 
AnswerRe: Load a txt file in a richTextBox? Pin
Luc Pattyn21-Nov-09 8:37
sitebuilderLuc Pattyn21-Nov-09 8:37 
GeneralRe: Load a txt file in a richTextBox? Pin
ahlm21-Nov-09 8:40
ahlm21-Nov-09 8:40 
GeneralRe: Load a txt file in a richTextBox? Pin
Luc Pattyn21-Nov-09 9:02
sitebuilderLuc Pattyn21-Nov-09 9:02 
GeneralRe: Load a txt file in a richTextBox? Pin
ahlm21-Nov-09 11:53
ahlm21-Nov-09 11:53 
GeneralRe: Load a txt file in a richTextBox? Pin
Luc Pattyn21-Nov-09 12:23
sitebuilderLuc Pattyn21-Nov-09 12:23 
GeneralRe: Load a txt file in a richTextBox? Pin
ahlm22-Nov-09 7:38
ahlm22-Nov-09 7:38 
GeneralRe: Load a txt file in a richTextBox? Pin
dojohansen23-Nov-09 9:32
dojohansen23-Nov-09 9:32 
QuestionHow to print (and preview) any document - but not with CrystalReport Pin
E_Gold21-Nov-09 7:02
E_Gold21-Nov-09 7:02 
AnswerRe: How to print (and preview) any document - but not with CrystalReport Pin
Luc Pattyn21-Nov-09 7:11
sitebuilderLuc Pattyn21-Nov-09 7:11 
GeneralRe: How to print (and preview) any document - but not with CrystalReport Pin
E_Gold21-Nov-09 8:41
E_Gold21-Nov-09 8:41 
GeneralRe: How to print (and preview) any document - but not with CrystalReport Pin
Luc Pattyn21-Nov-09 9:04
sitebuilderLuc Pattyn21-Nov-09 9:04 
AnswerRe: How to print (and preview) any document - but not with CrystalReport Pin
dojohansen23-Nov-09 9:35
dojohansen23-Nov-09 9:35 
Questionquick combobox question Pin
Ronni Marker21-Nov-09 5:36
Ronni Marker21-Nov-09 5:36 
AnswerMessage Closed Pin
21-Nov-09 5:45
stancrm21-Nov-09 5:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.