/    Sign up×
Community /Pin to ProfileBookmark

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’])?$_SESSION[‘dice_one’]:0;
$_
SESSION[‘dice_two’] = isset($_SESSION[‘dice_two’])?$_SESSION[‘dice_two’]:0;
$_SESSION[‘humancounter’] = isset($_SESSION[‘humancounter’])?$_SESSION[‘humancounter’]:0;
$_
SESSION[‘compcounter’] = isset($_SESSION[‘compcounter’])?$_SESSION[‘compcounter’]:0;
?>
<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’])?$_POST[‘hold’]:false;
$roll = isset($_POST[‘roll’])?$_POST[‘roll’]:false;
$counter = $_POST[‘counter’];
$boxcounter = $_
POST[‘boxcounter’];
$compcounter = $_POST[‘compcounter’];
$compboxcounter = $_
POST[‘compboxcounter’];

$output = “”;
// Hold the roll
if(isset($_POST[‘hold’])){
$humanRoll1 = $_
SESSION[‘dice_one’];
$humanRoll2 = $_SESSION[‘dice_two’];
}else{
// Calculate Dice Rolls
$humanRoll1 = rand(1,6);
$_
SESSION[‘dice_one’] = $humanRoll1 ;
$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>

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @jerseydrummer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...