2013-03-21 149 views
3

我一直沒有找到飛鏢的任何腳本或代碼,所以我一直在寫我自己的,如果有人知道一個網站有這些請讓我知道(我搜索了多種方式沒有運氣)。飛鏢遊戲計算

我正在嘗試爲飛鏢遊戲(301/501)製作一個腳本,它將計算拍攝完成遊戲的內容。比賽結束時,你用飛鏢(3,2或1)打出精確的得分,最後一支飛鏢是雙倍的(例如,得分是170,可以打2個三重20s(2 * 60),並且雙重靶球(2 * 25)

我開始編寫一個腳本,循環遍歷這些值並拉出了第一個加起來的值,雖然我寫了這個錯誤/效率很低,但是因爲1.我循環了三次(即使只有1或2個飛鏢可能是必要的),和2。我只輸出的第一組值在拍攝,並不是所有的值

Example: 
Score 50 
1. double bull (25) 
2. 20 + double 15 
3. 20 + 20 + double 5 
4. 16 + 8 + double 13 
5. double 12 + double 12 + double 1 
6. triple 15 + 1 + double 2 
7. etc. 

這裏是我開始:

$score = $_GET['score']; 
if ($score > 170) { 
    die('No Outs'); 
} 
$possdarts = array(); 
$possdartstext = array(); 
for ($x = 0; $x < 2; $x++) { 
    for ($a = 0; $a < 60; $a++) { 
     if (($score - $a) >= 0) { 
      $possdarts[] = $a; 
      if(($x == 2) && ($a % 2 == 0)) { 
       $outs[] = $a; 
      } 
     } 
    } 
} 

感謝您的幫助。

+0

更新代碼現在遍歷所有值,但不檢查第二個飛鏢上的值是否可能。

<?($ xx = 60; $ xx> 0; $ xx--){ \t \t for($ xxx = 50; $ xx--)對於($ x = 60; $ x> 0; $ x--){$ XXX> 0; $ XXX = $ XXX - 2){ \t \t \t如果($ XXX == 48){ \t \t \t \t $ XXX = 40; \t \t \t} \t \t \t回聲 ' \t \t \t \t \t​​'。 $ x。 ' \t \t \t \t \t​​'。 $ xx。 ' \t \t \t \t \t​​'。 $ xxx。 ' \t \t \t \t \t​​'。 ($ xxx + $ xx + $ x)。 ' \t \t \t \t'; \t \t} \t} } \t \t \t >
' – user1419338 2013-03-25 20:38:02

回答

0

這是非常接近,雖然它不是在順序和重複會發生。

$score = $_GET['score']; 
if ($score >170 || $score <=1) { 
     $message = 'No outs possible'; 
    } else { 
     for ($x=60; $x >= 0; $x--) { 
      if (
       $x <= 20 || 
       $x == 50 || 
       $x == 25 || 
       ($x % 3 == 0) || 
       ($x <= 40 && 
       ($x % 2 == 0) 
       ) 
       ) { 
       /* 
       if it is less than 20 or equal to, because 20 - 1 on board, 
       if it is less than or equal to 40 and divisible by 2 because it could be a double 
       if it is divisible by 3 because it could be a triple 
       if it is a double or single bull, no point in checking the double for 60-51 
       */ 
       for ($xx=60; $xx >= 0; $xx--) { 
        if (
          $x <= 20 || 
          $x == 50 || 
          $x == 25 || 
          ($x % 3 == 0) || 
          ($x <= 40 && 
          ($x % 2 == 0) 
          ) 
         ) { 
         for ($xxx=50; $xxx > 0; $xxx = $xxx - 2) { 
          if ($xxx == 48) { 
           $xxx = 40; 
          } 
          $xxxdis = ($xxx/2) . ' Double'; 
          if (($xxx + $xx + $x) == $score) { 
           echo $x . ', ' . $xx . ', ' . $xxxdis . "\n"; 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
+0

從刻痕50的輸出將給出:48,0,1雙 - 45,3,1雙 - 45,1,2雙 - 42 6,1雙。你說這不是爲了 - 你想如何輸出?給出前5個左右的例子。 – bestprogrammerintheworld 2013-04-08 17:18:24

+2

只是一個提示:你真的應該使用變量名,這意味着什麼,而不僅僅是附加一個字母($ x,$ xx,$ xxx,$ xxxx表示absolulety沒有,並且很難讀) – bestprogrammerintheworld 2013-04-08 17:20:45

0

我會嘗試做這樣的事情:(你必須用三倍還補充,但這是我會怎麼開始......)你必須有可用的使用數組的內存...它吃了很多。我嘗試使用170,並且php耗盡了......內存限制。你應該是AWARE那。如果你的服務器允許的話,這不是一個真正的問題。

<?php 
$score = 50; 

if ($score >170 || $score <=1) { 
     $message = 'No outs possible'; 
    } else { 

     $outs = array(); 

     //Create an array with values from 0 to {value of $score} 
     for($v=0;$v<$score;$v++) { 
      $outs[] = $v; 
     } 

     // Get combinations of values (like 0 0 1, 0 0 2, 0 0 3 etc with space between values) 
     $combinations = getCombinations($outs,3); 

     //Create an array when combinations are equal to set score 

     $ascArr = array();  
     $possibleOuts = array(); 
     foreach($combinations as $key=>$c) { 
      $tmpArr = explode(' ', $c);    //Create array of each combination (value1, value2, value3) 

      //Value3 has to be a double 
      $tmpArr[2] *= 2;    

      //Get ascii-value of whole string (without spaces) 
      $tc = trim($c); 
      $ascValue = getAsciiValueOfString($tc); 

      //If sum of array-values is the same as score, add it to outs array 
      //Also make sure that ascii-value of string occurence cannot be repeated ('0 1 49' or '0 49 1' is actually same) 
      if (array_sum($tmpArr) == $score && !in_array($ascValue, $ascArr)) {   
       $possibleOuts[] = $c . ' double'; 
       $ascArr[] = $ascValue; 
      } 

     } 

     //Could some more part of outs array (besides last) be a double? 
     $doubleOuts = array(); 
     $do = array(); 

     foreach($possibleOuts as $value) { 
      $tmpArr = explode(' ', $value); 
      $do[0] = $tmpArr[0]; 
      $do[1] = $tmpArr[1]; 
      $do[2] = $tmpArr[2]; 
      $do[3] = $tmpArr[3]; 

      //Check first value. If first value can be divided into 2 and the second value as well 
      //then set double on first value, and reduce second value 
      if ($tmpArr[0] % 2 === 0 && $tmpArr[0]>0 && $tmpArr[1] % 2 === 0) { 
       $do[0] = $tmpArr[0] . ' double'; 
       $do[1] = $tmpArr[1] - $tmpArr[0]; 
      } 

      $doubleOuts[] = implode(' ', $do); 

     } 


     //Merge outs and doubleouts-array 
     $posOuts = array_merge($possibleOuts, $doubleOuts); 


    // Print it out 
    foreach($posOuts as $value) { 
     echo $value . ' <br />'; 
    } 



} 



//Function for getting value of chars 
function getAsciiValueOfString($string) 
{ 
    $ascii = 0; 

    for ($i = 0; $i < strlen($string); $i++) 
    { 
     $ascii += ord($string[$i]); 
    } 

    return($ascii); 
} 

//Recursive functions for getting differet combinations 
function getCombinations($arr,$n) 
{ 
    $res = array(); 

    foreach ($arr as $w) 
    { 
      if ($n==1) $res[] = $w; 
      else 
      { 
       $perms = getCombinations($arr,$n-1); 

       foreach ($perms as $p) 
       { 
         $res[] = $w." ".$p; 
       } 
      } 
    } 


    return $res; 
} 


?> 

輸出:

0 0 25 double 
0 2 24 double 
0 4 23 double 
0 6 22 double 
0 8 21 double 
0 10 20 double 
0 12 19 double 
0 14 18 double 
0 16 17 double 
0 18 16 double 
0 20 15 double 
0 22 14 double 
0 24 13 double 
0 26 12 double 
0 28 11 double 
0 30 10 double 
0 32 9 double 
0 34 8 double 
0 36 7 double 
0 38 6 double 
0 46 2 double 
0 48 1 double 
1 19 15 double 
1 39 5 double 
3 9 19 double 
3 19 14 double 
3 29 9 double 
3 39 4 double 
5 19 13 double 
5 29 8 double 
5 39 3 double 
7 19 12 double 
7 29 7 double 
7 39 2 double 
9 19 11 double 
9 29 6 double 
10 10 15 double 
10 12 14 double 
10 14 13 double 
10 16 12 double 
10 18 11 double 
10 20 10 double 
10 40 0 double 
11 19 10 double 
13 19 9 double 
15 19 8 double 
17 19 7 double 
19 19 6 double 
19 29 1 double 
0 0 25 double 
0 2 24 double 
0 4 23 double 
0 6 22 double 
0 8 21 double 
0 10 20 double 
0 12 19 double 
0 14 18 double 
0 16 17 double 
0 18 16 double 
0 20 15 double 
0 22 14 double 
0 24 13 double 
0 26 12 double 
0 28 11 double 
0 30 10 double 
0 32 9 double 
0 34 8 double 
0 36 7 double 
0 38 6 double 
0 46 2 double 
0 48 1 double 
1 19 15 double 
1 39 5 double 
3 9 19 double 
3 19 14 double 
3 29 9 double 
3 39 4 double 
5 19 13 double 
5 29 8 double 
5 39 3 double 
7 19 12 double 
7 29 7 double 
7 39 2 double 
9 19 11 double 
9 29 6 double 
10 double 0 15 double 
10 double 2 14 double 
10 double 4 13 double 
10 double 6 12 double 
10 double 8 11 double 
10 double 10 10 double 
10 double 30 0 double 
11 19 10 double 
13 19 9 double 
15 19 8 double 
17 19 7 double 
19 19 6 double 
19 29 1 double 
0

這是我的帳戶在幾年前。如果有人絆倒了這一點,我最終完成了這個代碼,在這裏。每@bestprogrammerintheworld建議

    1. 更新變量名允許用戶把他們的首選整理鏢。
    2. 刪除重複項(例如'45,30,double 2'和'30,45,double 2'只會顯示一次)。

    (編輯器不會在這裏沒有東西的情況下破壞代碼列表)。

    <?php 
    $score =$_GET['score']; 
    $finaldart = $_GET['out'] * 2; 
    if ($score >170 || $score <=1) { 
        die('No outs possible'); 
    } else { 
        for ($firstdart=60; $firstdart >= 0; $firstdart--) { 
         if (
          $firstdart <= 20 || 
          $firstdart == 50 || 
          $firstdart == 25 || 
          ($firstdart % 3 == 0) || 
          ($firstdart <= 40 && $firstdart % 2 == 0) 
         ) { 
          /* 
          if it is less than 20 or equal to, because 20 - 1 on board, 
          if it is less than or equal to 40 and divisible by 2 because it could be a double 
          if it is divisible by 3 because it could be a triple 
          if it is a double or single bull, no point in checking the double for 60-51 
          */ 
          for ($seconddart=60; $seconddart >= 0; $seconddart--) { 
           if (
            $seconddart <= 20 || 
            $seconddart == 50 || 
            $seconddart == 25 || 
            ($seconddart % 3 == 0) || 
            ($seconddart <= 40 && $seconddart % 2 == 0) 
            ) { 
            for ($thirddart=50; $thirddart > 0; $thirddart = $thirddart - 2) { 
             if ($thirddart == 48) { 
              $thirddart = 40; 
             } 
             $outstring = 'Double ' . ($thirddart/2); 
             if (($thirddart + $seconddart + $firstdart) == $score && (@!in_array($seconddart . ', ' . $firstdart . ', ' . $outstring . "<br />\n", $pouts) && @!in_array($seconddart . ', ' . $firstdart . ', ' . $outstring . "<br />\n", $everyotherout))) { 
              if ($thirddart == $finaldart) { 
               $pouts[] = $firstdart . ', ' . $seconddart . ', ' . $outstring . "<br />\n"; 
              } else { 
               $everyotherout[] = $firstdart . ', ' . $seconddart . ', ' . $outstring . "<br />\n"; 
              } 
             } 
            } 
           } 
          } 
         } 
        } 
    } 
    if(!empty($finaldart)) { 
        if(!empty($pouts)){ 
         foreach($pouts as $out) { 
          echo $out; 
         } 
         echo "<br />\n" . 'Every Other Out' . "<br /><br />\n"; 
        } else { 
         echo 'No preferred outs avaliable.' . "<br />\n"; 
        } 
    } 
    if(!empty($everyotherout)){ 
        foreach($everyotherout as $out) { 
         echo $out; 
        } 
    } else if(empty($pouts)){ 
        echo 'No outs avaliable.'; 
    } 
    ?>