Skip to content

Instantly share code, notes, and snippets.

@D1360-64RC14
Last active January 18, 2022 20:38
Show Gist options
  • Save D1360-64RC14/49b752cacb01b0d97d78792a658620c7 to your computer and use it in GitHub Desktop.
Save D1360-64RC14/49b752cacb01b0d97d78792a658620c7 to your computer and use it in GitHub Desktop.
Script para calcular a quantidade de times possíveis em uma partida do Overwatch. As configurações podem ser alteradas em `teams`.
console.time('timer');
const teams = {
tank: {
size: 8,
slots: 2
},
damage: {
size: 17,
slots: 2
},
support: {
size: 7,
slots: 2
}
};
function choose(teams_object) {
// Fatorial like a functional programming
const fac = n => n === 0 ? 1 : n * fac(Math.abs(n) - 1);
const result = Object.entries(teams_object).map(([classe, team]) => {
if (team.size < team.slots) throw Error(`Impossível preencher ${team.slots} posições com ${team.size} ${classe}`);
const output = {
classe,
possibilities: fac(team.size) / (fac(team.size - team.slots) * fac(team.slots)),
slots: []
};
let remaining_slots = team.slots;
while (remaining_slots) {
const will_select = Math.floor(Math.random() * team.size + 1);
if (!output.slots.includes(will_select)) {
output.slots.push(will_select);
remaining_slots--;
};
};
return output;
})
console.log('Seu time:\n' + result.map((v) => ` ${v.classe}: [ ${v.slots.join(', ')} ]`).join('\n'));
const team_qnt = result.map(team => team.possibilities).reduce((n, p) => n * p, 1);
console.log('\nEste é 1 entre', team_qnt, 'times');
console.log(`Você teve ${1 / team_qnt * 100}% de chance de escolhe-lo\n`);
}
choose(teams);
console.timeEnd('timer');
// --- Output ---
// Seu time:
// tank: [ 2, 6 ]
// damage: [ 14, 5 ]
// support: [ 7, 6 ]
// Este é 1 entre 79968 times
// Você teve 0.001250500200080032% de chance de escolhe-lo
// timer: 7.199ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment