PDA

View Full Version : Resolved searching the right algorithm ...


jellev
10-09-2009, 03:34 PM
Hi there,

I have:

private BindingList<Player> spelers_; //'spelers' mean's 'players' in dutch
private BindingList<Lane> lanes_;
Every Lane contains some players.
When I want to insert a player from spelers_ into a Lane, I have to check if the
player's group isn't already represented in the lane. (every group has 4 players, which may not play in 1 Lane)

I have the following:

foreach (Player p in spelers)
{
foreach (Lane l in lanes)
{
foreach (Player lp in l.players)
{
//something like this
if (lp.group != p.group)
l.players.Add(p);
}
}
But now I didn't create a new lane yet.
I think that the creation of a lane should also be done in this loop...

Is it clear?
And is it possible?

Massive thanx

jellev
10-10-2009, 10:55 AM
Ok,
an update.

This is how I tried to fix it:

public int devide()
{
BindingList<Player> spelersTmp = spelers;
if (lanes.Count == 0)
for (int i = 0; i < 4; i++)
{
lanes.Add(new Lane());
}
if (lanes.Count > 0)
{
foreach (Lane l in lanes)
{
Console.WriteLine("1");
if (l.players.Count == 0)
{
Console.WriteLine("inserttest");
l.players.Add(spelersTmp[0]);
spelersTmp.RemoveAt(0);
}
foreach (Player lp in l.players)
{
Console.WriteLine("2");
foreach (Player p in spelersTmp)
{
if (lp.group != p.group)
{
l.players.Add(p);
spelersTmp.Remove(p);
}
Console.WriteLine("test player: {0}", lp.name);
}
}
}
}
return GameInput.GI.lanes.Count;
}

This works a little, but the remove from the tempList "spelersTmp" isn't correct.
I got an error like this:

The collection is changed. You may not run the inventory operation.//translated from dutch

hope this looks a little more obvious ...

jellev
10-10-2009, 04:12 PM
So,

Seems that some people already came to see my very hard post...

I'm already a little further but it doesn't seem right...

re-explanation:


I have a list with teams (variable and create with a button). -> class Team
every team has 4 teammembers.
I have a list with players (variable, named spelers in the program) -> class Player
I have a list with lanes (variable, autoincreased*) -> class Lane

Now,
when the players are getting divided over the Lanes there may not be two players of the same team in one lane.

when the lane has 8 players there should be *created an new lane for the other players.

I'm using BindingLists to make it possible to view the lists in a datagridview.

The previous post I tried with several foreach loops, but that gave me an error when adding and removing.
so here is the next step with for-loops :o. It looks a little complex.
public int divide()
{
BindingList<Player> spelersTmp = spelers;
if (lanes.Count == 0)
{
lanes.Add(new Lane());
Console.WriteLine(lanes[lanes.Count - 1].name);
}
if (lanes.Count > 0)
{
//int i = 0;
//while( i < 8)
for (int i = 0; i < lanes.Count; i++)
{
if (lanes[i].players.Count > 7)
{
lanes.Add(new Lane());
Console.WriteLine(lanes[lanes.Count - 1].name);
}
else
{
Console.WriteLine("1");
if (lanes[i].players.Count == 0 && spelersTmp.Count > 0)
{
Console.WriteLine("inserttest");
lanes[i].players.Add(spelersTmp[0]);
spelersTmp.RemoveAt(0);
}

for (int j = 0; j < lanes[i].players.Count; j++)
{
Console.WriteLine("2");
for (int k = 0; k < spelersTmp.Count; k++)
{
//if (lanes[i].players[j].group == spelersTmp[k].group)
if (lanes[i].teaminlane.Contains(spelersTmp[k].team))
{
lanes[i].teaminlane.Add(spelersTmp[k].team);
Console.WriteLine("exists in");
}
else
{
Console.WriteLine("not exists in");
lanes[i].players.Add(spelersTmp[k]);
spelersTmp.RemoveAt(k);
}
}
}
}
}
}
foreach (Lane l in lanes)
foreach (Player p in l.players)
Console.WriteLine("Lane:{0} -> player: {1}", l.name, p.name);
return GameInput.GI.lanes.Count;
}
So if anyone would help and want the project... just ask
Or if someone has a simple and better idea for this...

I'll really appreciate your help!

jellev
10-11-2009, 11:20 AM
for them who don't understand...
I have the solution.

I just need to sort mij BindingList<player> spelersTmp so that the players with number 1 are standing before the players with nr 2.

In this way there are no following players with the same Letter.
(Player p = new Player(a3, a, 3);)

Problem is that the BindingList isn't sortable.
my plroblem lays now somewhere else :)