PHP實現(xiàn)扎金花游戲之大小比賽的方法
來源:易賢網(wǎng) 閱讀:894 次 日期:2015-03-12 10:38:41
溫馨提示:易賢網(wǎng)小編為您整理了“PHP實現(xiàn)扎金花游戲之大小比賽的方法”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了PHP實現(xiàn)扎金花游戲之大小比賽的方法,實例分析了扎金花游戲的實現(xiàn)原理與相關(guān)算法技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP實現(xiàn)扎金花游戲之大小比賽的方法。分享給大家供大家參考。具體分析如下:

程序離不開算法,前面討論過尋路的算法。不過,當(dāng)時的示例圖中,可選的路徑是唯一的。我們挑選一個算法,就是說要把這個唯一的路徑選出來,怎么選呢?

還記得上初中的時候經(jīng)常下午放學(xué)就躲在路邊扎金花來賭*錢,貌似還上癮了,現(xiàn)在過年的時候還經(jīng)常一起扎金花賭*錢,但運氣不啥好,每次都是輸啊。

今天陽光明媚,由于清明節(jié)才出去玩了,所以今天沒有去哪。閑著沒事就想了下怎么用程序?qū)崿F(xiàn)金花中兩幅牌的大小比較,現(xiàn)在把它實現(xiàn)了,有些方法還是蠻重要的,因此就記下來。

好了,不廢話了。

扎金花兩副牌的比較規(guī)則就不說了,注明一下是順子的時候 : JQK < A23 < QKA

思路:扎金花

1. 隨機生成兩幅牌,每副牌結(jié)構(gòu)為

代碼如下:

array(

array('Spade','K'),

array('Club','6'),

array('Spade','J'),

)

代碼如下:

array(

array('Spade','K'),

array('Club','6'),

array('Spade','J'),

)

2. 計算每副牌的分值:每副牌有個原始大小(即排除對子,順子,金花,順金,筒子的大?。?,再

每張牌的分值為一個2位數(shù),不足2位的補前導(dǎo)0,例如'A':14,‘10':10,'2‘:'02‘,'k‘:13,'7‘:07

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

例外,對于對子要將對子的位數(shù)放在前兩位(后面會看到為什么這么做)。例如‘779':070709,‘7A7':070714,‘A33':030314

現(xiàn)在的分值是一個6位數(shù),將對子設(shè)為一個原始值加上10*100000的值,現(xiàn)在為一個7位數(shù)。例如‘779':1070709,‘7A7':1070714,‘A33':1030314

對于順子,將結(jié)果加上20*100000.。例如‘345':2050403,‘QKA':2141312,‘23A':2140302

對于金花,將結(jié)果加上30*100000。例如‘Spade K,Spade 6,Spade J':3131106

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

對于筒子,將結(jié)果加上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;

}

//補齊前導(dǎo)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;

}

//補齊前導(dǎo)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程序設(shè)計有所幫助。

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:PHP實現(xiàn)扎金花游戲之大小比賽的方法

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)