<? session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>2 Up game</title>
</head>

<body>
<? if(!$_POST['step']){
gameSetupForm();
}else{
if($_POST['step']==1){
$_SESSION['bank'] = $_POST['startMoney'];
bet();
}elseif($_POST['step']==2){
$betAmount = $_POST['bet'];
$choice = $_POST['choose'];

$coin[0] = rand(0,1);
$coin[1] = rand(0,1);
if($coin[0]==$coin[1]){
//game can play
if($choice == $coin[0]){
//win
echo "Bank is now: $" . $_SESSION['bank'] += $betAmount;
bet();
}else{
//lose
echo "Bank is now: $" . $_SESSION['bank'] -= $betAmount;
bet();
}
}else{
//missmatch re throw
echo "Mis-match";
bet();
}
}

}

 

function gameSetupForm(){
?>
<form action="<?=$SERVER['php_self']?>" method="post">
<fieldset><legend>How much money do you have to start the game?</legend>
<p><label for="startMoney">Initial funds $</label><input name="startMoney" type="text" placeholder="Enter an amount"></p>

<button type="submit" name="step" value="1">Next</button>
</fieldset>
</form>
<?

}

function bet(){
?>
<form action="<?=$SERVER['php_self']?>" method="post">
<fieldset><legend>How much would you like to bet?</legend>
<p><label for="bet">Bet amount $</label><input name="bet" type="text" placeholder="Enter an amount"></p>
<p><label for="Choose">Choose $</label><select name="choose">
<option value="0">Heads</option>
<option value="1">Tales</option>
</select></p>
<button type="submit" name="step" value="2">Play 2 Up</button>
</fieldset>
</form>
<?

}
?>

 

</body>
</html>