PHP实现扎金花游戏之大小比赛的方法


Posted in PHP onMarch 10, 2015

本文实例讲述了PHP实现扎金花游戏之大小比赛的方法。分享给大家供大家参考。具体分析如下:

程序离不开算法,前面讨论过寻路的算法。不过,当时的示例图中,可选的路径是唯一的。我们挑选一个算法,就是说要把这个唯一的路径选出来,怎么选呢?

还记得上初中的时候经常下午放学就躲在路边扎金花来赌*钱,貌似还上瘾了,现在过年的时候还经常一起扎金花赌*钱,但运气不啥好,每次都是输啊。

今天阳光明媚,由于清明节才出去玩了,所以今天没有去哪。闲着没事就想了下怎么用程序实现金花中两幅牌的大小比较,现在把它实现了,有些方法还是蛮重要的,因此就记下来。

好了,不废话了。

扎金花两副牌的比较规则就不说了,注明一下是顺子的时候 : JQK < A23 < QKA

思路:扎金花

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,',').')';  

}

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
swfupload 多文件上传实现代码
Aug 27 PHP
第四章 php数学运算
Dec 30 PHP
win7+apache+php+mysql环境配置操作详解
Jun 10 PHP
关于查看MSSQL 数据库 用户每个表 占用的空间大小
Jun 21 PHP
php实现最简单的MVC框架实例教程
Sep 08 PHP
PHP根据图片色界在不同位置加水印的方法
Jul 01 PHP
PHP实现图片自动清理的方法
Jul 08 PHP
WordPress中登陆后关闭登陆页面及设置用户不可见栏目
Dec 31 PHP
PHP程序员的技术成长规划
Mar 25 PHP
yii2.0实现pathinfo的形式访问的配置方法
Apr 06 PHP
Laravel学习教程之request validation的编写
Oct 25 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
Jun 16 PHP
php获取本周开始日期和结束日期的方法
Mar 09 #PHP
php数组转成json格式的方法
Mar 09 #PHP
php实现将数组转换为XML的方法
Mar 09 #PHP
php返回字符串中所有单词的方法
Mar 09 #PHP
php通过正则表达式记取数据来读取xml的方法
Mar 09 #PHP
PHP实现算式验证码和汉字验证码实例
Mar 09 #PHP
PHP实现指定字段的多维数组排序函数分享
Mar 09 #PHP
You might like
在PHP中养成7个面向对象的好习惯
2010/01/28 PHP
iis下php mail函数的sendmail配置方法(官方推荐)
2012/04/25 PHP
php获取中文拼音首字母类和函数分享
2014/04/24 PHP
php将远程图片保存到本地服务器的实现代码
2015/08/03 PHP
DEDE实现转跳属性文档在模板上调用出转跳地址
2016/11/04 PHP
解决laravel资源加载路径设置的问题
2019/10/14 PHP
javascript的数据类型、字面量、变量介绍
2012/05/23 Javascript
js操作textarea 常用方法总结
2012/12/03 Javascript
js截取字符串的两种方法及区别详解
2013/11/05 Javascript
使用JQ来编写最基本的淡入淡出效果附演示动画
2014/10/31 Javascript
浅谈javascript中关于日期和时间的基础知识
2016/07/13 Javascript
js仿小米手机上下滑动效果
2017/02/05 Javascript
使用pm2部署node生产环境的方法步骤
2019/03/09 Javascript
使用webpack搭建vue环境的教程详解
2019/12/31 Javascript
VUE中使用HTTP库Axios方法详解
2020/02/05 Javascript
[01:07:57]DOTA2-DPC中国联赛 正赛 Ehome vs Magma BO3 第二场 1月19日
2021/03/11 DOTA
Python用imghdr模块识别图片格式实例解析
2018/01/11 Python
Python使用jsonpath-rw模块处理Json对象操作示例
2018/07/31 Python
pandas使用apply多列生成一列数据的实例
2018/11/28 Python
python飞机大战pygame游戏框架搭建操作详解
2019/12/17 Python
Python使用Tkinter实现转盘抽奖器的步骤详解
2020/01/06 Python
用python获取txt文件中关键字的数量
2020/12/24 Python
Marc Jacobs官方网站:美国奢侈品牌
2017/08/29 全球购物
专注澳大利亚特产和新西兰特产的澳洲中文网:0061澳洲制造
2019/03/24 全球购物
技术总监岗位职责
2013/12/05 职场文书
迟到早退检讨书
2014/02/10 职场文书
2014新年元旦活动策划方案
2014/02/18 职场文书
团代会主持词
2014/04/02 职场文书
小学语文课后反思精选
2014/04/25 职场文书
怎样拟定创业计划书
2014/05/01 职场文书
治超工作实施方案
2014/05/04 职场文书
工商管理自荐书
2014/07/06 职场文书
商铺门面租房协议书
2014/10/21 职场文书
2015年教育实习工作总结
2015/04/24 职场文书
安全教育第一课观后感
2015/06/17 职场文书
动画《朋友游戏》公开佐藤友生绘制的开播纪念绘
2022/04/06 日漫