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

<body>

<?

if(!$_POST['submit'] ){
unset($_SESSION['purse']);
initialForm();
}else{
if(!isset($_SESSION['purse'])){
$_SESSION['purse'] = $_POST['bankAmount'];
}
$selection = $_POST['selection'];
$bet = $_POST['betAmount'];
//random coins

//TEMP MAX amount
$max = 1000;
$maxWinnings = 0;
$minWinnings = 0;

while($_SESSION['purse']!=0 && $_SESSION['purse']<=$max){
$coin = throwCoins();
playGame($coin, $selection,$bet);

if($_SESSION['purse']>$maxWinnings){
$maxWinnings = $_SESSION['purse'];
}
if($_SESSION['purse']<$minWinnings){
$minWinnings = $_SESSION['purse'];
}

}
echo "<h2>You reached a maximum winnings of $" . $maxWinnings . "</h2>";

echo "<h2>You reached a minimum winnings of $" . $minWinnings . "</h2>";



}

 

function throwCoins(){
//This function randomly generates the two coins and only until the two coins match will it return the $coin array with matching coins
$coin[0] = random_int(0,1);
$coin[1] = random_int(0,1);
if($coin[0] != $coin[1]){
//miss match, please again.
while($coin[0] != $coin[1]){

displayCoins($coin,"mismatch");
echo "<h2>Mismatch, throw again</h2>";
$coin[0] = random_int(0,1);
$coin[1] = random_int(0,1);
echo "<br>";
}
}
echo "<h2>Valid throw!</h2>";
displayCoins($coin,"valid");
return $coin;
}
function displayCoins($coin,$type){
if($type=="mismatch"){ $size = "100";}else{ $size = "250";}
echo "<p>";
foreach($coin as $val){
if($val==0){
echo "<img src=\"images/obverse.png\" height=\"" . $size ."\">";
}else{
echo "<img src=\"images/tales.png\" height=\"" . $size ."\">";
}
}
echo "</p>";
}

function initialForm(){
?>
<form method="post" action="<?=$_SERVER['php_self']?>">
<p><label for="bankAmount">How much would you like to gamble today? </label><input type="text" name="bankAmount" id="bankAmout"></p>
<p><label for="betAmount">Initial bet </label><input type="text" name="betAmount" id="betAmout"></p>
<p><label for="selection">Heads</label><input type="radio" name="selection" value="heads"> &nbsp; &nbsp; &nbsp; &nbsp; Tales<input type="radio" name="selection" value="tales"></p>
<p><button type="submit" name="submit" value="submit">Play 2-Up!</button>
</form>
<?
}

function playGame($coin, $selection, $bet){
if($coin[0] == $selection){
//winner
echo "<h1>Win</h1>";
echo "<p>You have $" . $_SESSION['purse'] += $bet . "</p>";
//Add bet amount to the purse
//Show the player how much they have left to bet
//Ask player for their next bet
}else{
//loser
echo "<h1>Lose</h1>";
echo "<p>You have $" . $_SESSION['purse'] -= $bet . "</p>";
//Remove bet amount to the purse
//Show the player how much they have left to bet
//If the player has some money left in their purse, ask player for their next bet

}
}
?>

</body>
</html>