Last active
January 17, 2018 00:44
-
-
Save knoajp/c9c448a3f9f665e553d84ca480706347 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
const COUNT = 1000000; | |
const FINAL = 1; | |
const SORT = false; | |
let tournament = [ | |
[ | |
[ | |
[ | |
{name: '村山慈明', rate: 1737}, | |
{name: '三枚堂達也', rate: 1760}, | |
], | |
[ | |
{name: '丸山忠久', rate: 1731}, | |
{name: '久保利明', rate: 1814}, | |
], | |
], | |
[ | |
[ | |
{name: '渡辺明', rate: 1793}, | |
{name: '三浦弘行', rate: 1765}, | |
], | |
[ | |
{name: '菅井竜也', rate: 1830}, | |
{name: '広瀬章人', rate: 1774}, | |
], | |
], | |
], | |
[ | |
[ | |
[ | |
{name: '澤田真吾', rate: 1748}, | |
{name: '藤井聡太', rate: 1729}, | |
], | |
[ | |
{name: '永瀬拓矢', rate: 1854}, | |
{name: '佐藤天彦', rate: 1810}, | |
], | |
], | |
[ | |
[ | |
{name: '羽生善治', rate: 1824}, | |
{name: '高見泰地', rate: 1714, defeated: true}, | |
], | |
[ | |
{name: '糸谷哲朗', rate: 1810, defeated: true}, | |
{name: '八代弥', rate: 1665, defeated: true}, | |
], | |
], | |
], | |
]; | |
let winners = {}, ranking = [], output = '', t = JSON.stringify(tournament); | |
let fight = function(a, b){ | |
let defeat = (w, l) => {l.score = 0; winners[l.name] = l; return w;}; | |
if(a.defeated) return defeat(b, a); | |
if(b.defeated) return defeat(a, b); | |
let ea = 1 / (1 + Math.pow(10, (b.rate - a.rate) / 400)); | |
return (Math.random() < ea) ? a : b; | |
}; | |
let round = function(match){ | |
if(match === null) return; | |
switch(match.length){ | |
case(1): | |
if(match[0].length === 2) return round(match[0]); | |
return match[0]; | |
case(2): | |
let a = match[0], b = match[1]; | |
if(a.length) a = round(a); | |
if(b.length) b = round(b); | |
return fight(a, b); | |
default: | |
console.log('Tournament error.'); | |
return null; | |
} | |
}; | |
for(let i = 0; i < COUNT; i++){ | |
let winner; | |
for(let a = round(tournament[0]), b = round(tournament[1]), wins = {}, i = 0; i < FINAL; i++){ | |
winner = fight(a, b); | |
wins[winner.name] = wins[winner.name] + 1 || 1; | |
if(wins[winner.name] > FINAL/2) break; | |
} | |
if(!winners[winner.name]) winners[winner.name] = winner; | |
winners[winner.name].score = winner.score + 1 || 1; | |
} | |
Object.keys(winners).forEach((name) => ranking.push(winners[name])); | |
if(SORT) ranking.sort((a, b) => b.score - a.score); | |
else ranking.sort((a, b) => t.indexOf(a.name) - t.indexOf(b.name)); | |
let digits = String(COUNT).length - 1, format = (score) => (' '.repeat(digits) + score).slice(- digits).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); | |
ranking.forEach((player) => output += `${format(player.score)}: (${player.rate}) ${player.name}\n`); | |
console.log(output); | |
return output; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment