•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 429,788 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,884 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1357 | Replies: 1
![]() |
•
•
Join Date: Nov 2005
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hey Everyone,
I've been working on a PHP craps game, and I'm not sure why my code isn't working. Everything seems logical to me, but I can't get my inner loop to work.
The game of craps:
1) Roll a pair of die and add their some
2)IF on the 1st roll the sum == 7 or 11 then the user wins
IF on 1st roll sum == 2,3, or 12 user loses
IF it is any other number, THEN that number becomes the "point" that the user has to continue to roll to get. IF the user gets the 'point' then they win that round, unless they roll a 7, in which case they lose that round
Here is my code:
[php]<?
if($message) {
if($userRoll==1){
$rollSum=(rand(1,6) + rand(1,6));
if ($rollSum==7 || $rollSum==11){
$userScore +=1;
$message="You Won!, User Score: $userScore , Comp Score: $compScore";
$userRoll=1;
} else {
if($rollSum==2 || $rollSum==3 || $rollSum==12){
$compScore +=1;
$message="You Lost!, User Score: $userScore, Comp Score: $compScore";
$userRoll=1;
} else {
$message="Keep Rolling for Point..";
$userRoll=0;
$pointRoll=(rand(1,6) + rand(1,6));
$point=$rollSum;
if($point==$pointRoll){
$userScore +=1;
$message="You Won The Point! UserScore: $userScore, Comp Score: $compScore";
$userRoll=1;
$point=0;
$pointRoll=0;
} else {
if($pointRoll==7){
$compScore +=1;
$message="You Lost The Point, User Score
userScore, Comp Score: $compScore";
$userRoll=1;
$point=0;
$pointRoll=0;
}
}
}
}
}
} else {
$message= "Welcome to the Craps Game.";
$compScore=0;
$userScore=0;
$userRoll=1;
}
?>
<html><head><title>Craps with hidden fields</title></head><body>
<p> <h2>Craps Game </h2> <br/>
<br/>
<?=$message?> <br/>
<br/>
<form>
Press <input type="submit" value="Proceed" / > to play.
<input type="hidden" name="message" value="<?=$message?>">
<input type="hidden" name="userScore" value="<?=$userScore?>">
<input type="hidden" name="compScore" value="<?=$compScore?>">
<input type="hidden" name="rollSum" value="<?=$rollSum?>">
<input type="hidden" name="pointRoll" value="<?=$pointRoll?>">
<input type="hidden" name="point" value="<?=$point?>">
<input type="hidden" name="userRoll" value="<?=$userRoll?>">
</form>
<p> Roll: <?=$rollSum?>
<p> Point Roll: <?=$pointRoll?>
<p> Point: <?=$point?>
<br/>
<br/>
Computer Score: <?=$compScore?> <br/>
User Score: <?=$userScore?>
<br/>
User Roll: <?=$userRoll?>
</body>
</html>[/php]
I've been working on a PHP craps game, and I'm not sure why my code isn't working. Everything seems logical to me, but I can't get my inner loop to work.
The game of craps:
1) Roll a pair of die and add their some
2)IF on the 1st roll the sum == 7 or 11 then the user wins
IF on 1st roll sum == 2,3, or 12 user loses
IF it is any other number, THEN that number becomes the "point" that the user has to continue to roll to get. IF the user gets the 'point' then they win that round, unless they roll a 7, in which case they lose that round
Here is my code:
[php]<?
if($message) {
if($userRoll==1){
$rollSum=(rand(1,6) + rand(1,6));
if ($rollSum==7 || $rollSum==11){
$userScore +=1;
$message="You Won!, User Score: $userScore , Comp Score: $compScore";
$userRoll=1;
} else {
if($rollSum==2 || $rollSum==3 || $rollSum==12){
$compScore +=1;
$message="You Lost!, User Score: $userScore, Comp Score: $compScore";
$userRoll=1;
} else {
$message="Keep Rolling for Point..";
$userRoll=0;
$pointRoll=(rand(1,6) + rand(1,6));
$point=$rollSum;
if($point==$pointRoll){
$userScore +=1;
$message="You Won The Point! UserScore: $userScore, Comp Score: $compScore";
$userRoll=1;
$point=0;
$pointRoll=0;
} else {
if($pointRoll==7){
$compScore +=1;
$message="You Lost The Point, User Score
userScore, Comp Score: $compScore";$userRoll=1;
$point=0;
$pointRoll=0;
}
}
}
}
}
} else {
$message= "Welcome to the Craps Game.";
$compScore=0;
$userScore=0;
$userRoll=1;
}
?>
<html><head><title>Craps with hidden fields</title></head><body>
<p> <h2>Craps Game </h2> <br/>
<br/>
<?=$message?> <br/>
<br/>
<form>
Press <input type="submit" value="Proceed" / > to play.
<input type="hidden" name="message" value="<?=$message?>">
<input type="hidden" name="userScore" value="<?=$userScore?>">
<input type="hidden" name="compScore" value="<?=$compScore?>">
<input type="hidden" name="rollSum" value="<?=$rollSum?>">
<input type="hidden" name="pointRoll" value="<?=$pointRoll?>">
<input type="hidden" name="point" value="<?=$point?>">
<input type="hidden" name="userRoll" value="<?=$userRoll?>">
</form>
<p> Roll: <?=$rollSum?>
<p> Point Roll: <?=$pointRoll?>
<p> Point: <?=$point?>
<br/>
<br/>
Computer Score: <?=$compScore?> <br/>
User Score: <?=$userScore?>
<br/>
User Roll: <?=$userRoll?>
</body>
</html>[/php]
Last edited by Gary King : Nov 13th, 2005 at 2:51 pm.
•
•
Join Date: Jul 2004
Location: Sydney, Australia
Posts: 166
Reputation:
Rep Power: 5
Solved Threads: 7
You're not collecting the submitted values anywhere. Use the global $_GET['fieldname'] values to retrieve the values of the hidden fields when the form is submitted.
If I've been a help please confirm by clicking the Add to Lafinboy's Reputation link in the header of this reply.
Lafinboy Productions
:: Website Design :: Website Development ::
Lafinboy Productions
:: Website Design :: Website Development ::
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- What do you think of a Game Development forum? (DaniWeb Community Feedback)
- Craps, game help! (C++)
- PHP coder needed for online game (Web Development Job Offers)
- C craps game (C)
Other Threads in the PHP Forum
- Previous Thread: PHP Pop-up attribute question
- Next Thread: Function variables


Linear Mode