【问题标题】:Finding a sequence of numbers in an array在数组中查找数字序列
【发布时间】:2009-05-12 20:11:31
【问题描述】:

我正在制作 Yahtzee 游戏,我需要一种方法来计算小顺子(按顺序排列的 4 个数字序列)。所以有效的是:1,2,3,4 | 2,3,4,5 | 3,4,5,6。

我有一个包含 5 个数字的数组,我需要确定这 3 个组合中的一个是否在该数组中。

对于那些不熟悉 Yahtzee 的人,有 5 个骰子(数组中的五个数字)可以从 1 到 6。

【问题讨论】:

  • 你手头有一些状态吗?如果是这样,您可以获得更好的结果(速度)。
  • 状态是什么意思?我只是将一个包含 5 个数字的数组传递给任何函数。

标签: php


【解决方案1】:
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。希望这会有所帮助。

【讨论】:

  • 在回来修理之前不得不跑掉吃晚饭。 :)
【解决方案2】:

可能是这样的:--- 警告 --- 空气编码

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]

然后您可以测试“序列长度”是否 >= 4 以测试您的“小直”

【讨论】:

    【解决方案3】:

    更准确地说:

      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
    

    【讨论】:

      【解决方案4】:

      这里还有一个想法,使用一个通用的 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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-11
        • 2018-12-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-13
        • 2014-02-14
        • 2023-04-09
        • 2022-11-22
        相关资源
        最近更新 更多