#!/usr/bin/perl
#####
# program to calculate some craps odds with honest and crooked dice.
# 
# At first I expected to look at the 1st, 2nd, 3rd, etc roll,
# but then realized that if you get to the 2nd roll, the odds
# are determined by the ratio of p(7)/p(point), since 7 loses and point wins;
# all that matters is which comes up first.  So winning or losing from
# the 2nd roll on is simply the probability of getting to a specific point
# times win/lose probabilities with that ratio.
#
# With the crooked dice we've been playing around with,
# which are more likely to come up 1 or 6, you actually
# have a smaller chance at winning at craps, even though
# your odds of winning on the first roll are higher.
#
# - Jim Mahoney, Feb 6 2006
######
use strict;
use warnings;
use Data::Dumper;

my ($A, $B);                    # 1 die probabilities
my @p;                          # 2 dice probabilities
my @r;                          # after n rolls of 2 dice
my @points = (4,5,6,8,9,10);    # numbers that continue on first roll
my ($win, $lose);

# Usage: e.g. set_A(1/6);
sub set_A {
  $A = shift;          # = p(1) = p(6) ; one die
  $B = (1.0-2*$A)/4.;  # = p(2) = p(3) = p(4) = p(5); one die

  $p[0] = $p[1] = 0;
  $p[2] = $p[12] = $A**2;
  $p[3] = $p[11] = 2 * $A * $B;
  $p[4] = $p[10] = 2 * $A * $B + 1 * $B**2;
  $p[5] = $p[9]  = 2 * $A * $B + 2 * $B**2;
  $p[6] = $p[8]  = 2 * $A * $B + 3 * $B**2;
  $p[7] = 2 * $A**2 + 4 * $B**2;

  $r[1] = {};
  $win  = $r[1]->{win}  = $p[7] + $p[11];
  $lose = $r[1]->{lose} = $p[2] + $p[3] + $p[12];
  $r[1]->{continue} = $p[4] + $p[5] + $p[6] + $p[8] + $p[9] + $p[10];
  for my $point (@points){
    $r[1]->{$point} = $p[$point];
    $r[1]->{$point . "_odds"} = $p[$point]/$p[7];
    $win   += $p[$point] * $p[$point]/($p[$point] + $p[7]);
    $lose  += $p[$point] * $p[7]/($p[$point] + $p[7]);
  }
  print " p(1)=p(6)= " . round($A) . "\n";
  print " p(2)=p(3)=p(4)=p(5)= " . round($B) . "\n";
  print " prob of 1st roll win      = " . round($r[1]->{win}) . "\n";
  print " prob of 1st roll lose     = " . round($r[1]->{lose}) . "\n";
  print " prob of 1st roll continue = " . round($r[1]->{continue}) . "\n";
  print " prob of overall win  = " . round($win) . "\n";
  print " prob of overall lose = " . round($lose) . " \n";
  print "\n";
}

sub round {
  my $x = shift;
  return sprintf("%4.3g",$x);
}

my $A_honest = 1./6.;
my $A_loaded = 0.203;  # best guess from 2006 toolbox & 2005 data
my $sigma    = 0.013;  # weighted average of 2006 & 2005; N = 1300

print " -- honest dice -- \n";
set_A( $A_honest );

print " -- best guess for loaded dice -- \n";
set_A( $A_loaded );

print " -- loaded + 2 sigma -- \n";
set_A( $A_loaded + 2*$sigma );

print " -- loaded - 2 sigma -- \n";
set_A( $A_loaded - 2*$sigma );


__END__

mahoney@cs dice_and_statistics$ ./craps.pl 
 -- honest dice -- 
 p(1)=p(6)= 0.167
 p(2)=p(3)=p(4)=p(5)= 0.167
 prob of 1st roll win      = 0.222
 prob of 1st roll lose     = 0.111
 prob of 1st roll continue = 0.667
 prob of overall win  = 0.493
 prob of overall lose = 0.507 

 -- best guess for loaded dice -- 
 p(1)=p(6)= 0.203
 p(2)=p(3)=p(4)=p(5)= 0.148
 prob of 1st roll win      = 0.231
 prob of 1st roll lose     = 0.143
 prob of 1st roll continue = 0.626
 prob of overall win  = 0.471
 prob of overall lose = 0.529 

 -- loaded + 2 sigma -- 
 p(1)=p(6)= 0.229
 p(2)=p(3)=p(4)=p(5)= 0.136
 prob of 1st roll win      = 0.24
 prob of 1st roll lose     = 0.167
 prob of 1st roll continue = 0.593
 prob of overall win  = 0.454
 prob of overall lose = 0.546 

 -- loaded - 2 sigma -- 
 p(1)=p(6)= 0.177
 p(2)=p(3)=p(4)=p(5)= 0.161
 prob of 1st roll win      = 0.224
 prob of 1st roll lose     = 0.12
 prob of 1st roll continue = 0.656
 prob of overall win  = 0.487
 prob of overall lose = 0.513 
