我目前正在尝试制作一个扑克算法,该算法可以创建2张牌的所有可能的扑克组合。我知道起始手有1326种组合。所以我像这样创建了我的起始牌组:
$startingDeck = array();
for($i=1; $i <= 13; $i++)
{
for($x=0; $x <= 3; $x++)
{
array_push($startingDeck, array("Value" => $i ,"Color" => $x, "key"=> $i.$x));
}
}
我现在想循环遍历所有的牌并得到所有可能的组合例如
: value 1 color 1 and value 1 color 2
: value 1 color 1 and value 1 color 3
... etc
: value 2 color 0 and value 1 color 1
: value 2 color 0 and value 1 color 2
... etc
我想对所有可能的组合都这样做,但是如何
你已经有你的开始牌了。这个想法是生成所有的排列:
。
CARD NUMBER
1 2 3 4 5 6 7 8 9 10 11 12 13 ... 52
1 o x x x x x x x x x x x x x
2 - o x x x x x x x x x x x x
3 - - o x x x x x x x x x x x
4 - - - o x x x x x x x x x x
5 - - - - o x x x x x x x x x
6
7 ......... and so on .........
8
9
10
11
12
13
...
52
我们有52张牌(这是你在$startingDeck
中生成的):
x
表示我们想要生成的组合。注意,我们不必生成矩阵的下半部分,因为在扑克中顺序并不重要,因为你同时收到两张牌。我们也不生成坐标轴,因为你不能有相同的卡两次(除非你作弊:))。
现在的想法是使用两个循环,但让第二个循环开始取决于第一个初始$j
取决于$i
。这样我们就不会再生成例如2-1, 3-2, 3-1, ...
,因为我们已经生成了1-2, 1-3, 2-3, ...
。
让我们开始:
for ($i = 0; i < count($startingDeck); $i++) {
// Ignore e.g. 2-1, 3-2, as we already generated 1-2, 2-3, and so on...
for ($j = $i+1; $j < count($startingDeck); $j++) {
$firstCard = $startingDeck[$i];
$secondCard = $startingDeck[$j];
// print my stuff
}
}
生成顶部矩阵。$i+1
确保我们不生成对角线。