Page 1 of 1

Dice + Keeping State

Posted: Sat Mar 07, 2009 1:54 pm
by mikami
So I'm a .php novice. I'm making this dice game and it involves keeping state.
I have it right now so you see an image of 5 dice, they have 5 radio buttons beneath them, and if you hit the roll button it randomizes the dice.
That was easy.

However, I can't seem to keep state for the life of me. ._.; I was wondering if anyone could help me? I'm just trying to get it to where if someone checks a radio box, the dice in that "slot" does not change. I just can't figure out the logic. lakdjf;ajd;fa;. I'm about to pull my hair out, been working on it for 4 hours.

This is what I have so far;

Code: Select all

<? session_start(); ?>
<html>
<head>
<title>Yahtzee! Or something kind of like it...</title>
</head>
<body>
<center>
 
<?
 
$d1 = mt_rand(1,6);
$d2 = mt_rand(1,6);
$d3 = mt_rand(1,6);
$d4 = mt_rand(1,6);
$d5 = mt_rand(1,6);
 
lock1 = 0;
lock2 = 0;
lock3 = 0;
lock4 = 0;
lock5 = 0;
 
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" value"<?$d1?>/></td>
<td><input type="checkbox" name="lock2" id="lock2" value"<?$d2?>/></td>
<td><input type="checkbox" name="lock3" id="lock3" value"<?$d3?>/></td>
<td><input type="checkbox" name="lock4" id="lock4" value"<?$d4?>/></td>
<td><input type="checkbox" name="lock5" id="lock5" value"<?$d5?>/></td>
</table>
 
<p><input type="submit" value="Roll dice!" />
</center>
</form>
 
</body>
</html>
 
Any help would be appreciated.

Re: Dice + Keeping State

Posted: Sat Mar 07, 2009 2:13 pm
by jayshields

Code: Select all

 
<table border="0" cellpadding="13.5">
<td><input type="checkbox" name="lock1" id="lock1" <?php if(isset($_POST['lock1'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock2" id="lock2" <?php if(isset($_POST['lock2'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock3" id="lock3" <?php if(isset($_POST['lock3'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock4" id="lock4" <?php if(isset($_POST['lock4'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock5" id="lock5" <?php if(isset($_POST['lock5'])) echo 'checked="checked"'; ?> />/></td>
</table>
 
 
Try swapping that into your code. Something like that anyway. Also don't use short tags.

I haven't read your code thoroughly, you might need to use $_SESSION, but I've given you the logic.

Re: Dice + Keeping State

Posted: Sat Mar 07, 2009 2:23 pm
by mikami
That didn't quite work. :( It's still not locking. Is it because I'm not tying in a variable somewhere?

Like, in my

Code: Select all

 else if ($roll == 6) 
  {
    $_SESSION['six'] = $_SESSION['six'] +1;
  }
kind of code, I don't really tie locking or the dice into it. Perhaps those should be d1, d2, d3, etc?

Also, here is an example of the program running: http://silo.cs.indiana.edu:46011/dice.php

edit:

Well I tried changing that and that didn't work either. .-.;

Code: Select all

 
<? session_start(); ?>
<html>
<head>
<title>Yahtzee! Or something kind of like it...</title>
</head>
<body>
<center>
 
<?
 
$d1 = mt_rand(1,6);
$d2 = mt_rand(1,6);
$d3 = mt_rand(1,6);
$d4 = mt_rand(1,6);
$d5 = mt_rand(1,6);
 
lock1 = 0;
lock2 = 0;
lock3 = 0;
lock4 = 0;
lock5 = 0;
 
for ($i = 1; $i <= 5; $i++) 
{
  //$roll = mt_rand(1,6);
  echo "<img src='dice{$roll}.gif' alt='$roll' /> ";
 
  if ($d1 == 1) 
  {
    $_SESSION['d1'] = $_SESSION['d1'] + 1;
  } 
  else if ($d2 == 2) 
  {
    $_SESSION['d2'] = $_SESSION['d2'] + 1;
  } 
  else if ($d3 == 3) 
  {
    $_SESSION['d3'] = $_SESSION['d3'] + 1;
  } 
  else if ($d4 == 4) 
  {
    $_SESSION['d4'] = $_SESSION['d5'] + 1;
  } 
  else if ($d5 == 5) 
  {
    $_SESSION['d5'] = $_SESSION['d5'] + 1;
  } 
  else if ($d6 == 6) 
  {
    $_SESSION['d6'] = $_SESSION['d6'] +1;
  }
}
?>
 
<?
 
 
?>
 
<form>
 
 
<table border="0" cellpadding="13">
<td><input type="checkbox" name="lock1" id="lock1" 
<?php if(isset($_SESSION['lock1'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock2" id="lock2" 
<?php if(isset($_SESSION['lock2'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock3" id="lock3" 
<?php if(isset($_SESSION['lock3'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock4" id="lock4" 
<?php if(isset($_SESSION['lock4'])) echo 'checked="checked"'; ?> /></td>
<td><input type="checkbox" name="lock5" id="lock5" 
<?php if(isset($_SESSION['lock5'])) echo 'checked="checked"'; ?> />/></td>
</table>
 
<p><input type="submit" value="Roll dice!" />
</center>
</form>
 
</body>
</html>
 
 

Re: Dice + Keeping State

Posted: Sat Mar 07, 2009 2:58 pm
by jayshields
Not only do I not know how to play Yahtzee, but I don't understand what your code is trying to do either. If you haven't got register globals set to ON, which you shouldn't, then you should be grabbing form inputs using the $_POST superglobal. I don't really think you need to use sessions here at all, just use $_POST to carry form submissions from page to page.