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.

* 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:


A vs B
C vs D


A vs C
B vs D


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.