Click here to Skip to main content
15,909,193 members
Home / Discussions / C#
   

C#

 
AnswerRe: Licensing Pin
SBolder12-Dec-05 13:28
SBolder12-Dec-05 13:28 
Questionarray of strucs Pin
dust212-Dec-05 11:53
dust212-Dec-05 11:53 
AnswerRe: array of strucs Pin
Christian Graus12-Dec-05 12:03
protectorChristian Graus12-Dec-05 12:03 
GeneralRe: array of strucs Pin
dust212-Dec-05 12:48
dust212-Dec-05 12:48 
GeneralRe: array of strucs Pin
Christian Graus12-Dec-05 12:54
protectorChristian Graus12-Dec-05 12:54 
GeneralRe: array of strucs Pin
dust213-Dec-05 3:14
dust213-Dec-05 3:14 
GeneralRe: array of strucs Pin
Christian Graus13-Dec-05 10:37
protectorChristian Graus13-Dec-05 10:37 
QuestionRandom Generation Pin
Dave Hurt12-Dec-05 10:16
Dave Hurt12-Dec-05 10:16 
I'm trying to build a match generator for a scoring program for some friends of mine... part of the task is to randomly generate match pairing with a few rules... teams shouldn't play back to back rounds, if more then 1 field is running teams shouldn't be scheduled to play one both fields at the same time, etc.... I've worked this out for the most part, but I'm running into a few issues and I wanted to throw this out to see if there are other ways to do this... So here's a quick walk through of how my code works right now, and I'll try to comment things accordingly so things line up:

1. Get all the teams playing in the event
2. Load team id's into an array
3. Go through the team array and load every possible match into the raw matches array
4. Randomly draw a match from raw match array
5. Check both teams to see if they have played in the last 2 rounds
6. If not, write to database as next match
7. Repeat process to until raw matches are done.
8. If it's set that teams will play each other multiple times, repeat entire process.

Part of what I need to do is come up with a way to determine my recently played buffer... right now it's set to the last 2 rounds, so it holds 4 teams. If only 5 teams are in the event, it's pretty much useless, so I've had to add checking to limit how many times it tries to pull a match that isn't putting a team back to back...

Thanks for any input!

and here's my code....

public void buildRounds(int fields, int times, int eventId)
{
	OleDbConnection oConn;
	OleDbCommand oComm;

	OleDbDataAdapter adapter;
	DataSet data = new DataSet();
	string sSql;
	int totalTeams;
	int foo;			
	int currentField = 1;
	int currentRound = 1;
	Random random = new Random();

	oConn = new OleDbConnection(connectionString);

	try
	{
		oConn.Open();

		for(int rounds=1;rounds<=times;rounds++)
		{
			//gets number of teams in event
			sSql = "SELECT count(*) from eventDtl where eventID = " + eventId;

			oComm = new OleDbCommand(sSql, oConn);

			totalTeams = Convert.ToInt32(oComm.ExecuteScalar());

			//selects all teams in event to load into array
			sSql = "SELECT teamID from eventDtl where eventID = " + eventId;

			oComm = new OleDbCommand(sSql, oConn);
			adapter = new OleDbDataAdapter(oComm);
			data = new DataSet();

			adapter.Fill(data);
		
			ArrayList array = new ArrayList(); //holds individual teams
			ArrayList teamBuffer = new ArrayList(); //team buffer for checking back to back games
			ArrayList rawMatches = new ArrayList(); //holds raw matches

			//populates the array with the team list...
			foreach(DataTable myTable in data.Tables)
			{
				foreach(DataRow myRow in myTable.Rows)
				{
					array.Add(myRow["teamID"]);
				}
			}

			foo = array.Count;

			for(int i = 0;i<foo;i++)
			//creates raw matches and loads into array
			{	
				for(int j=i+1;j<foo;j++)
				{
					rawMatches.Add(array[i] + " " + array[j]);
				}
			}

			foo = rawMatches.Count;
			
			while(foo>0)
			{
				if(teamBuffer.Count > 4)
				//buffer to check teams playing back to back.  pops oldest 2 entires if stack is over 4
				{
					teamBuffer.RemoveAt(0);
					teamBuffer.RemoveAt(0);}

					int myIndex = 0;
					int team1;
					int team2;
					int match;
					int counter = 1;
					do
					//generates random match from list and looks through the buffer to see if teams have played recently.  
					//if teams played recently, it pulls another match.
					{
						match = random.Next(0,foo);

						if(currentField>fields)
						{
							currentField=1;
							currentRound++;
						}

						string [] teams = rawMatches[match].ToString().Split(' ');
					
						team1 = Convert.ToInt32(teams[0]);
						team2 = Convert.ToInt32(teams[1]);
					
						if(counter > foo){
							myIndex = 0;
							break;}
						if(foo>4)
						//skips repeat match check for last 4 teams
						{
							foreach(Object bar in teamBuffer)
							//looks through buffer array
							{							
								if(Convert.ToInt32(bar) == team1 || Convert.ToInt32(bar) == team2)
								{
									myIndex=1;
									break;}
								else
									myIndex=0;
							}
						}
						counter++;
					}while(myIndex==1);

					//if finally get a good round, add teams to buffer
					teamBuffer.Add(team1);
					teamBuffer.Add(team2);

					//insert round into dbase
					sSql = "insert into rounds (eventID, roundNum, field, team1ID, team2ID) values ("
						+  eventId + ", " + currentRound + ", " + currentField + ", " + team1 + ", " + team2 + ")";

					oComm= new OleDbCommand(sSql, oConn);
					oComm.ExecuteNonQuery();
					
					//advance current field
					currentField++;				

					//pop match from raw list
					rawMatches.RemoveAt(match);
					//update raw matches left counter
					foo = rawMatches.Count;
				}
			}
			oConn.Close();
		}
		catch(Exception ex)
		{
			oConn.Close();
			throw new Exception("Database error. " + ex.Message);
		}
	}
}

AnswerRe: Random Generation Pin
Ista12-Dec-05 16:13
Ista12-Dec-05 16:13 
QuestionImplementing an Interface Pin
adafaaa12-Dec-05 10:07
adafaaa12-Dec-05 10:07 
AnswerRe: Implementing an Interface Pin
Colin Angus Mackay12-Dec-05 11:09
Colin Angus Mackay12-Dec-05 11:09 
AnswerRe: Implementing an Interface Pin
Charlie Williams12-Dec-05 11:11
Charlie Williams12-Dec-05 11:11 
QuestionInterfaces &amp; Objects Pin
adafaaa12-Dec-05 9:48
adafaaa12-Dec-05 9:48 
AnswerRe: Interfaces &amp;amp; Objects Pin
Colin Angus Mackay12-Dec-05 11:15
Colin Angus Mackay12-Dec-05 11:15 
QuestionImplementing cut, copy, paste, undo, select all Pin
monrobot1312-Dec-05 9:01
monrobot1312-Dec-05 9:01 
AnswerRe: Implementing cut, copy, paste, undo, select all Pin
Polis Pilavas12-Dec-05 11:26
Polis Pilavas12-Dec-05 11:26 
Questionpassing parameters to a DLL function receiving pointer Pin
devinzhang12-Dec-05 8:27
devinzhang12-Dec-05 8:27 
AnswerRe: passing parameters to a DLL function receiving pointer Pin
Colin Angus Mackay12-Dec-05 9:33
Colin Angus Mackay12-Dec-05 9:33 
AnswerRe: passing parameters to a DLL function receiving pointer Pin
Colin Angus Mackay12-Dec-05 9:35
Colin Angus Mackay12-Dec-05 9:35 
GeneralRe: passing parameters to a DLL function receiving pointer Pin
devinzhang12-Dec-05 9:58
devinzhang12-Dec-05 9:58 
AnswerRe: passing parameters to a DLL function receiving pointer Pin
Jared Parsons12-Dec-05 11:40
Jared Parsons12-Dec-05 11:40 
GeneralRe: passing parameters to a DLL function receiving pointer Pin
devinzhang12-Dec-05 12:08
devinzhang12-Dec-05 12:08 
QuestionSigning CodeDom assemblies Pin
cbadal12-Dec-05 8:00
cbadal12-Dec-05 8:00 
AnswerRe: Signing CodeDom assemblies Pin
cbadal14-Dec-05 7:08
cbadal14-Dec-05 7:08 
QuestionDatagrid ghost selection Pin
Glenn E. Lanier II12-Dec-05 6:29
Glenn E. Lanier II12-Dec-05 6:29 

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.