飞镖游戏计算

时间:2013-03-21 13:12:26

标签: php permutation

我无法找到飞镖的任何脚本或代码,所以如果有人知道有这些的网站,我一直在写我自己的信息请告诉我(我已经搜索过多种方式而没有运气)

我正在尝试制作一个飞镖游戏(301/501)的脚本,它将计算拍摄什么来完成游戏。当你用飞镖(3点,2点或1点)达到精确分数并且最后一个飞镖是双倍时,游戏结束(例如,得分为170,可能达到2倍三倍20(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;
            }
        }
    }
}

感谢您的帮助。

3 个答案:

答案 0 :(得分: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";
                            }
                        }
                    }
                }
            }
        }
    }

答案 1 :(得分: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

答案 2 :(得分:0)

几年前这是我的帐户。如果有人偶然发现了这一点,我最终完成了这个代码,这里就是。

  1. 根据@bestprogrammerintheworld建议更新变量名称
  2. 允许用户输入他们首选的完成飞镖。
  3. 删除重复项(例如&#39; 45,30,双2&#39;&#39; 30,45,双2&#39;只会显示一次)。
  4. (编辑不会在没有任何内容的情况下从代码中删除列表)。

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