Skip to content

Instantly share code, notes, and snippets.

@tkihira

tkihira/daida.js

Last active Nov 3, 2015
Embed
What would you like to do?
代打って効果あるの?
var originalRatios = [
.259, .337, .330, .267, .188, .274, .224, .227, .091
];
function calcHit(h, bases) {
if(h > 2) {
// homerun
var s = 1 + (bases[0]?1:0) + (bases[1]?1:0) + (bases[2]?1:0);
bases[0] = bases[1] = bases[2] = false;
return s;
}
if(h == 2) {
// two bases
var s = 0;
if(bases[2]) { bases[2] = false; s++; }
if(bases[1]) { bases[1] = false; s++; }
if(bases[0]) { bases[0] = false; bases[2] = true; }
bases[1] = true;
return s;
}
if(h == 1) {
// hit
var s = 0;
if(bases[2]) { bases[2] = false; s++; }
if(bases[1]) { bases[1] = false; s++; }
if(bases[0]) { bases[0] = false; bases[1] = true; }
bases[0] = true;
return s;
}
return 0;
}
var daida = false;
var daidaInning = 7;
var daidaRatio = 0.3;
var daidaRatioAfter = 0.1;
var k = 1000000;
var sum = 0;
for(var i = 0; i < k; i++) {
var ratios = [].concat(originalRatios);
var currentDaida = daida;
var inning = 1;
var batter = 0;
var score = 0;
for(inning = 1; inning < 10; inning++) {
//console.log("inning: " + inning);
var bases = [false, false, false];
var outs = 0;
while(outs < 3) {
var isDaida = false;
if(currentDaida && inning >= daidaInning) {
if(bases[2] || bases[1]) {
isDaida = true;
currentDaida = false;
ratios[batter] = daidaRatio;
}
}
var ratio = ratios[batter];
var h = 0;
do {
var r = Math.random();
var hit = false;
if(r < ratio) {
h++;
hit = true;
}
} while(hit && h < 3);
if(h == 0) {
// out
outs++;
} else {
// hit
score += calcHit(h, bases);
}
if(isDaida) {
if(batter == 8) {
ratios[batter] = 0;
} else {
ratios[batter] = daidaRatioAfter;
}
}
batter++;
var s = (bases[0]?"o":"_") + (bases[1]?"o":"_") + (bases[2]?"o":"_");
//console.log("batter " + batter + ": " + h + " " + s + " score: " + score);
if(batter >= 9) { batter = 0; }
}
}
//console.log("score: " + score);
sum += score;
}
console.log("ave score:" + (sum / k));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment