2009-05-12 7 views
1

저는 Yahtzee 게임을 만들고 있어요. 나는 작은 직선 (순서대로 4 개의 숫자의 순서)을 계산할 방법이 필요합니다. 유효한 것 : 1,2,3,4 | 2,3,4,5 | 3,4,5,6.배열에서 숫자의 시퀀스 찾기

나는 5 개의 숫자로 된 배열을 가지고 있는데, 그 3 개의 조합 중 하나가 해당 배열에 있는지 알아야합니다.

Yahtzee에 익숙하지 않은 분들을 위해 1-6 개가 될 수있는 5 개의 주사위 (배열의 5 개 숫자)가 있습니다.

+0

손에 몇 가지 상태가 있습니까? 그렇다면 더 나은 결과 (속도)를 얻을 수 있습니다. – Milhous

+0

주 (state)는 무엇을 의미합니까? 나는 단지 5 개의 숫자 배열을 어떤 함수에 건네는 것이다. – roflwaffle

답변

5
function isStraight($hand) { 
    $straights = array(range(1, 4), range(2, 5), range(3, 6)); 
    foreach($straights as $solution) { 
     if(array_intersect($solution, $hand) == $solution) { 
      return $solution; 
     } 
    } 
    return false; 
} 

$hand = array(1,5,2,4,3); 

var_dump(isStraight($hand)); 

게임의 규칙에 대해 확실하지만 그것을 어떻게해야하지.

이 함수는 찾은 첫 번째 해결책 (이 경우 [1,2,3,4])을 반환합니다. 손에 직선이없는 경우 부울 false을 반환합니다. 희망이 도움이됩니다.

+0

나를 두들겨 +1 – da5id

+0

안녕하세요, 당신의 대답은 돌아 왔습니다 ... 배열 교차 ... 오 그래 ... – cgp

+0

고치기 위해 돌아 오기 전에 도망 가서 저녁 식사를해야했습니다. :) –

1

아마 이런 식으로 뭔가 : --- 경고 --- 공기 코딩

function findSequenceLength(array $array) 
{ 
    // Filter duplicates out - and sort the array. 
    $sorted = array_unique($array, SORT_NUMERIC); 

    $lastValue = null; 
    $thisSeq = 0; 
    $longestSeq = 0; 
    foreach ($sorted as $value) 
    { 
    if (($lastValue !== null) && $value == $lastValue + 1) 
    { 
     // our value is exactly one above the last entry 
     // increase the counter 
     $thisSeq++; 
    } else { 
     // sequence ended - save the value 
     $longestSeq = max($longestSeq, $thisSeq); 
     $thisSeq = 1; 
    } 
    $lastValue = $value; 
    } 
    return max($longestSeq, $thisSeq); 
} 

$sequence = array(1,2,4,5,4,6); 
echo findSequenceLength($sequence); // should return 3 [4,5,6] 

그런 다음 "시퀀스 길이가"당신의 "작은 직선"

0
에 대한 테스트> = 4하는지 테스트 할 수 더 정확하게

:

function array_match($array, $target_array) { 
    $offset = 0; 
    $maxoffset = sizeof($target_array) - sizeof($array); 
    for($y=0;$y<$maxoffset;$y++) { 
    $result = true; 
    for($x=0;$x<sizeof($array);$x++) { 
     if ($array[$x] != $target_array[$x+$y]) { 
     $result = false; 
     continue; 
     } 
    } 
    if ($result) 
     return "true"; 
    } 
    return "false"; 
} 
echo array_match(array(1,2,3), array(1,2,3,4,5)); //true 
echo array_match(array(1,2,3), array(4,1,2,3,5)); //true 
echo array_match(array(1,2,3), array(4,2,2,1,2)); //false 
0

가 여기에 또 다른 생각이있어, 일반 isStraight() 메소드를 사용하여 두 개의 배열 조각을 테스트하는 데 사용합니다.

$tests = array(
      array(1,2,3,4,5), 
      array(1,1,2,3,4), 
      array(4,3,2,1,4), 
      array(3,4,5,6,1) 
     ); 
foreach($tests as $test) { 
    print_r($test); 
    echo "Straight: "; 
    var_dump(isStraight($test)); 
    echo "Short Straight: "; 
    var_dump(isShortStraight($test)); 
    echo "\n"; 
} 

function isShortStraight($hand) { 
    return isStraight(array_slice($hand, 0, 4)) || 
      isStraight(array_slice($hand, 1, 4)); 
} 

function isStraight($hand) { 
    $unique = array_unique($hand); 
    if (count($hand) != count($unique)) { 
     /* Has Duplicates, not a straight */ 
     return false; 
    } 

    sort($unique); 
    if ($unique != $hand) { 
     /* Sort order changed, not a straight */ 
     return false; 
    } 
    return true; 
} 
관련 문제