<?php

$var_array
= array(
        
'Starting Bankroll' => 'bankroll',
        
'Starting Bet' => 'min_bet',
        
'Maximum Bet' => 'max_bet',
        
'Probability Of Winning One Hand' => 'p',
        
'House Pays' => 'house_pays',
        
'Percent Bankroll Increase Before Quitting' => 'pcnt_bankroll',
        
'Number of Games to Play' => 'games',
        
'Amount to Add to Each Bet' => 'bet_add'
);

extract($_GET);
$bankroll = isset($bankroll) ? $bankroll : '10000';
$orig_bankroll = $bankroll;
$min_bet = isset($min_bet) ? $min_bet: '1';
$max_bet = isset($max_bet) ? $max_bet: '1000';
$bet_add = isset($bet_add) ? $bet_add: '0';

/**
* probability of player winning a hand
*/
$p = isset($p) ? $p : '0.48';

// make sure it's at least sort of realistic..
if($p < 0.01) {
        
$p = 0.01;
}

$house_pays = isset($house_pays) ? $house_pays : 1;
if(
$house_pays < 0.01) {
        
$house_pays = 0.01;
}

$pcnt_bankroll = isset($pcnt_bankroll) ? $pcnt_bankroll : 10;
$verbose = isset($verbose) ? $verbose : 0 ;
if(isset(
$games)) {
        if (
$games < 1) {
                
$games = 1;
        } elseif (
$games > 1000) {
                
$games = 1000;
        }
} else {
        
$games = 1;
}

?>
<html>
<head>
<style>
body {font-family: helvetica, sans-serif; padding:10px;}
.main {padding:10px;}
h1 {color: chocolate; font-size: 16px;}
h3 {color: green;}
a {color: slateblue;}
input {width: 60px; border: 1px solid silver; padding:2px;}
.check {width: 15px;}
form {padding: 5px; border: 1px solid silver; background: #f9f9f9;
width:300px;}
table {border:none}
td {border:none}
</style>
</head>
<body>
<h1>Bet-doubling Simulator</h1>
<div class="main">
<p><a href="source.html">Source Code</a> | <a href="index.php">Reset Defaults</a></p>

<form action="<?=$_SERVER['PHP_SELF']?>" method="get">
<table>
<?php foreach ($var_array as $title=>$value) : ?>
<tr>
<td><?=$title?>:</td><td> <input type="text" name="<?=$value?>" value="<?=${$value}?>"></td>
</tr>
<?php endforeach ?>
<tr><td>Verbose output <input class ="check" type="checkbox" value="1" name="verbose" <?php if($verbose) {echo "checked=\"checked\"";} ?>/></td></tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>


<?php
$prev_bet
= $min_bet;
$total_winnings = 0;
for (
$i = 1; $i <= $games; $i++) {
        
$bankroll = $orig_bankroll;
        
$win_flag = 0;
        
$count = 0;
        
$bet = 0;
        
$payoff = 0;
        
$hand_count = 0;
        
$max_bankroll = (1 + $pcnt_bankroll / 100) * $orig_bankroll;


        while (
                (!
$win_flag) || (
                        (
$bankroll > 0) && ($bankroll < $max_bankroll)
                )
        ) {

                
//double bet each time
                //$bet = min(min($min_bet * pow(2,$count), $bankroll),$max_bet);

                
if ($count == 0) {
                        
$bet = $min_bet;
                } else {
                
$bet = min(min($prev_bet * 2 + $bet_add, $bankroll + $bet_add)
                        ,
$max_bet + $bet_add) ;
                }
                
$prev_bet = $bet;
                
$x = rand(0,1000)/1000;
                
$count ++;
                
$hand_count ++;

                if (
$x < $p) {
                        
$win_flag = 1;
                        
$payoff = $bet * $house_pays;
                        
$bankroll = $bankroll + $payoff;
                        
$outcome = "<b>win $payoff</b>";

                } else {
                        
$win_flag = 0;
                        
$bankroll = $bankroll - $bet;
                        
$outcome = "lose $bet";
                }

                if(
$verbose) {
                        echo
"<p>Hand $count : Bet: $bet, Outcome: $outcome, Ending bankroll: $bankroll";
                }

                if (
$win_flag == 1) {
                        
$bet = $min_bet;
                        
$count = 0;
                }
        }
        echo
"<h3>Game $i</h3>";
        
out($hand_count, "Total Hand Count");
        
out($bankroll, "Ending Bankroll");
        
$total_winnings += $bankroll - $orig_bankroll;
}
?><hr/><?php
out
($total_winnings, "Grand Total Winnings");

/**
* Functions
*/

function out($text, $title=null) {
        echo
"<p>";
        if(
$title) {
                echo
"<b>$title</b>: ";
        }
        echo
" $text";
        echo
"</p>";
}
?>
</div>
</body>
</html>