Adam1239 Posted August 13, 2007 Share Posted August 13, 2007 Hi, i have a function that checks a users hand to see if its a Short straight or not. The code is: function Is_ShortStraight($test) { for ( $i = 1; $i < 7; $i++ ) { $for[$i] = substr_count($test, $i); } if($for[1] == "1" && $for[2] == "1" && $for[3] == "1" && $for[4] == "1" OR $for[2] == "1" && $for[3] == "1" && $for[4] == "1" && $for[5] == "1" OR $for[3] == "1" && $for[4] == "1" && $for[5] == "1" && $for[6] == "1"){ return true; }else{ return false; } } This was working ok untill i had a short straight like this 3-4-5-6-3 and this function returned false, because the bit of the function $for[3] == "1" && $for[4] == "1" is basically saying if there is one 3 and one 4 in the hand when it is not one 3 because that last number could be anything. Do you know a way round this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/ Share on other sites More sharing options...
ballhogjoni Posted August 13, 2007 Share Posted August 13, 2007 I am a novice compared to what that code is. I can't even understand it, but does it have something to do with the OR instead of || in the if () statement? Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322099 Share on other sites More sharing options...
keeB Posted August 13, 2007 Share Posted August 13, 2007 http://php.net/sort This is a fun excersize, so I am going to code up an example Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322102 Share on other sites More sharing options...
cunoodle2 Posted August 13, 2007 Share Posted August 13, 2007 A few questions for you... 1. What format does the variable $test come in? Is it a series of numbers separated by spaces? Just a long string? 2. What purpose does comparing the individual items of the array to the number "1" do? Get back to me when you have time and I'll take a further look into this. Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322113 Share on other sites More sharing options...
Adam1239 Posted August 13, 2007 Author Share Posted August 13, 2007 A few questions for you... 1. What format does the variable $test come in? Is it a series of numbers separated by spaces? Just a long string? 2. What purpose does comparing the individual items of the array to the number "1" do? Get back to me when you have time and I'll take a further look into this. Hi, thanks for your intrest, the test comes in 5 numbers separated by - like this: $test = "6-2-3-4-5" if( Is_ShortStraight($test) ) { echo "$test is a short straight"; }else{ echo "$test is not a short staight"; } Also for the second question the $for[$i] = substr_count($test, $i); } part counts how meny times the number $i as come up in the $test string. so you could put it like this: if the said number comes up once. Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322118 Share on other sites More sharing options...
keeB Posted August 13, 2007 Share Posted August 13, 2007 Prereq = $seqCount == number of elements in array <?php # Find a method of discovering if any part of the string is sequential between high, low. # Ex: 1 4 3 2 5 - returns true # Ex: 1 4 6 4 3 - returns false, no 5 $string = "1 4 3 2 2"; $string2 = "1 4 3 2 5"; print checkString($string, 5); #false print checkString($string2, 5); #true function checkString($string, $seqCount) { $string = explode(" ", $string); # print_r($string); for($number = 0; $number <= $seqCount; $number++) { if( in_array($string[$number] + 1, $string) && (in_array($string[$number] - 1, $string) || $string != 0)) { foreach($string as $num) { $flag = 1; if($num == $string[$number]) $flag++; } if ($flag < 2) $count++; } } print "matched $count <br>"; if($count >= $seqCount) return true; return false; } #print sort($string); Seems to work for me Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322127 Share on other sites More sharing options...
Adam1239 Posted August 13, 2007 Author Share Posted August 13, 2007 Prereq = $seqCount == number of elements in array <?php # Find a method of discovering if any part of the string is sequential between high, low. # Ex: 1 4 3 2 5 - returns true # Ex: 1 4 6 4 3 - returns false, no 5 $string = "1 4 3 2 2"; $string2 = "1 4 3 2 5"; print checkString($string, 5); #false print checkString($string2, 5); #true function checkString($string, $seqCount) { $string = explode(" ", $string); # print_r($string); for($number = 0; $number <= $seqCount; $number++) { if( in_array($string[$number] + 1, $string) && (in_array($string[$number] - 1, $string) || $string != 0)) { foreach($string as $num) { $flag = 1; if($num == $string[$number]) $flag++; } if ($flag < 2) $count++; } } print "matched $count <br>"; if($count >= $seqCount) return true; return false; } #print sort($string); Seems to work for me If you try your code with the string as $string = "4 2 3 6 5"; to match 5 seen as this is for a dice game, and there are 6 dice sides, it will only match 3. But very good example to what i came up with Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322137 Share on other sites More sharing options...
keeB Posted August 13, 2007 Share Posted August 13, 2007 You're right.. I'll think on it. This is actually a tough problem Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322143 Share on other sites More sharing options...
Adam1239 Posted August 13, 2007 Author Share Posted August 13, 2007 Yea, the numbers can be like from 1 to 4 or 2 to 5 or 3 to 6 any where in the string Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322523 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 try <?php $straight = array (1,2,3,4,5,6); $hand = "3-4-5-2-3"; $ha = explode('-', $hand); sort ($ha); for ($i=0; $i<3; $i++) { $tmp = array_slice($straight,$i,4); if ($tmp == array_intersect($tmp, $ha)) echo 'Found: ', join('-', $tmp); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322609 Share on other sites More sharing options...
Adam1239 Posted August 13, 2007 Author Share Posted August 13, 2007 try <?php $straight = array (1,2,3,4,5,6); $hand = "3-4-5-2-3"; $ha = explode('-', $hand); sort ($ha); for ($i=0; $i<3; $i++) { $tmp = array_slice($straight,$i,4); if ($tmp == array_intersect($tmp, $ha)) echo 'Found: ', join('-', $tmp); } ?> Hi thanks for your help, the code works, just i need this to be in a function which returns true if it as a match or false if it as not got a match. so if i use the if($tmp == array_intersect($tmp, $ha)) return true; else return false; would it work? Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322761 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 <?php function shortStraight($hand) { $straight = array (1,2,3,4,5,6); $ha = explode('-', $hand); sort ($ha); for ($i=0; $i<3; $i++) { $tmp = array_slice($straight,$i,4); if ($tmp == array_intersect($tmp, $ha)) return true; } return false; } $hand = "2-4-6-2-3"; echo shortStraight($hand) ? 'Yes' : 'No' ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322768 Share on other sites More sharing options...
Adam1239 Posted August 13, 2007 Author Share Posted August 13, 2007 <?php function shortStraight($hand) { $straight = array (1,2,3,4,5,6); $ha = explode('-', $hand); sort ($ha); for ($i=0; $i<3; $i++) { $tmp = array_slice($straight,$i,4); if ($tmp == array_intersect($tmp, $ha)) return true; } return false; } $hand = "2-4-6-2-3"; echo shortStraight($hand) ? 'Yes' : 'No' ; ?> Thank you much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/64602-solved-help-with-function/#findComment-322779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.