Skip to content

Instantly share code, notes, and snippets.

@cou929
Created May 30, 2011 23:22
Show Gist options
  • Save cou929/999625 to your computer and use it in GitHub Desktop.
Save cou929/999625 to your computer and use it in GitHub Desktop.
tmp.js
BaseBallGame.prototype.pitch = function(toHitting, toBunt, score) {
var strike_prob = 0.6;
var goro_prob = 0.3;
var swing_miss_prob = 0.2;
var hit_prob = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 4];
var strike = Math.random() > strike_prob;
var base = hit_prob[parseInt(Math.random() * 10, 10)];
var isHit = false;
var isGoro = Math.random() < goro_prob;
var isSwingMiss = false;
if (strike && toHitting) {
if (Math.random() > swing_miss_prob) {
// hit
isHit = true;
} else {
// swing miss strike
isSwingMiss = true;
score.updateCount(true);
}
} else if (strike && !toHitting) {
// strike
score.updateCount(true);
} else if (!strike && toHitting) {
// fall
if (score.count.strike < 2) {
score.updateCount(true);
}
} else if (!strike && !toHitting) {
// ball
score.updateCount(false);
}
if (isHit) {
if (toBunt) {
// bunt
score.hit(1);
score.outCount();
score.runner[0] = 0;
} else if (isGoro) {
// goro
score.outCount();
} else {
// hit
score.hit(base);
score.clearCount();
}
}
return {
isHit: isHit,
hitBase: base,
isGoro: isGoro,
isBunt: toBunt,
isStrike: strike && !toHitting,
isBall: !strike && !toHitting,
isFall: !strike && toHitting,
isSwingMiss: isSwingMiss,
score: score
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment