我试图向玩家交出五张牌并对其进行评分.我的得分程序似乎工作正常,但我遇到了不时被处理的重复卡问题.我尝试使用while循环来检查重复的卡片,但这看起来有点像hackish.我的代码如下.请记住,我绝对是新手,所以解决方案越简单越好!非常感谢.
// create suits array
$suits = array("996", "997", "998", "999");
// create faces array
$faces = array();
$faces[1] = "1";
$faces[2] = "2";
$faces[3] = "3";
$faces[4] = "4";
$faces[5] = "5";
$faces[6] = "6";
$faces[7] = "7";
$faces[8] = "8";
$faces[9] = "9";
$faces[10] = "10";
$faces[11] = "11";
$faces[12] = "12";
$faces[13] = "13";
// create player's hand
$card = array();
for ($i = 0; $i < 5; $i++)
{
$face_value = shuffle($faces);
$suit_value = shuffle($suits);
$card[$i] = $faces[$face_value].$suits[$suit_value];
$counter = 0;
while ($counter < 100)
{
if (in_array($card[$i], $card))
{
$face_value = shuffle($faces);
$suit_value = shuffle($suits);
$card[$i] = $faces[$face_value].$suits[$suit_value];
}
$counter++;
}
print ("<img src=\"../images/4/$card[$i].gif\">");
}
Run Code Online (Sandbox Code Playgroud)
简单地设置一个包含52个元素的数组可能更有效,每个卡一个.
$cards = range(0,51);
shuffle($cards);
$hand = array();
for ($i = 0; $i < 5; $i++)
{
$hand[$i] = $cards[$i];
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可以$i通过执行操作简单地提取卡片的套装和等级
$suit = $hand[$i] % 4;
$rank = $hand[$i] / 4;
Run Code Online (Sandbox Code Playgroud)
这样可以防止重复.
编辑:西装和等级被逆转.他们现在应该是正确的.