Skip to content

Instantly share code, notes, and snippets.

@let-aurn
Last active May 16, 2021 02:31
Show Gist options
  • Select an option

  • Save let-aurn/af413c7480d51e0534924863150836ec to your computer and use it in GitHub Desktop.

Select an option

Save let-aurn/af413c7480d51e0534924863150836ec to your computer and use it in GitHub Desktop.
Liga 2020/2021 simulation (script)
const TEAMS = [ 'Atlético Madrid', 'Real Madrid', 'FC Barcelona' ];
const TABLE = [ 80, 78, 76 ];
const REMAINING_MATCH_DAYS = 2;
const GAME_RESULTS = [ 'win', 'draw', 'loose' ];
const CHANCE_OF_BEING_CHAMPION = [ 0, 0, 0 ];
let MATCH_DAY = 1;
function runSimulation(teams, t, iteration) {
const table = [...t];
if (teams.length === 0) {
if (MATCH_DAY < REMAINING_MATCH_DAYS) {
MATCH_DAY++;
runSimulation([...TEAMS], [...table], iteration);
MATCH_DAY--;
} else {
let winnerIndex = table.indexOf(Math.max(...table));
if (winnerIndex !== 1 && table[winnerIndex] === table[1]) {
winnerIndex = 1;
}
const tab = ' '.repeat(iteration - 1);
const team = TEAMS[winnerIndex];
console.log(`${tab} 🏆 ${team} Champion`, table, winnerIndex === 1 ? ' HALA MADRID 35th' : '');
CHANCE_OF_BEING_CHAMPION[winnerIndex] += 1;
}
} else {
const team = teams[0];
const index = TEAMS.indexOf(team);
for (const result of GAME_RESULTS) {
const tab = ' '.repeat(iteration);
console.log(`${tab} > Match day ${MATCH_DAY}: ${team} ${result}s`);
const temp = table[index];
if (result === 'win') table[index] += 3;
if (result === 'draw') table[index] += 1;
const nextTeams = [...teams];
nextTeams.shift();
runSimulation(nextTeams, [...table], iteration + 1);
table[index] = temp;
}
}
}
runSimulation([...TEAMS], [...TABLE], 0);
const champions = {};
TEAMS.forEach((team, index) => champions[team] = CHANCE_OF_BEING_CHAMPION[index]);
console.log('\n\n\nNumber of possibilities of being champion\n');
console.table(champions);
console.log('\n\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment