Skip to content

Instantly share code, notes, and snippets.

@daynebatten
Last active August 29, 2015 14:12
Show Gist options
  • Save daynebatten/c0e0db221a3b91ff9aba to your computer and use it in GitHub Desktop.
Save daynebatten/c0e0db221a3b91ff9aba to your computer and use it in GitHub Desktop.
Calculate win probabilities from any Pass the Pigs game state
function calculate() {
for (i=0; i<=99; i++) {
probs[i]=[];
for (j=0; j<=99; j++) {
probs[i][j]=[];
for (k=0; k<=99; k++){
probs[i][j][k]=.5;
}
}
}
var maxChange=100;
while (maxChange>.001) {
maxChange=0;
for (i=0; i<=99; i++) {
for (j=0; j<=99; j++) {
for (k=0; k<=99; k++){
oldValue=probs[i][j][k];
probs[i][j][k]=Math.max(
.213005*getValue(i,j,k+1)
+.406224*getValue(i,j,k+5)
+.078484*getValue(i,j,k+10)
+.027834*getValue(i,j,k+15)
+.062288*getValue(i,j,k+20)
+.00042*getValue(i,j,k+25)
+.0009*getValue(i,j,k+40)
+.000049*getValue(i,j,k+60)
+.210796*(1-getValue(j,i,0))
,1-getValue(j,i+k,0)
);
if (Math.abs(probs[i][j][k]-oldValue)>maxChange) maxChange=Math.abs(probs[i][j][k]-oldValue);
}
}
}
}
}
function getValue(x, y, z) {
if (x+z>99) return(1);
if (y>99) return(0);
return(probs[x][y][z]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment