Help On Php Code For A Dice Game - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Help On Php Code For A Dice Game (5772 Views)
| Help On Php Code For A Dice Game by jboy01(op): 5:21pm On Sep 26, 2012 |
pls Gurus, i wrote the below codes for a dice game with php, but the problem is how to make it GAME OVER after 5 wrong guessing. This is the codes: <html> <head> <title>Reloaded</title> </head> <body> <center> <h1>Dice Game Reloaded</h1> <?php //declearing the variable name for the dices $dice1="<img src='dice1.png'>"; $dice2="<img src='dice2.png'>"; $dice3="<img src='dice3.png'>"; $dice4="<img src='dice4.png'>"; $dice5="<img src='dice5.png'>"; $dice6="<img src='dice6.png'>"; ?> <?php $roll=rand(1, 6); echo "<h2>You roll a $roll</h2>"; ?> <?php //displaying pics with respect to random roll generated if ($roll==1) print $dice1; elseif ($roll==2) print $dice2; elseif ($roll==3) print $dice3; elseif ($roll==4) print $dice4; elseif ($roll==5) print $dice5; elseif ($roll==6) print $dice6; else print ""; ?> <br> <br> <?php //number of attempt before game over for ( $attempt<=5; $attempt=0; $attempt++ ) ?> <?php if ($guess != $roll){ echo <<<HERE <form method="post" action=""> <table> <tr> <th>Input your Guess here</th> </tr> <tr> <td> <input type="text" name="guess"> </td> </tr> <tr> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> HERE; //else { //echo "<h1>GAME OVER</h1>"; //} } else { print"<h2>YOU win</h2>" ; echo "<a href='reloaded.php'> Play Another Game</a>"; } ?> </center> </body> </html> |
| Re: Help On Php Code For A Dice Game by bakenda(m): 11:48pm On Sep 28, 2012 |
Can you explain more how the game is played, is the guessing consecutive for a player, I mean, does a player guess 5 times consecutively...? |
| Re: Help On Php Code For A Dice Game by jacob05(m): 8:37am On Sep 29, 2012 |
You can use the $_SESSION global variable to store the number of attempt the user made. , i would advice you store your die images in and array. |
| Re: Help On Php Code For A Dice Game by jboy01(op): 6:39pm On Sep 29, 2012 |
Thank you pals, I have gotten the by myself. I use <html> <head> <title>Reloaded</title> </head> <body> <center> <h1>Dice Game Reloaded</h1> <?php //declearing the variable name for the dices $dice1="<img src='dice1.png'>"; $dice2="<img src='dice2.png'>"; $dice3="<img src='dice3.png'>"; $dice4="<img src='dice4.png'>"; $dice5="<img src='dice5.png'>"; $dice6="<img src='dice6.png'>"; ?> <?php $roll=rand(1, 6); echo "<h2>You roll a $roll</h2>"; ?> <?php //displaying pics with respect to random roll generated if ($roll==1) print $dice1; elseif ($roll==2) print $dice2; elseif ($roll==3) print $dice3; elseif ($roll==4) print $dice4; elseif ($roll==5) print $dice5; elseif ($roll==6) print $dice6; else print ""; ?> <br> <?php $hdncounter++; $hdnboxcounter++; If($hdnboxcounter==6){ Echo "<h1>GAME OVER</h1>"; } Elseif ($guess != $roll){ echo <<<HERE <form method="post" action=""> <table> <tr> <th>Input your Guess here</th> </tr> <tr> <td> <input type="text" name="guess"> <Input type="hidden" Name="hdncounter" value="$hdncounter"> <Input type="hidden" Name="hdnboxcounter" Value="$hdnboxcounter"> </td> </tr> <tr> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> HERE; } else { print"<h2>YOU win</h2>" ; echo "<a href='reloaded.php'> Play Another Game</a>"; } ?> </center> </body> </html> |
| Re: Help On Php Code For A Dice Game by jboy01(op): 6:41pm On Sep 29, 2012 |
Thank you pals, I have gotten the by myself. I use <html> <head> <title>Reloaded</title> </head> <body> <center> <h1>Dice Game Reloaded</h1> <?php //declearing the variable name for the dices $dice1="<img src='dice1.png'>"; $dice2="<img src='dice2.png'>"; $dice3="<img src='dice3.png'>"; $dice4="<img src='dice4.png'>"; $dice5="<img src='dice5.png'>"; $dice6="<img src='dice6.png'>"; ?> <?php $roll=rand(1, 6); echo "<h2>You roll a $roll</h2>"; ?> <?php //displaying pics with respect to random roll generated if ($roll==1) print $dice1; elseif ($roll==2) print $dice2; elseif ($roll==3) print $dice3; elseif ($roll==4) print $dice4; elseif ($roll==5) print $dice5; elseif ($roll==6) print $dice6; else print ""; ?> <br> <?php $hdncounter++; $hdnboxcounter++; If($hdnboxcounter==6){ Echo "<h1>GAME OVER</h1>"; } Elseif ($guess != $roll){ echo <<<HERE <form method="post" action=""> <table> <tr> <th>Input your Guess here</th> </tr> <tr> <td> <input type="text" name="guess"> <Input type="hidden" Name="hdncounter" value="$hdncounter"> <Input type="hidden" Name="hdnboxcounter" Value="$hdnboxcounter"> </td> </tr> <tr> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> HERE; } else { print"<h2>YOU win</h2>" ; echo "<a href='reloaded.php'> Play Another Game</a>"; } ?> </center> </body> </html> |
| Re: Help On Php Code For A Dice Game by jboy01(op): 6:43pm On Sep 29, 2012 |
Thank you pals, I have gotten the by myself. I use <html> <head> <title>Reloaded</title> </head> <body> <center> <h1>Dice Game Reloaded</h1> <?php //declearing the variable name for the dices $dice1="<img src='dice1.png'>"; $dice2="<img src='dice2.png'>"; $dice3="<img src='dice3.png'>"; $dice4="<img src='dice4.png'>"; $dice5="<img src='dice5.png'>"; $dice6="<img src='dice6.png'>"; ?> <?php $roll=rand(1, 6); echo "<h2>You roll a $roll</h2>"; ?> <?php //displaying pics with respect to random roll generated if ($roll==1) print $dice1; elseif ($roll==2) print $dice2; elseif ($roll==3) print $dice3; elseif ($roll==4) print $dice4; elseif ($roll==5) print $dice5; elseif ($roll==6) print $dice6; else print ""; ?> <br> <?php $hdncounter++; $hdnboxcounter++; If($hdnboxcounter==6){ Echo "<h1>GAME OVER</h1>"; } Elseif ($guess != $roll){ echo <<<HERE <form method="post" action=""> <table> <tr> <th>Input your Guess here</th> </tr> <tr> <td> <input type="text" name="guess"> <Input type="hidden" Name="hdncounter" value="$hdncounter"> <Input type="hidden" Name="hdnboxcounter" Value="$hdnboxcounter"> </td> </tr> <tr> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> HERE; } else { print"<h2>YOU win</h2>" ; echo "<a href='reloaded.php'> Play Another Game</a>"; } ?> </center> </body> </html> |
| Re: Help On Php Code For A Dice Game by jacob05(m): 7:00pm On Sep 29, 2012 |
I think you can do better than this. |
| Re: Help On Php Code For A Dice Game by jacob05(m): 7:05pm On Sep 29, 2012 |
jboy01: Thank you pals, I have gotten the by myself. I usewere you able to run this? Because i'm getting errors while running your code |
| Re: Help On Php Code For A Dice Game by jboy01(op): 8:10pm On Sep 29, 2012 |
jacob05: were you able to run this? Because i'm getting errors while running your codeIt run on my pc. Do you know that there are some images invove |
| Re: Help On Php Code For A Dice Game by jacob05(m): 9:18pm On Sep 29, 2012 |
The images aren't the issue but the undefined variables $hdncounter and $ hdnboxcounter. you really need improve your code. You can start by using an array for storing the images and also making use of the $_SESSION global variable instead of using hidden form values to store counts. ![]() |
| Re: Help On Php Code For A Dice Game by jboy01(op): 10:44pm On Sep 29, 2012 |
jacob05: The images aren't the issue but the undefined variables $hdncounter and $Ok thank u |
Using Finger Print Scanner On PHP • Free Ebooks On Php Progaming • Please I Need Someone Who Can Put Me Through On Php And Mysql. • 2 • 3 • 4
Are You A Programmer Or App Developer Who Wish To Work Remotely For A US Company • How To Create A Forum Like Nairaland • Solution To Tokenmismatchexception Problem In Laravel 5.1
, i would advice you store your die images in and array.