jaguilar87
9/29/2018 - 10:27 PM

Pairing Generator

Pairing Generator

// Parameters
const players = [
    'FUR',
    'JLO',
    'PMA',
    'PUR',
    'NAP',
    'BRA',
    'TBE',
    'JBD',
    'MDL',
    'JMH',
];
const nOfGames = 7;

// Script

let result = [];

function shuffle(a) {
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
    return a;
}

for (let i = 0; i < nOfGames; i++) {
    let isUnique = false;
    
    while (!isUnique) {
        const buffer = [];
        let series = shuffle(players);

        for (let j = 0; j < 5; j++) {
            const a = series[j];
            const b = series[j+5];

            const pair = a < b ? `${a}\t${b}` : `${b}\t${a}`;

            if (result.includes(pair)) {
                isUnique = false;
                break;
            } else {
                buffer.push(pair);
            }

            isUnique = true;
        }

        if (isUnique) {
            console.log(buffer.join('\n'));
            result = result.concat(buffer);
        }
    }

    console.log('\n\n');
}

function onlyUnique(value, index, self) { 
    return self.indexOf(value) === index;
}

console.log(JSON.stringify(result));
console.log('is unique:', result.filter(onlyUnique).length === result.length);