Jump to content

[SOLVED] Help with function


Adam1239

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/64602-solved-help-with-function/
Share on other sites

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.

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.

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 :)

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  :P

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);
}
?>

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?

<?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' ;
?>

<?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.

Archived

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

×
×
  • Create New...

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.