Jump to content

Archived

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

AV1611

Learning Exercise

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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:
[code]
<?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];
?>
[/code]
Someone Please help!

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

Thanks
[code]
<?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;}
?>
[/code]

Share this post


Link to post
Share on other sites
Thanks for all the help...

Mark this one solved...

[code]
<?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;}
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.