Page 1 of 1

Can anybody help with this basic function excercise

Posted: Tue Jul 28, 2009 1:04 pm
by irishjoe
Hi,

I am trying to figure out how to write a function (or combination of functions) that will perform the same action as this code

Code: Select all

$bet = $_GET['bet'];
 
if ( ( $bet == 'even' && ( rand(1, 6) + rand(1, 6) ) % 2 == 0 ) || 
     ( $bet == 'odd'  && ( rand(1, 6) + rand(1, 6) ) % 2 != 0 ) )
{
    echo '<p>You win!</p>';
}
else
{
    echo '<p>You lose!</p>';
}
   
Basially $bet contains the term "even" or "odd". The aim is to select even or odd on a form, and if the sum of the two dice is odd or even (depending on what you selected), you win.

I have tired a few different things that didnt work and I'm sure its a fairly simple function.
Any help or suggestions will be greatly appreciated :)

Thanks very much.

Re: Can anybody help with this basic function excercise

Posted: Tue Jul 28, 2009 1:48 pm
by prometheuzz
Something like this:

Code: Select all

function win_or_lose($bet) {
  $even = (rand(1, 6) + rand(1, 6))%2 == 0;
  if($bet == 'even') {
    return $even ? "Win" : "Lose";
  }
  else {
    return $even ? "Lose" : "Win";
  }
}
?

Re: Can anybody help with this basic function excercise

Posted: Tue Jul 28, 2009 1:51 pm
by andyhoneycutt
Would this work for ya? Returns true or false based on is odd and they bet odd, is even and they bet even.

Code: Select all

function bet($x)
{
  return ($x == "odd") ? (rand(1, 6) + rand(1, 6) ) % 2 != 0) : (rand(1, 6) + rand(1, 6) ) % 2 == 0);
}

Re: Can anybody help with this basic function excercise

Posted: Tue Jul 28, 2009 5:04 pm
by irishjoe
Thanks for the replies! Both helped alot.
Exactly what I was looking for
:D