Jump to content


Photo

Learning Exercise


  • Please log in to reply
2 replies to this topic

#1 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 17 April 2006 - 01:01 PM

As a leaning exercise, I have written a game of Yatzee, the Dice game with 5 dice... But I am stuck...

I have to write the validation routines so if, for example, I role 5 dice, and the following is true:

D1=5 D2=1 D3=1 D4=4 D5=3

Now, I want to take the "Ones" catagory, so I can see that I have two "ones", for a score of 2 in the ones catagory.

Now, in PHP, the way I am doing it, is every catagory is set with a session variable that equals "Available" so I now present a radio button, and have the script compute the score for each "Available" catagory, so the player only has to select the radio button and press the submit button, then they go to their next turn.

I need to write the validation for each catagory, so this much is true:

If ($_SESSION['ones']=='AVAILABLE'){
// show the radio button then <--- I have this part already
// compute the score to enter if the person selects this catagory}

I suppose I have to to a VERY long (30+?) set of clauses, but there must be an easier way...

AND I have to do the clauses for each catagory...

Any suggestions, or am I on the right track, and just have to write all the clauses?

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 17 April 2006 - 08:56 PM

Well, I have figured out how to do the following:

1's
2's
3's
4's
5's
6's
three of a kind
four of a kind
scratch
yatzee

I still need to do small straight and large straight...

Here is how I did the others, but I don't know how to verify the small or large straight...

I know this is part of the solution:
<?php
$straight=array($D1,$D2,$D3,$D4,$D5);
sort($straight);
echo $straight[0];
echo $straight[1];
echo $straight[2];
echo $straight[3];
echo $straight[4];
?>
Someone Please help!

How do you prove is 4 out of 5 numbers are sequential???

Thanks
<?php
if($ttt=='AVAILABLE'){
if(($D1==$D2 && $D1==$D3)
||($D1==$D2 && $D1==$D4)
||($D1==$D2 && $D1==$D5)
||($D1==$D3 && $D1==$D4)
||($D1==$D3 && $D1==$D5)
||($D1==$D4 && $D1==$D5)
||($D2==$D3 && $D2==$D4)
||($D2==$D3 && $D2==$D5)
||($D2==$D4 && $D2==$D5)
||($D3==$D4 && $D3==$D5))
{$ttttotal=$D1+$D2+$D3+$D4+$D5;
echo $ttttotal;}
else{echo 0;}
}
else {echo $ttt;}
?>


#3 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 17 April 2006 - 09:24 PM

Thanks for all the help...

Mark this one solved...

<?php
if($ss=='AVAILABLE'){
$s=array($D1,$D2,$D3,$D4,$D5);
sort($s);
if(($s[0]==$s[1]-1 && $s[0]==$s[2]-2 && $s[0]==$s[3]-3)||
($s[1]==$s[2]-2 && $s[1]==$s[3]-3 && $s[1]==$s[4]-4))
{$sstotal=35;
echo $sstotal;}
}
else
{echo $ss;}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users