飞镖游戏计算

3

我找不到任何有关飞镖的脚本或代码,所以我一直在编写自己的代码。如果有人知道有这些内容的网站,请告诉我(我已经用多种方式搜索了但没有找到)。

我正在尝试制作一个飞镖游戏(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;
            }
        }
    }
}

感谢您的任何帮助。

更新的代码现在遍历了所有值,但没有检查第二个飞镖上的值是否可行。<table border="1"> <?php for ($x=60; $x>0; $x--) { for ($xx=60; $xx>0; $xx--) { for ($xxx=50; $xxx>0; $xxx = $xxx - 2) { if ($xxx == 48) { $xxx = 40; } echo '<tr> <td>' . $x . '</td> <td>' . $xx . '</td> <td>' . $xxx . '</td> <td>' . ($xxx + $xx + $x) . '</td> </tr>'; } } }
?> </table>
- user1419338
3个回答

0

这是我几年前的账户。如果有人偶然发现了这个帖子,我最终完成了这个代码并分享给大家。

  1. 根据 @bestprogrammerintheworld 的建议更新了变量名。
  2. 允许用户输入他们喜欢的结束镖。
  3. 删除重复项(例如“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.';
}
?>

0
我会尝试做这样的事情:(你也必须添加三元组,但这是我开始的方式...)你必须有足够的内存来使用数组...它会占用很多内存。我尝试了170个,php被耗尽了...内存限制。你应该注意这一点。如果你的服务器允许,这并不是真正的问题。
<?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

这很接近,尽管它不是按顺序排列的,会出现重复。

$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";
                            }
                        }
                    }
                }
            }
        }
    }

得分50的输出将是:48、0、1 双-45、3、1 双-45、1、2 双-42、6、1 双。你说它不是按顺序排列的 - 你希望输出如何呈现?给出前5个或更多的例子。 - bestprogrammerintheworld
2
一个小建议:你真的应该使用有意义的变量名,而不仅仅是在其后附加一个字母($x、$xx、$xxx、$xxxx根本没有意义,而且很难阅读)。 - bestprogrammerintheworld

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接