Working round robin tournament chess pairing code for Matlab and Java I wrote, free to use/distribute. Please credit.
Maybe useful to somebody?
RR code in Matlab:
function rr2(n,round)
if nargin<1
round = 1;
n = input(‘Enter number of players: ‘);
round = input(‘Enter round number: ‘);
end
players = 1:n;
if mod(n,2)==0 %even
fprintf(‘Round %i of %i\n’,round,n-1);
else
fprintf(‘Round %i of %i\n’,round,n);
players = [0 players]; %zero is imaginary player
n = n + 1;
end
%cs = mod(round-1,n-1);
if round ==1
cs = mod(round-1,n-1);
else
cs = mod((round-1)*((n/2)-1),n-1);
end
players = [players(1) players((end-cs+1):end) players(2:(end-cs))]
if mod(round,2)==1
pairone = players(1:2);
else
pairone = [players(2) players(1)];
end
k = (length(players)-2)/2;
w = players(3:(3+k-1));
b = players(end:-1:(3+k));
if pairone(1)==0
fprintf(‘%i gets a bye\n’,pairone(2));
end
if pairone(2)==0
fprintf(‘%i gets a bye\n’,pairone(1));
end
if (pairone(1)~=0 && pairone(2)~=0)
fprintf(‘(W %i %i B) ‘,pairone(1),pairone(2));
end
for i=1:length(w)
if w(i)==0
fprintf(‘%i gets a bye\n’,b(i));
end
if b(i)==0
fprintf(‘%i gets a bye\n’,w(i));
end
if (w(i)~=0 && b(i)~=0)
fprintf(‘(W %i %i B) ‘,w(i),b(i))
end
end
fprintf(‘\n’);
end
RR code in Java:
import java.lang.Math;
public class rr {
public static void main(String[] args){
//13/01/2009 12:14pm
// This is a self contained offline version.
// Create new package rr and compile.
int tournround = 0;
tournround++;
int n = 6; //tourn.players.size
tournround = 1; //tourn.round
int[] playerIndex;
if ( n % 2 == 0 ){
playerIndex = new int[n+1];
System.out.println(“Round ” + tournround + ” of ” + (int)(n-1));
for (int i=1;i<=n;i++) playerIndex[i]=i;
}
else{
System.out.println(“Round ” + tournround + ” of ” + n);
playerIndex = new int[n+2];
n++;
for (int i=1;i<=n;i++) playerIndex[i]=i-1;
}
int cs; //cycle shift
if (tournround==1) cs = (tournround-1) % (n-1);
else{cs = ((tournround-1)*(n/2-1)) % (n-1); }
int firstlength = Math.max(0,n-cs-1);
int secondlength = Math.max(0,cs);
int[] first = new int[firstlength];
int[] second = new int[secondlength];
System.arraycopy(playerIndex,2,first,0,firstlength);
System.arraycopy(playerIndex,n-cs+1,second,0,secondlength);
System.arraycopy(first,0,playerIndex,secondlength+2,firstlength);
System.arraycopy(second,0,playerIndex,2,secondlength);
int[] pairone = new int[2];
if ((tournround % 2) ==1){
pairone[0] = playerIndex[1];
pairone[1] = playerIndex[2];
}
else{
pairone[0] = playerIndex[2];
pairone[1] = playerIndex[1];
}
int k = (n-2)/2; // 1,2,–k–|–k– <- length n
int[] w = new int[k];
int[] b = new int[k];
System.arraycopy(playerIndex,3,w,0,k);
System.arraycopy(playerIndex,k+3,b,0,k);
int[] br = new int[k];
for (int i=0;i<k;i++){
br[i] = b[k-1-i];
}
for (int i=0;i<k;i++){
b[i] = br[i];
}
if (pairone[0]==0){
System.out.println(pairone[1] + ” gets a bye. “);
}
if (pairone[1]==0){
System.out.println(pairone[0] + ” gets a bye. “);
}
if (pairone[0]!=0 && pairone[1]!=0){
System.out.println(“(W “+pairone[0]+” “+pairone[1]+” B)”);
}
for (int i=0;i<k;i++){
if (w[i]==0){
System.out.println(b[i] + ” gets a bye. “);
}
if (b[i]==0){
System.out.println(w[i] + ” gets a bye. “);
}
if (w[i]!=0 && b[i]!=0){
System.out.println(“(W “+w[i]+” “+b[i]+” B)”);
}
}
System.exit(0);
}
}
Another page with RR C code:
http://www.devenezia.com/javascript/article.php/RoundRobin1.html