Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,767 members, 7,817,117 topics. Date: Saturday, 04 May 2024 at 06:20 AM

Help On Php Code For A Dice Game - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help On Php Code For A Dice Game (5569 Views)

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)

(1) (Reply) (Go Down)

Help On Php Code For A Dice Game by jboy01(m): 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. wink , i would advice you store your die images in and array.
Re: Help On Php Code For A Dice Game by jboy01(m): 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(m): 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(m): 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 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>
were 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(m): 8:10pm On Sep 29, 2012
jacob05: were you able to run this? Because i'm getting errors while running your code
It 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. wink

1 Like

Re: Help On Php Code For A Dice Game by jboy01(m): 10:44pm On Sep 29, 2012
jacob05: 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. wink
Ok thank u

(1) (Reply)

Tytit users keep growing since it launched on 7th September / Code Challenge [1]: Pseudo-code, C#, JAVA (apply Object Oriented Principles) / Dll Load Failure With Py2exe (python And Qt)

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 32
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.