Yahtzee Php Code 3 of a Kind

时间:2015-02-26 14:58:27

标签: php

if($_SESSION['valueofdie1'] != 0 && $_SESSION['valueofdie2'] != 0 && $_SESSION['valueofdie3'] != 0 && $_SESSION['valueofdie4'] != 0 && $_SESSION['valueofdie5'] != 0)
                                            {
                                                if((($_SESSION['valueofdie1'] == $_SESSION['valueofdie2']) && ($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']||$_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5'])) 
                                                || (($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie2'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
                                                || (($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5'])))
                                                {
                                                    if($_POST['choose'] == 'choose 3oaK')
                                                    {
                                                        $_SESSION['g'] = 5;
                                                        $_SESSION['scoretkind'] = $_SESSION['valueofdie1'] + $_SESSION['valueofdie2'] + $_SESSION['valueofdie3'] + $_SESSION['valueofdie4'] + $_SESSION['valueofdie5'];
                                                        unset($_SESSION['3oaKBut']);
                                                        echo '<input type="hidden" name="choose" value="Clear" onLoad="form.submit();">';
                                                        $_POST['sub'] = 'reset';
                                                        $_POST['choose'] = '';
                                                    }

                                                    if(empty($_SESSION['g']))
                                                    {
                                                        $_SESSION['3oaKBut'] = '<input type="submit" name="choose" value="choose 3oaK">';
                                                        echo $_SESSION['3oaKBut'];
                                                    }
                                                }
                                            }
                                            if($_SESSION['g'] == 5)
                                            {
                                                echo $_SESSION['scoretkind'];
                                            }

所以这是我们的代码。我们试图检查5个模具中的3个是否相等。如果它们相等,我们会回显一个按钮,允许用户选择对他的3种进行评分,这是所有骰子的总和。一切都有效,除了在某些情况下,3种类型的按钮会在没有3种情况时回响。 Halp PLS

1 个答案:

答案 0 :(得分:1)

对不起,我没有通过实际解决您的问题来回答您的问题,但我认为您的代码很难阅读,而且您的方法会使编写所有规则变得很麻烦。

一般建议:将$_SESSION['valueofdie1']和其他骰子放入一组值中。这更容易使用。在那之后,检查每个值发生的次数应该很容易。即使你保留了你的方法,你也可以制作像$die1这样的变量,它比$_SESSION['valueofdie1']更短,更易读。

但是使用数组,你可以粗略地开始:

// Put all dice into an array.
$dice = array(
  $_SESSION['valueofdie1'],
  $_SESSION['valueofdie2'],
  etc.... );

// Count how many times each die is rolled.
$diceCount = array();
foreach($dice as $die) {
   $count = 0;
   if (isset($diceCount[$die])) {
     $count = $diceCount[$die];
   }

   $diceCount[$die] = $count + 1;
}

// Check possible results simply by looking at those counts.

// If one die value is rolled 5 times, it's Yahtzee...
if (array_search(5, $diceCount) !== false) {
  echo 'Yahtzee!';
}

if (array_search(4, $diceCount) !== false) {
  echo 'Four of a kind';
}

// Full house takes two types.
if (array_search(3, $diceCount) !== false && array_search(2, $diceCount) !== false) {
  echo 'Full house';
} 

for ($diceCount as $die => $count) {
  echo "$count times $die";
}
... etc ...

您需要扩展此列表,并考虑其他一些规则。毕竟,Yahtzee也可以算作四种类型。但是通过检查所有这些规则,您可以生成一个新的可能组合数组,您可以根据以前选择的选项进行检查。其结果决定了玩家可以选择的选项。