Jump to content

PHP Code Help (Dice + Keeping State)


meritre

Recommended Posts

Hey there! I'm working on a game and I'm very much a novice to .php. However, I'm creating this dice game that basically all you do is roll dice and then you can click a radio button and it will let you keep that dice and re-roll the others.

 

I feel like I'm almost there, but I'm missing some sort of.. logic, I guess.

 

This is what I have so far;

<? session_start(); ?>
<html>
<head>
<title>Yahtzee! Or something kind of like it...</title>
</head>
<body>
<center>

<?

for ($i = 1; $i <= 5; $i++) 
{
  $roll = mt_rand(1,6);
  echo "<img src='dice{$roll}.gif' alt='$roll' /> ";

  if ($roll == 1) 
  {
    $_SESSION['one'] = $_SESSION['one'] + 1;
  } 
  else if ($roll == 2) 
  {
    $_SESSION['two'] = $_SESSION['two'] + 1;
  } 
  else if ($roll == 3) 
  {
    $_SESSION['three'] = $_SESSION['three'] + 1;
  } 
  else if ($roll == 4) 
  {
    $_SESSION['four'] = $_SESSION['four'] + 1;
  } 
  else if ($roll == 5) 
  {
    $_SESSION['five'] = $_SESSION['five'] + 1;
  } 
  else if ($roll == 6) 
  {
    $_SESSION['six'] = $_SESSION['six'] +1;
  }
}
?>

<?


?>

<form>

<table border="0" cellpadding="13.5">
<td><input type="checkbox" name="lock1" id="lock1" unchecked 

value"<?$dice?>/></td>
<td><input type="checkbox" name="lock1" id="lock1" unchecked 
value"<?$dice?>/></td>
<td><input type="checkbox" name="lock1" id="lock1" unchecked 
value"<?$dice?>/></td>
<td><input type="checkbox" name="lock1" id="lock1" unchecked 
value"<?$dice?>/></td>
<td><input type="checkbox" name="lock1" id="lock1" unchecked 
value"<?$dice?>/></td>
</table>

<p><input type="submit" value="Roll dice!" />
</center>
</form>

</body>
</html>

 

So basically what that does right now is it shows 5 dice and randomizes them when you press the 'Roll dice' button. Then underneath the 5 dice it has radio buttons.

 

So what I need it to do is have it to where when you press the radio check button it locks that dice into place, so when you press roll dice, that dice doesn't change.

I am just missing some sort of logic when it comes to keeping state x__x ...

 

Any help would be much appreciated, thank you!

 

 

Link to comment
https://forums.phpfreaks.com/topic/148368-php-code-help-dice-keeping-state/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.