我正在为扑克手牌(向“球员”发牌)。 我想在我的codeigniter中模拟卡片处理功能。但是我被卡住了,因为所有玩家都得到相同的卡片对。
这是我的模型功能:
$deck = array(
'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD',
'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC',
'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH',
'AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS'
);
shuffle($deck);
$player_card = array();
$cycle = 2;
for ($k=0; $k < $players; $k++) {
for ($i=0; $i < $cycle; $i++) {
$player_card[$k][$i] = $deck[$i];
}
}
我正在获得这样的输出:
这是混乱的甲板:
Array
(
[0] => JC
[1] => AC
[2] => 4H
[3] => 5D
[4] => 3D
[5] => QD
[6] => 7H
[7] => 8S
[8] => AD
[9] => KD
[10] => 6C
[11] => KH
[12] => TS
[13] => 8D
[14] => 7S
[15] => 9C
[16] => 6D
[17] => 6S
[18] => 4S
[19] => KC
[20] => 2H
[21] => 9H
[22] => 8H
[23] => 2C
[24] => AS
[25] => 7C
[26] => 3C
[27] => 2D
[28] => QS
[29] => QC
[30] => JS
[31] => JH
[32] => 9D
[33] => TC
[34] => 3S
[35] => 4C
[36] => 2S
[37] => 3H
[38] => JD
[39] => 5H
[40] => 6H
[41] => AH
[42] => TH
[43] => TD
[44] => KS
[45] => 9S
[46] => 8C
[47] => 5S
[48] => 7D
[49] => 4D
[50] => 5C
[51] => QH
)
这是&#39; n&#39;玩家卡(n = 3)
Array
(
[0] => Array
(
[0] => JC
[1] => AC
)
[1] => Array
(
[0] => JC
[1] => AC
)
[2] => Array
(
[0] => JC
[1] => AC
)
)
我想要的东西Put是: 我希望为每位玩家制作回合制的牌。
Array
(
[0] => Array
(
[0] => JC
[1] => 5D
)
[1] => Array
(
[0] => AC
[1] => 3D
)
[2] => Array
(
[0] => 4H
[1] => QD
)
)
答案 0 :(得分:3)
你已经洗牌了,为什么不简单array_chunk
?
shuffle($cards);
$player_card = array_chunk($cards, 13);
http://php.net/manual/tr/function.array-chunk.php
这将输出如下内容:
Array
(
[0] => Array
(
[0] => 3D
[1] => 7S
[2] => AD
[3] => 9C
[4] => 5D
[5] => KS
[6] => 4H
[7] => 6S
[8] => AS
[9] => TH
[10] => 4S
[11] => TS
[12] => 3H
)
[1] => Array
(
[0] => AH
[1] => 5S
[2] => AC
[3] => QS
[4] => 5H
[5] => 7D
[6] => JH
[7] => QD
[8] => JS
[9] => 9S
[10] => 8S
[11] => 6C
[12] => 2H
)
[2] => Array
(
[0] => TD
[1] => 8H
[2] => 5C
[3] => KD
[4] => 8D
[5] => QH
[6] => JC
[7] => TC
[8] => 3C
[9] => 6D
[10] => 9H
[11] => 2C
[12] => 2D
)
[3] => Array
(
[0] => 7C
[1] => KH
[2] => 4D
[3] => 9D
[4] => 6H
[5] => 7H
[6] => KC
[7] => QC
[8] => JD
[9] => 2S
[10] => 8C
[11] => 4C
[12] => 3S
)
)
编辑:如果您想逐个向玩家发牌;
$player_card = [[],[],[],[]]; // four players
for($i = 0; i < count($deck); $i++){
$player_card[$i % 4][] = $deck[$i];
}
答案 1 :(得分:1)
这种情况正在发生,因为确定要处理哪张牌的逻辑是在内圈中,而玩家圈是外圈。因此,对于外循环的每次迭代,你都会向每个玩家发放相同的牌。
最好废弃你最初的洗牌,而不是每次都随机发一张卡片,然后从主阵列中取出那张卡片。
$num_cards_to_deal = 2;
for ($k=0; $k < $players; $k++)
for ($i=0; $i < $num_cards_to_deal; $i++) {
$card_index = rand(0, count($deck) - 1);
$player_card[$k][$i] = array_values($deck)[$card_index];
unset($deck[$card_index]);
}
但是,这会修改原始$deck
,因此您可能需要先复制一份,以便在当前一轮交易中使用。
$this_round_deck = $deck;
//...then replace $deck with $this_round_deck in the above code
答案 2 :(得分:0)
问题在于你是向玩家发放卡片,但从不从卡片中取出卡片。
如果你想让它像普通卡片一样工作,你应该总是取出最顶级的卡片。最简单的方法是使用array_shift($deck)
。它将从阵列返回第一张卡并将其删除。 See details.
为了使它干净,我会创建一个函数draw
function draw(){
global $deck;
return array_shift($deck);
}
你也可以创建一个甲板类,它会更好,因为你不需要玩全局变量,但那是另一个帖子......
另外,如果你害怕表演(没有理由除非你正在玩几千张卡牌),你可以使用array_pop()代替。它做同样的事情,但不是拿第一张卡,取最后一张牌。不会改变太多,但记忆明智,它在PHP上会更容易。
答案 3 :(得分:0)
特别感谢@taha Paksu:
array_chunk()对我有用。我根据自己的需要管理它。
这是'N'玩家卡交易逻辑的完整模型。
class Card_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function shuffled_deck($type, $players)
{
$noraml_deck = $deck = array(
'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD',
'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC',
'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH',
'AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS'
);
shuffle($deck);
$player_hand = array();
if ($type === 'holdem')
{
$card_per_player = 2;
$chunk = array_chunk($deck, $players);
for ($j=0; $j < $players; $j++)
{
for ($i=0; $i < $card_per_player; $i++)
{
array_push($player_hand, $chunk[$i][$j]);
}
}
$player_hand = array_chunk($player_hand, $card_per_player);
}
return [$player_hand];
}
}
输出:SHUFFLED DECK
[0] => Array
(
[0] => 2H
[1] => 6D
[2] => 3D
[3] => 2S
[4] => 8S
[5] => KC
[6] => JC
[7] => 8H
[8] => TD
[9] => 4S
[10] => 7C
[11] => KH
[12] => 6C
[13] => AC
[14] => 2D
[15] => JH
[16] => 7D
[17] => 9H
[18] => 8C
[19] => AD
[20] => TC
[21] => 9S
[22] => TH
[23] => 4C
[24] => 6S
[25] => 5S
[26] => 5C
[27] => 6H
[28] => 4D
[29] => KS
[30] => JD
[31] => KD
[32] => TS
[33] => QH
[34] => 5H
[35] => 8D
[36] => 9C
[37] => 5D
[38] => 3H
[39] => QC
[40] => 9D
[41] => 3S
[42] => 7H
[43] => QD
[44] => 3C
[45] => QS
[46] => AS
[47] => 7S
[48] => AH
[49] => 4H
[50] => 2C
[51] => JS
)
球员卡:球员= N = 3
Array
(
[0] => Array
(
[0] => 2H
[1] => 2S
)
[1] => Array
(
[0] => 6D
[1] => 8S
)
[2] => Array
(
[0] => 3D
[1] => KC
)
)