PHP写的斗地主功能开发,可以完成基础的斗地主功能
- 初始化游戏
public function gameInit($room,$GameList){$redis = new Redis();$init = $this->LicenSing($room);if ($init && $room){$roomGame1 = json_decode($redis->get('room_'.$room.'_game1'),true);$roomGame2 = json_decode($redis->get('room_'.$room.'_game2'),true);$roomGame3 = json_decode($redis->get('room_'.$room.'_game3'),true);$roomBottom = json_decode($redis->get('room_'.$room.'_bottom'),true);$roomGameInit = $GameList[rand(0,2)];$redis->hset('room_'.$room.'_game','roomGameInit',$roomGameInit['id']);$redis->hset('room_'.$room.'_game','thisGame',json_encode([],true));return [$roomGame1,$roomGame2,$roomGame3,$roomBottom,$roomGameInit];}else{echo "游戏初始化失败";}}public function LicenSing(int $room){$design = ['♠', '♥', '♦', '♣'];$card = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K','A', '2'];$CardLibrary = [];foreach ($design as $value){foreach ($card as $k=>$v){$CardLibrary[] = ['design' => $value,'card' => $v,'weight'=>array_search($v, $card),];}}$CardLibrary [] = ['design' => '','card' => 'joke 小王','weight'=>13,];$CardLibrary [] = ['design' => '','card' => 'joke 大王','weight'=>14,];shuffle($CardLibrary);$bottom []= $CardLibrary[51];$bottom []= $CardLibrary[52];$bottom []= $CardLibrary[53];unset($CardLibrary[51], $CardLibrary[52], $CardLibrary[53]);for ($i=0;$i<3;$i++){for ($i1=0;$i1<17;$i1++){$game[($i+1)][] = $CardLibrary[$i + (3*$i1)];}}foreach ($game as $k=>$v){$game[$k]=$this->sortOut($game[$k]);}$room_1_game1 = $game[1];$room_1_game2 = $game[2];$room_1_game3 = $game[3];$room_1_bottom = $bottom;$redis = new Redis();
$redis->set('room_'.$room.'_game1', json_encode($room_1_game1,true),86400);$redis->set('room_'.$room.'_game2', json_encode($room_1_game2,true),86400);$redis->set('room_'.$room.'_game3', json_encode($room_1_game3,true),86400);$redis->set('room_'.$room.'_bottom', json_encode($room_1_bottom,true),86400);return true;}public function cryHost($game,$di,$room,$thisGameId){$redis =RedisTool::table();if ($redis->hget('room_'.$room.'_game','landlord')){return false;}$game = array_merge($game, $di);$game = $this->sortOut($game);$redis->hset('room_'.$room.'_game','landlord',$thisGameId);$redis->set('room_'.$room.'_game'.$thisGameId, json_encode($game,true),86400);return $game;}public function checkCardType($cardArr,$roomGame){if (count($cardArr) == 0 && count($roomGame) == 0) {return ['type'=>0,'value'=>[],'length'=>0];}$GameCard = array_column($roomGame, 'weight');$cardArr = array_column($cardArr, 'weight');$totalCard = count($cardArr);if ($totalCard >0){foreach ($cardArr as $k=>$v){if (!in_array($v,$GameCard)){return ['type'=>-1,'value'=>[],'length'=>0];}}}if ($totalCard == 1){if (in_array($cardArr[0],$GameCard)){return ['type'=>1,'value'=>array_sum($cardArr),'length'=>1];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($totalCard == 2) {if ($cardArr[0] == $cardArr[1]) {return ['type'=>2,'value'=>array_sum($cardArr),'length'=>2];}if ($cardArr[0] == 13 && $cardArr[1] == 14) {return ['type'=>999,'value'=>9999,'length'=>2];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($totalCard == 3) {if($cardArr[0] == $cardArr[1]&& $cardArr[0] ==$cardArr[2]){return ['type'=>3,'value'=>array_sum($cardArr),'length'=>3];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($totalCard == 4) {$newCardArr = $this->groupSameValues($cardArr);if (isset($newCardArr[1]) === false){return ['type'=>4,'value'=>array_sum($newCardArr[0]),'length'=>4];}if (count($newCardArr[0]) == 3){return ['type'=>5,'value'=>array_sum($newCardArr[0]),'length'=>4];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($totalCard == 5) {if ($this->isConsecutive($cardArr)) {if (in_array(12,$cardArr) || in_array(13,$cardArr) || in_array(14,$cardArr)){return ['type'=>-1,'value'=>[],'length'=>0];}return ['type'=>6,'value'=>array_sum($cardArr),'length'=>5];}$newCardArr = $this->groupSameValues($cardArr);if (count($newCardArr[0]) == 3){return ['type'=>7,'value'=>array_sum($newCardArr[0]),'length'=>5];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($totalCard == 6 ) {if ($this->hasUniformRepetition($cardArr)){if ($this->isConsecutive($cardArr)) {if (in_array(12,$cardArr)){return ['type'=>-1,'value'=>[],'length'=>0];}return ['type'=>8,'value'=>array_sum($cardArr),'length'=>6];}}if ($this->isConsecutive($cardArr)) {if (in_array(12,$cardArr) || in_array(13,$cardArr) || in_array(14,$cardArr)){return ['type'=>-1,'value'=>[],'length'=>0];}return ['type'=>9,'value'=>array_sum($cardArr),'length'=>6];}$newCardArr = $this->groupSameValues($cardArr);if (count($newCardArr[0]) == 4){return ['type'=>10,'value'=>array_sum($newCardArr[0]),'length'=>6];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($this->hasUniformRepetition($cardArr)){if ($this->isConsecutive($cardArr)) {if (in_array(12,$cardArr)){return ['type'=>-1,'value'=>[],'length'=>0];}return ['type'=>8,'value'=>array_sum($cardArr),'length'=>$totalCard];}return ['type'=>-1,'value'=>[],'length'=>0];}if ($this->isConsecutive($cardArr)) {if (in_array(12,$cardArr) || in_array(13,$cardArr) || in_array(14,$cardArr)){return ['type'=>-1,'value'=>[],'length'=>0];}return ['type'=>9,'value'=>array_sum($cardArr),'length'=>$totalCard];}if ($totalCard >= 8 && $totalCard%4 == 0) {$newCardArr = $this->groupSameValues($cardArr);$tripleGroups = array_filter($newCardArr, function($group) {return count($group) === 3;});$tripleGroups2 = array_filter($newCardArr, function($group) {return count($group) != 3;});$valueNum= 0;foreach ($tripleGroups as $value) {$valueNum+=array_sum($value);}$tripleCount = count($tripleGroups);$tripleCount2 = count($tripleGroups2);if ($tripleCount == $tripleCount2) {return ['type'=>11,'value'=>$valueNum,'length'=>$totalCard];}}if ($totalCard >= 10 && $totalCard%5 == 0) {$newCardArr = $this->groupSameValues($cardArr);$tripleGroups = array_filter($newCardArr, function($group) {return count($group) === 3;});$tripleGroups2 = array_filter($newCardArr, function($group) {return count($group) == 2;});$valueNum= 0;foreach ($tripleGroups as $value) {$valueNum+=array_sum($value);}$tripleCount = count($tripleGroups);$tripleCount2 = count($tripleGroups2);if ($tripleCount == $tripleCount2) {return ['type'=>12,'value'=>$valueNum,'length'=>$totalCard];}if(in_array(13,$cardArr) && in_array(14,$cardArr)){if ($tripleCount == ($tripleCount2+1)) {return ['type'=>12,'value'=>$valueNum,'length'=>$totalCard];}}}if ($totalCard >= 12 && $totalCard%6 == 0) {$newCardArr = $this->groupSameValues($cardArr);$tripleGroups = array_filter($newCardArr, function($group) {return count($group) === 4;});$valueNum= 0;foreach ($tripleGroups as $value) {$valueNum+=array_sum($value);}$tripleGroups2 = array_filter($newCardArr, function($group) {return count($group) == 2;});$tripleCount = count($tripleGroups);$tripleCount2 = count($tripleGroups2);if ($tripleCount == $tripleCount2) {if ($tripleCount == ($tripleCount2+1)) {return ['type'=>13,'value'=>$valueNum,'length'=>$totalCard];}}if(in_array(13,$cardArr) && in_array(14,$cardArr)){if ($tripleCount == ($tripleCount2+1)) {if ($tripleCount == ($tripleCount2+1)) {return ['type'=>13,'value'=>$valueNum,'length'=>$totalCard];}}}}return ['type'=>-1,'value'=>[],'length'=>0];}public function groupSameValues(array $array){$counts = array_count_values($array);arsort($counts);$result = [];foreach ($counts as $value => $count) {$result[] = array_fill(0, $count, $value);}return $result;}public function isConsecutive(array $array): bool {if (empty($array)) return false;sort($array);$unique = array_unique($array);sort($unique);return $unique === range($unique[0], $unique[count($unique) - 1]);}public function hasUniformRepetition(array $array): bool {if (empty($array)) return false;$counts = array_count_values($array);$firstCount = reset($counts);foreach ($counts as $count) {if ($count !== $firstCount) {return false;}}return true;}public function sortOut($game){usort($game, function($a, $b) {return $b['weight'] <=> $a['weight'];});return $game;}
public function gameCard($goOutCard,$thisGame,$thisGameCard,$room,$gameId,$roomGameId,$roomLandId){$gameStatus = 0; $isLandlord = 0; $thisCard = ['type'=>0, 'value'=>[], 'length'=>0];if ($gameId != $roomGameId){$thisCard = $this->checkCardType($thisGameCard,$thisGameCard);}$checkCard = $this->checkCardType($goOutCard,$thisGame);if($checkCard && $this->compareCard($thisCard,$checkCard)){$GameList = $this->deleteCard($goOutCard,$thisGame,$room,$gameId);if(count($GameList) == 0){$gameStatus = 1;if($gameId == $roomLandId){$isLandlord = 1;}}return ['gameStatus'=>$gameStatus,'gameList'=>$GameList,'isLandlord'=>$isLandlord];} else{echo "出牌错误";}}public function deleteCard($goOutCard,$thisGame,$room,$gameId){foreach ($thisGame as $key =>$value) {foreach ($goOutCard as $k => $v) {if ($value['card'] == $v['card'] && $value['design'] == $v['design']) {unset($thisGame[$key]);break;}}}$redis = new Redis();$redis->set('room_'.$room.'_game'.$gameId, json_encode($thisGame,true),86400);$redis->hset('room_'.$room.'_game','thisGame',json_encode($goOutCard,true));$redis->hset('room_'.$room.'_game','roomGameInit',$gameId);return $thisGame;}public function compareCard($thisGameCard,$gameCard){if ($thisGameCard['type'] == 0) {return true;}if ($gameCard['type'] == 999) {return true;}if ($gameCard['type'] == 4) {if ($thisGameCard['type'] == 999) {return false;}if ($thisGameCard['type'] == 4) {return $gameCard['value'] > $thisGameCard['value'];}return true;}if ($thisGameCard['type'] === $gameCard['type'] &&$thisGameCard['length'] === $gameCard['length']) {return $gameCard['value'] > $thisGameCard['value'];}return false;}
模拟玩法
$redis = new Redis();$room = 1; $GameInit =[ 0=>['name'=>'玩家1','id'=>1],1=>['name'=>'玩家2','id'=>2],2=>['name'=>'玩家3','id'=>3],];$thisUid = 2; $roomGame1 = json_decode($redis->get('room_'.$room.'_game1'),true);$roomGame2 = json_decode($redis->get('room_'.$room.'_game2'),true);$roomGame3 = json_decode($redis->get('room_'.$room.'_game3'),true);$roomBottom = json_decode($redis->get('room_'.$room.'_bottom'),true);$gameInit = $this->gameInit($room,$GameInit);$roomGame = $redis->hGetAll('room_'.$room.'_game');$dizhu = $this->cryHost($roomGame2, $roomBottom, $room, 2, $GameInit);if ($dizhu === false) {echo '抢地主失败,已经抢过地主了';}$gameRoom = [$roomGame2[15], $roomGame2[13], $roomGame2[11], $roomGame2[9], $roomGame2[8]];$cardArr = $this->gameCard($gameRoom,$roomGame2,json_decode($roomGame['thisGame'],true),$room,$thisUid,$roomGame['roomGameInit'],$roomGame['landlord']);