Hi Guys,
I am working on a game called PIG (A Dice Game) and I can’t figure out how to add the scores up to 100. I was wondering if anyone can show me what I need to do? Basically I want it to every time when clicking roll to display the user’s score hold the users score and add it until reaching 100. Hope this makes sense thanks! Here is the code:
<?php
session_start();
$_SESSION[‘dice_one’] = isset($_SESSION[‘dice_one’]:0;
$_SESSION[‘dice_two’])?$
$_SESSION[‘humancounter’] = isset($_SESSION[‘humancounter’]:0;
$_SESSION[‘compcounter’])?$
?>
<html>
<head>
<title>Two-Dice Pig</title>
<style type=”text/css”>
img {
width: 90px; height: 90px; margin-right: 5px; }
p {
width: 100px;
height: 50px;
border: 1px solid #000;
padding: 5px; }
</style>
</head>
<body>
<h1>A Game of Two-Dice Pig!</h1>
<?php
// Get the values from the form
$hold = isset($_POST[‘hold’])?$
$roll = isset($_POST[‘roll’])?$
$counter = $_POST[‘counter’];
$boxcounter = $
$compcounter = $_POST[‘compcounter’];
$compboxcounter = $
$output = “”;
// Hold the roll
if(isset($_POST[‘hold’])){
$humanRoll1 = $
$humanRoll2 = $_SESSION[‘dice_two’];
}else{
// Calculate Dice Rolls
$humanRoll1 = rand(1,6);
$
$humanRoll2 = rand(1,6);
$_SESSION[‘dice_two’] = $humanRoll2;
}
$comp1Roll1 = rand(1,6);
$comp1Roll2 = rand(1,6);
$humanSum = $humanRoll1 + $humanRoll2;
$comp1Sum = $comp1Roll1 + $comp1Roll2;
// Count and hold the values for human
$counter = $hold + $humanRoll1 + $humanRoll2;
$_SESSION[‘humancounter’] = $boxcounter = $counter;
// Count and hold the values for human
$compcounter = $comp1Roll1 + $comp1Roll2;
$_SESSION[‘compcounter’] = $compboxcounter = $compcounter;
if($humanSum > $comp1Sum){
$output = “Congratulations. You have won!”;
}else if($comp1Sum > $humanSum){
$output = “Sorry! The computer won.”;
}else if($comp1Sum == $humanSum){
$output = “It was a tie!”;
}
$output .= sprintf(“<h3>You %s a $humanRoll1 and a $humanRoll2</h3>”,($hold?”held”:”rolled”));
$output .= “<img src=’dice{$humanRoll1}.jpg’>”;
$output .= “<img src=’dice{$humanRoll2}.jpg’>”;
$output .= “<h3>The computer rolled a $comp1Roll1 and a $comp1Roll2</h3>”;
$output .= “<img src=’dice{$comp1Roll1}.jpg’>”;
$output .= “<img src=’dice{$comp1Roll2}.jpg’>”;
// Output
echo $output;
print ” <p>Your Score: $boxcounter</p>”;
print ” <p>Computer Score: $compboxcounter</p>”;
?>
<form method=”post” action=”<?php print $_SERVER[‘PHP_SELF’]?>”>
<table>
<tr>
<th>You</th>
</tr>
<tr>
<td>
<input type=”hidden” name=”counter” value=”$counter1″>
<input type=”hidden” name=”boxcounter” value=”$boxcounter1″>
</td>
</tr>
<tr>
<td>
<input type=”submit” name=”roll” value=”Roll”>
<input type=”submit” name=”hold” value=”Hold”>
</td>
</tr>
</table>
<table>
<tr>
<th></th>
</tr>
<tr>
<td>
<input type=”hidden” name=”compcounter” value=”$compcounter1″>
<input type=”hidden” name=”compboxcounter” value=”$compboxcounter1″>
</td>
</tr>
</table>
</form>
</body>
</html>