发牌相关代码
1. 发手牌代码
// 发牌逻辑,确保返回的是花色+数值的组合
$deck = [];
$suits = ['C', 'D', 'H', 'S'];
$values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
// 创建牌组,根据游戏类型决定是否包含2、3、4、5
foreach ($suits as $suit) {
foreach ($values as $value) {
// 短牌游戏去掉2、3、4、5
if ($game_type === '短牌' && in_array($value, ['2', '3', '4', '5'])) {
continue;
}
$deck[] = $value . $suit; // 例如: "10H"代表红桃10
}
}
shuffle($deck);
// 给每个玩家发2张牌
// 初始化playerCards数组,确保它是空的
$playerCards = [];
// 给坐下的玩家发牌
foreach ($qualified_players as $player) {
$hand = [array_pop($deck), array_pop($deck)];
$playerCards[$player['current_seat']] = $hand;
}
2. 发公共牌代码
// 创建牌组,根据游戏类型决定是否包含2、3、4、5
$deck = [];
$suits = ['C', 'D', 'H', 'S'];
$values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
// 获取游戏类型
$game_type_stmt = $conn->prepare("SELECT game_type FROM table_attributes WHERE id = ?");
$game_type_stmt->bind_param("i", $table_id);
$game_type_stmt->execute();
$game_type_result = $game_type_stmt->get_result()->fetch_assoc();
$game_type = $game_type_result['game_type'] ?? '长牌';
// 创建牌组,根据游戏类型决定是否包含2、3、4、5
foreach ($suits as $suit) {
foreach ($values as $value) {
// 短牌游戏去掉2、3、4、5
if ($game_type === '短牌' && in_array($value, ['2', '3', '4', '5'])) {
continue;
}
$deck[] = $value . $suit; // 例如: "10H"代表红桃10
}
}
// 从数据库中获取已发给玩家的牌和已发出的公共牌,从牌组中移除
$existing_cards = [];
// 收集所有已发出的牌(玩家手牌和公共牌)
for ($i = 1; $i <= 10; $i++) {
if (isset($seats_data["seat{$i}"]) && !empty($seats_data["seat{$i}"])) {
$seat_data = json_decode($seats_data["seat{$i}"], true);
if ($seat_data) {
// 移除玩家手牌
if (isset($seat_data['cards']) && is_array($seat_data['cards'])) {
foreach ($seat_data['cards'] as $card) {
$existing_cards[] = $card;
}
}
// 移除已发出的公共牌
if (isset($seat_data['flop_cards']) && is_array($seat_data['flop_cards'])) {
foreach ($seat_data['flop_cards'] as $card) {
$existing_cards[] = $card;
}
}
if (isset($seat_data['turn_card'])) {
$existing_cards[] = $seat_data['turn_card'];
}
if (isset($seat_data['river_card'])) {
$existing_cards[] = $seat_data['river_card'];
}
}
}
}
// 从牌组中移除已发出的牌
foreach ($existing_cards as $card) {
$key = array_search($card, $deck);
if ($key !== false) {
unset($deck[$key]);
}
}
// 重新索引数组
$deck = array_values($deck);
// 随机打乱剩余的牌
shuffle($deck);
3. 发牌逻辑代码
// 根据需要发出相应数量的牌
if ($active_players_count < 2) {
// 有手牌没有allin以及有剩余积分的玩家数量小于2,发出所有剩余的公共牌(确保总共5张)
$needed_cards = 5 - $community_cards_count;
while (count($deck) > 0 && count($cards_to_deal) < $needed_cards) {
$cards_to_deal[] = array_pop($deck);
}
// 设置游戏状态为showdown(摊牌)
$new_game_state = 'showdown';
} else if ($community_cards_count == 5) {
// 公共牌已经是5张,直接设置游戏状态为showdown,不需要发牌
$new_game_state = 'showdown';
} else if ($community_cards_count == 0) {
// 发出翻牌(3张)
$cards_to_deal = [array_pop($deck), array_pop($deck), array_pop($deck)];
// 设置游戏状态为flop(翻牌)
$new_game_state = 'flop';
} else if ($community_cards_count == 3) {
// 发出转牌(1张)
$cards_to_deal = [array_pop($deck)];
// 设置游戏状态为turn(转牌)
$new_game_state = 'turn';
} else if ($community_cards_count == 4) {
// 发出河牌(1张)
$cards_to_deal = [array_pop($deck)];
// 设置游戏状态为river(河牌)
$new_game_state = 'river';
}