這篇文章主要介紹了PHP實現炸金花遊戲比賽的方法,實例分析了炸金花遊戲的實現原理與相關演算法技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了PHP實現炸金花遊戲比賽的方法。具體分析如下:
程式離不開演算法,前面討論過尋路的演算法。不過,在當時的範例圖中,可選的路徑是唯一的。我們挑選一個演算法,就是說要把這個唯一的路徑選出來,怎麼選呢?
炸金花兩副牌的比較規則就不說了,註明一下是順子的時候: JQK
思路:炸金花
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 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,',').')'; }
#總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。
相關推薦:
#以上是PHP實現炸金花遊戲比賽的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版
視覺化網頁開發工具