Solved

Round Robin Programming

Posted on 2012-03-21
6
1,484 Views
Last Modified: 2012-06-27
Hi,

I know I can't be the only game scheduler in the world with this problem...

I need to write a tool that schedules games for x number of weeks with x number of teams that ensures every  team plays each other (if possible).

The basics of the functionality is this:

* There can be an even or odd number of teams, if odd there will of course be a "bye" week where one team is not playing (buy could be considered a team when doing the algorithm).

* The number of weeks can be either even or odd, basically the algorithm should just run until the number of weeks run out even if all teams don't get to play each other.

* The rotation should be so that every team plays every team but never itself (if number of weeks permits)

Basically for 4 teams the result would look like this:

WEEK1:

A vs B
C vs D

WEEK2:

A vs C
B vs D

WEEK3:

A vs D
B vs C

I would like the code to be able to handle the number of games as input, but honestly if the rotation is solved, I can limit the number of games myself.

I do appreciate any help or direction I can get.

Thanks
~j
0
Comment
Question by:prosit
6 Comments
 
LVL 39

Expert Comment

by:appari
ID: 37751136
try with this sql, in case of odd number of teams use ? for the last team and make teams even. in the schedule game with team ? means bye.
DECLARE @Teams TABLE (teamid varchar(2))
DECLARE @rounds TABLE (roundNum int,teamid1 varchar(2),teamid2 varchar(2))

insert into @Teams 
Select 'A'
union all Select 'B'
union all Select 'C'
union all Select 'D'
union all Select 'E'
union all Select '?'

declare @teamCnt int, @round int

select @teamCnt = count(*), @round=0 from @Teams;
while @round<@teamcnt-1
begin
;with team1 as (Select row_number() over(order by teamid)-1 rowNum, teamid from @teams),
team2 as (Select 
case when (rowNum + @round)>=@teamcnt then (rowNum + @round) % @teamcnt+1 else
 (rowNum + @round) % @teamcnt end rowNum, 
teamid from 
(Select row_number() over(order by teamid) rowNum, teamid from team1 where rownum<>0 ) A)
insert into @rounds
select distinct @round roundnum,team1.teamid, team2.teamid from 
(Select * from team1 where rownum=0 union all
Select * from team2 where rownum< @teamCnt/2 ) as team1 
join (Select * from team2 where rownum>= @teamCnt/2 ) team2 
on team1.rownum+team2.rownum = @teamCnt-1

select @round=@round+1 
end
select * from @rounds

Open in new window

0
 
LVL 18

Expert Comment

by:DarrenD
ID: 37751868
Hi,

You are definitely not the only person with this issue.

Check out: http://en.wikipedia.org/wiki/Round-robin_tournament

and read the "Scheduling algorithm" section.

That should give you an idea of how to implement this in code.

Hope this helps,

Darren
0
 
LVL 2

Author Comment

by:prosit
ID: 37751902
Although this works, it's not really a VB solution...

I'd still prefer if that was a possibility...

~j
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 18

Accepted Solution

by:
DarrenD earned 500 total points
ID: 37751939
Hi,

Here's another link that might help.

http://www.vb-helper.com/howto_net_round_robin.html

Cheers,

Darren
0
 
LVL 45

Expert Comment

by:aikimark
ID: 37752866
The way I would approach this would be to iterate all the unique two player combinations.  The simplest approach was to create a Players table and the join the table with itself in a query like this:
SELECT Players.Player, Players_1.Player
FROM Players, Players AS Players_1
WHERE (((Players.Player)<[Players_1].[player]));

Open in new window


When the Players table contains (ABCD), the result is:
Players.Player
	Players_1.Player
A	B
A	C
B	C
A	D
B	D
C	D

Open in new window


When the Players table contains (ABCDE?), the result is:
Players.Player
	Players_1.Player
?	A
A	B
?	B
A	C
B	C
?	C
A	D
B	D
C	D
?	D
A	E
B	E
C	E
D	E
?	E

Open in new window


The code to process this builds the weeks from the query results.  If a player has appeared on the left or right side of a match in a week, they can not be assigned to another match in that same week.
In the following example, the first PvP pair is simply added to the first week.  Since the second pair contains player A, we have to find a week that doesn't already contain player A -- week 2. The third pair contains player B, so we can't add it to the first week and contains player C, so we can't add it to the second week -- week 3.  The fourth pair contains player A, so we can't add it to either of the first two weeks -- week 3.  The fifth pair contains player B, so we can't add it to week 1 -- week 2.  The sixth pair goes into week 1.
Week1: A      B; C       D
Week2: A      C; B       D
Week3: B      C; A       D
0
 
LVL 2

Author Closing Comment

by:prosit
ID: 37763785
Exactly what I asked for... thanks

~j
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Object to array 7 27
Incorrect group by on LINQ query 5 27
Vb.Net Menu 1 22
Red error squiggly on vb.net 7 0
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now