html5炸金花棋牌開發比較演算法在php中的實現

來源:互聯網
上載者:User

標籤:計算   [1]   代碼   shuff   search   i++   實現   public   大小   

PHP中紮金花比大小如何?

在棋牌遊戲中,不管是現實的還是線上的,紮金花無疑是最熱門棋牌遊戲之一,鄙人從小就酷愛紮金花,機緣巧合後面從事了IT行業,話不多說,直接進去正題吧。

紮金花兩副牌的比較規則就不說了,註明一下是順子的時候 : JQK < A23 < QKA

下面是個人一點建議,供大家參考!(有哪裡不對的歡迎指教 棋牌平台開發: h5.zhengtuwl.com)

思路:紮金花

1. 隨機產生兩幅牌,每副牌結構為

複製代碼 代碼如下:
array( 
array(‘Spade‘,‘K‘), 
array(‘Club‘,‘6‘), 
array(‘Spade‘,‘J‘), 
)

複製代碼 代碼如下:
array( 
array(‘Spade‘,‘K‘), 
array(‘Club‘,‘6‘), 
array(‘Spade‘,‘J‘), 
)
2. 計算每副牌的分值:每副牌有個原始大小(即排除對子,順子,金花,順金,筒子的大小),再

每張牌的分值為一個2位元,不足2位的補前置0,例如‘A‘:14,‘10‘:10,‘2‘:‘02‘,‘k‘:13,‘7‘:07

將3張牌按點數大小排序(從大到小),湊成一個6位元。例如‘A27‘:140702,‘829‘:090802,‘JK8‘:131108,‘2A10‘:141002

例外,對於對子要將對子的位元放在前兩位(後面會看到為什麼這麼做)。例如‘779‘:070709,‘7A7‘:070714,‘A33‘:030314

現在的分值是一個6位元,將對子設為一個原始值加上10*100000的值,現在為一個7位元。例如‘779‘:1070709,‘7A7‘:1070714,‘A33‘:1030314

對於順子,將結果加上20*100000.。例如‘345‘:2050403,‘QKA‘:2141312,‘23A‘:2140302

對於金花,將結果加上30*100000。例如‘Spade K,Spade 6,Spade J‘:3131106

因為順金的時候其實是金花和順子的和,所以順金應該是50*10000。 例如‘Spade 7,Spade 6,Spade 8‘:5080706

對於筒子,將結果加上60*100000。例如‘666‘:6060606,‘JJJ‘:6111111

3. 比較兩幅牌的大小(用所計算的分值來比較)

就這麼簡單!!

代碼如下(PHP)

複製代碼 代碼如下:
<?php 
class PlayCards 

public $suits = array(‘Spade‘, ‘Heart‘, ‘Diamond‘, ‘Club‘); 
public $figures = array(‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘10‘, ‘J‘, ‘Q‘, ‘K‘, ‘A‘); 
public $cards = array(); 
public function __construct() 

$cards = array(); 
foreach($this->suits as $suit){ 
foreach($this->figures as $figure){ 
$cards[] = array($suit,$figure); 


$this->cards = $cards; 

public function getCard() 

shuffle($this->cards); 
//產生3張牌 
return array(array_pop($this->cards), array_pop($this->cards), array_pop($this->cards)); 

public function compareCards($card1,$card2) 

$score1 = $this->ownScore($card1); 
$score2 = $this->ownScore($card2); 
if($score1 > $score2) return 1; 
elseif($score1 < $score2) return -1; 
return 0; 

private function ownScore($card) 

$suit = $figure = array(); 
foreach($card as $v){ 
$suit[] = $v[0]; 
$figure[] = array_search($v[1],$this->figures)+2; 

//補齊前置0 
for($i = 0; $i < 3; $i++){ 
$figure[$i] = str_pad($figure[$i],2,‘0‘,STR_PAD_LEFT); 

rsort($figure); 
//對於對子做特殊處理 
if($figure[1] == $figure[2]){ 
$temp = $figure[0]; 
$figure[0] = $figure[2]; 
$figure[2] = $temp; 

$score = $figure[0].$figure[1].$figure[2]; 
//筒子 60*100000 
if($figure[0] == $figure[1] && $figure[0] == $figure[2]){ 
$score += 60*100000; 

//金花 30*100000 
if($suit[0] == $suit[1] && $suit[0] == $suit[2]){ 
$score += 30*100000; 

//順子 20*100000 
if($figure[0] == $figure[1]+1 && $figure[1] == $figure[2]+1 || implode($figure) ==‘140302‘){ 
$score += 20*100000; 

//對子 10*100000 
if($figure[0] == $figure[1] && $figure[1] != $figure[2]){ 

$score += 10*100000; 

return $score; 



//test 
$playCard = new PlayCards(); 
$card1 = $playCard->getCard(); 
$card2 = $playCard->getCard(); 
$result = $playCard->compareCards($card1,$card2); 
echo ‘card1 is ‘,printCard($card1),‘<br/>‘; 
echo ‘card2 is ‘,printCard($card2),‘<br/>‘; 
$str = ‘card1 equit card2‘; 
if($result == 1) $str = ‘card1 is larger than card2‘; 
elseif($result == -1) $str = ‘card1 is smaller than card2‘; 
echo $str; 
function printCard($card) 

$str = ‘(‘; 
foreach($card as $v){ 
$str .= $v[0].$v[1].‘,‘; 

return trim($str,‘,‘).‘)‘; 
}


複製代碼 代碼如下:
<?php 
class PlayCards 

public $suits = array(‘Spade‘, ‘Heart‘, ‘Diamond‘, ‘Club‘); 
public $figures = array(‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘10‘, ‘J‘, ‘Q‘, ‘K‘, ‘A‘); 
public $cards = array(); 
public function __construct() 

$cards = array(); 
foreach($this->suits as $suit){ 
foreach($this->figures as $figure){ 
$cards[] = array($suit,$figure); 


$this->cards = $cards; 

public function getCard() 

shuffle($this->cards); 
//產生3張牌 
return array(array_pop($this->cards), array_pop($this->cards), array_pop($this->cards)); 

public function compareCards($card1,$card2) 

$score1 = $this->ownScore($card1); 
$score2 = $this->ownScore($card2); 
if($score1 > $score2) return 1; 
elseif($score1 < $score2) return -1; 
return 0; 

private function ownScore($card) 

$suit = $figure = array(); 
foreach($card as $v){ 
$suit[] = $v[0]; 
$figure[] = array_search($v[1],$this->figures)+2; 

//補齊前置0 
for($i = 0; $i < 3; $i++){ 
$figure[$i] = str_pad($figure[$i],2,‘0‘,STR_PAD_LEFT); 

rsort($figure); 
//對於對子做特殊處理 
if($figure[1] == $figure[2]){ 
$temp = $figure[0]; 
$figure[0] = $figure[2]; 
$figure[2] = $temp; 

$score = $figure[0].$figure[1].$figure[2]; 
//筒子 60*100000 
if($figure[0] == $figure[1] && $figure[0] == $figure[2]){ 
$score += 60*100000; 

//金花 30*100000 
if($suit[0] == $suit[1] && $suit[0] == $suit[2]){ 
$score += 30*100000; 

//順子 20*100000 
if($figure[0] == $figure[1]+1 && $figure[1] == $figure[2]+1 || implode($figure) ==‘140302‘){ 
$score += 20*100000; 

//對子 10*100000 
if($figure[0] == $figure[1] && $figure[1] != $figure[2]){ 

$score += 10*100000; 

return $score; 



//test 
$playCard = new PlayCards(); 
$card1 = $playCard->getCard(); 
$card2 = $playCard->getCard(); 
$result = $playCard->compareCards($card1,$card2); 
echo ‘card1 is ‘,printCard($card1),‘<br/>‘; 
echo ‘card2 is ‘,printCard($card2),‘<br/>‘; 
$str = ‘card1 equit card2‘; 
if($result == 1) $str = ‘card1 is larger than card2‘; 
elseif($result == -1) $str = ‘card1 is smaller than card2‘; 
echo $str;

function printCard($card) 

$str = ‘(‘; 
foreach($card as $v){ 
$str .= $v[0].$v[1].‘,‘; 

return trim($str,‘,‘).‘)‘; 
}

html5炸金花棋牌開發比較演算法在php中的實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.