2014年七月写过的代码,现在看来,还有待改进呀

那个时候闲来无事,随手写了一个斗地主的逻辑

<?php

/**

 * Created by JetBrains PhpStorm.

 * User: Administrator

 * Date: 14-7-30

 * Time: 下午3:49

 * To change this template use File | Settings | File Templates.

 */

class DemoAction extends Action{

    private $order = array();

    function _initialize(){

        $this->rule();

    }

    function index(){

        $array = $this->getAllCard();

        shuffle($array);

        $i = 0;

        //分牌

        foreach($array as $val){

            $j = ++$i%3;

            $result[$j][] = $val;

        }

        //三张底牌

        $other = array();

        $other[0] = array_pop($result[0]);

        $other[1] = array_pop($result[1]);

        $other[2] = array_pop($result[2]);

        //print_r($result[0]);

        $result2[0] = $this->sort($result[0]);

        $res = $this->insert_three($result2[0],$other);

        print_r($res);die;

        $result2[1] = $this->sort($result[1]);

        $result2[2] = $this->sort($result[2]);

        print_r($result2);

        print_r($other);

    }

    //排序

    function sort($cards){

        $temp = array();

        foreach($cards as $k=>$v){

            $temp[$v[‘num‘]][] = $k;

        }

        $temp2 = array();

        foreach($this->order as $val){

            $temp[$val] && $temp2[] = $temp[$val];

        }

        $temp3 = array();

        foreach($temp2 as $k2=>$v2){

            foreach($v2 as $k3=>$v3){

                $temp3[] = $cards[$v3];

            }

        }

        return $temp3;

    }

    //抢地主之后,将底牌插入序列

    function insert_three($array,$three){

        foreach($three as $val){

            $temp[$val[‘num‘]][] = $val[‘color‘];

        }

        $nums = array_keys($temp);

        $new_array = array();

        $insert1 = true;

        $insert2 = true;

        $insert3 = true;

        foreach($array as $key2=>$val2){

            $new_array[] = $val2;

            $next_key = $key2+1;

            if($insert1){

                $begin_compare = $this->compare_num($val2[‘num‘],$nums[0]);

                $end_compare = $this->compare_num($nums[0],$array[$next_key][‘num‘]);

                if($begin_compare<=0 && $end_compare<=0){

                    foreach($temp[$nums[0]] as $color){

                        $new_array[] = array(‘num‘=>$nums[0],‘color‘=>$color);

                    }

                    unset($nums[0]);

                    $insert1 = false;

                }

            }

            if($nums[1] && $insert2){

                $begin_compare = $this->compare_num($val2[‘num‘],$nums[1]);

                $end_compare = $this->compare_num($nums[1],$array[$next_key][‘num‘]);

                if($begin_compare<=0 && $end_compare<=0){

                    foreach($temp[$nums[1]] as $color){

                        $new_array[] = array(‘num‘=>$nums[1],‘color‘=>$color);

                    }

                    unset($nums[1]);

                    $insert2 = false;

                }

            }

            if($nums[2] && $insert3){

                $begin_compare = $this->compare_num($val2[‘num‘],$nums[2]);

                $end_compare = $this->compare_num($nums[2],$array[$next_key][‘num‘]);

                if($begin_compare<=0 && $end_compare<=0){

                    foreach($temp[$nums[2]] as $color){

                        $new_array[] = array(‘num‘=>$nums[2],‘color‘=>$color);

                    }

                    unset($nums[2]);

                    $insert3 = false;

                }

            }

        }

        $last_count = count($nums);

        if($last_count!=0){

            if($last_count==1){

                $this_num = current($nums);

                foreach($temp[$this_num] as $color){

                    $new_array[] = array(‘num‘=>$this_num,‘color‘=>$color);

                }

            }else{

                sort($nums);

                foreach($nums as $k=>$v){

                    foreach($temp[$v] as $color){

                        $new_array[] = array(‘num‘=>$v,‘color‘=>$color);

                    }

                }

            }

        }

        return $new_array;

    }

    function getAllCard(){

        return array(

            array("num"=>1,"color"=>1),

            array("num"=>1,"color"=>2),

            array("num"=>1,"color"=>3),

            array("num"=>1,"color"=>4),

            array("num"=>2,"color"=>1),

            array("num"=>2,"color"=>2),

            array("num"=>2,"color"=>3),

            array("num"=>2,"color"=>4),

            array("num"=>3,"color"=>1),

            array("num"=>3,"color"=>2),

            array("num"=>3,"color"=>3),

            array("num"=>3,"color"=>4),

            array("num"=>4,"color"=>1),

            array("num"=>4,"color"=>2),

            array("num"=>4,"color"=>3),

            array("num"=>4,"color"=>4),

            array("num"=>5,"color"=>1),

            array("num"=>5,"color"=>2),

            array("num"=>5,"color"=>3),

            array("num"=>5,"color"=>4),

            array("num"=>6,"color"=>1),

            array("num"=>6,"color"=>2),

            array("num"=>6,"color"=>3),

            array("num"=>6,"color"=>4),

            array("num"=>7,"color"=>1),

            array("num"=>7,"color"=>2),

            array("num"=>7,"color"=>3),

            array("num"=>7,"color"=>4),

            array("num"=>8,"color"=>1),

            array("num"=>8,"color"=>2),

            array("num"=>8,"color"=>3),

            array("num"=>8,"color"=>4),

            array("num"=>9,"color"=>1),

            array("num"=>9,"color"=>2),

            array("num"=>9,"color"=>3),

            array("num"=>9,"color"=>4),

            array("num"=>10,"color"=>1),

            array("num"=>10,"color"=>2),

            array("num"=>10,"color"=>3),

            array("num"=>10,"color"=>4),

            array("num"=>11,"color"=>1),

            array("num"=>11,"color"=>2),

            array("num"=>11,"color"=>3),

            array("num"=>11,"color"=>4),

            array("num"=>12,"color"=>1),

            array("num"=>12,"color"=>2),

            array("num"=>12,"color"=>3),

            array("num"=>12,"color"=>4),

            array("num"=>13,"color"=>1),

            array("num"=>13,"color"=>2),

            array("num"=>13,"color"=>3),

            array("num"=>13,"color"=>4),

            array("num"=>14,"color"=>1),

            array("num"=>14,"color"=>2),

        );

    }

    function rule(){

        $this->order = array(3,4,5,6,7,8,9,10,11,12,13,1,2,14);

    }

    //根据规则比较牌的大小

    function compare_num($num1,$num2){

        if($num1===false)return false;

        $index1 = array_search($num1,$this->order);

        $index2 = array_search($num2,$this->order);

        if($index1>$index2)return 1;

        if($index1<$index2)return -1;

        return 0;

    }

    //炸弹

    function is_bomb($cards){

        $count = $cards[0][‘num‘]==14?2:4;

        if(count($cards)!=$count) return false;

        if(!empty($cards[0][‘num‘])

            && $cards[0][‘num‘]==$cards[1][‘num‘]

            && $cards[0][‘num‘]==$cards[2][‘num‘]

            && $cards[0][‘num‘]==$cards[3][‘num‘]){

            return true;

        }

        return false;

    }

    //炸弹KO

    function hit_bomb($initiative,$passive){

        if( $this->is_bomb($initiative)

            && $this->is_bomb($passive)){

            if($this->compare_num($initiative[0][‘num‘],$passive[0][‘num‘])==1){

                return true;

            }else{

                return false;

            }

        }

        return null;

    }

    //单牌$cards = array([0]=array(....));

    function is_one($cards){

        if(count($cards)!=1)return false;

        return true;

    }

    //单牌KO $passive是单牌

    function hit_one($initiative,$passive){

        if($this->is_one($initiative)){

            if($this->compare_num($initiative[0][‘num‘],$passive[0][‘num‘])==1){

                return true;

            }

        }elseif($this->is_bomb($initiative))

            return true;

    }

    //对牌

    function is_twins($cards){

        if(count($cards)!=2)return false;

        return (!empty($cards[0][‘num‘]) && $cards[0][‘num‘]==$cards[1][‘num‘] && $cards[0][‘num‘]!=14);

    }

    /**

     * 对牌KO,要求$passive必须是is_twins

     * @param $initiative主动

     * @param $passive被动

     */

    function hit_twins($initiative,$passive){

        //if($this->is_twins($passive)){

            if($this->is_twins($initiative)){

                if($this->compare_num($initiative[0][‘num‘],$passive[0][‘num‘])==1){

                    return true;

                }

            }elseif($this->is_bomb($initiative))

                return true;

        //}

        //return null;

    }

    //三个X

    function is_three($cards){

        if(count($cards)!=3)return false;

        return (!empty($cards[0][‘num‘]) && $cards[0][‘num‘]==$cards[1][‘num‘] && $cards[0][‘num‘]==$cards[2][‘num‘]);

    }

    //要求$passive必须是is_three

    function hit_three($initiative,$passive){

        //if($this->is_three($passive)){

            if($this->is_three($initiative)){

                if($this->compare_num($initiative[0][‘num‘],$passive[0][‘num‘])==1){

                    return true;

                }

            }elseif($this->is_bomb($initiative))

                return true;

        //}

        //return null;

    }

    //三带一

    function is_three_one($cards){

        if(count($cards)!=4)return false;

        $tongji = array();

        foreach($cards as $ca){

            ++$tongji[$ca[‘num‘]];

        }

        if(count($tongji)!=2)return false;

        return array_search(3,$tongji);

    }

    //$passive_num 由is_three_one计算得来

    function hit_three_one($initiative,$passive_num){

        if($this->compare_num($this->is_three_one($initiative),$passive_num)==1)

        {

                return true;

        }elseif($this->is_bomb($initiative))

            return true;

    }

    //三代二

    function is_three_two($cards){

        if(count($cards)!=5)return false;

        $tongji = array();

        foreach($cards as $ca){

            ++$tongji[$ca[‘num‘]];

        }

        if(count($tongji)!=2)return false;

        if(array_search(2,$tongji)===false)return false;

        return array_search(3,$tongji);

    }

    //$passive_num 由is_three_two计算得来

    function hit_three_two($initiative,$passive_num){

        if($this->compare_num($this->is_three_two($initiative),$passive_num)==1)

        {

            return true;

        }elseif($this->is_bomb($initiative))

            return true;

    }

    //四带二

    function is_four_two($cards){

        if(count($cards)!=6)return false;

        $tongji = array();

        foreach($cards as $ca){

            ++$tongji[$ca[‘num‘]];

        }

        if(count($tongji)!=2)return false;

        if(array_search(2,$tongji)===false)return false;

        return array_search(4,$tongji);

    }

    //$passive_num 由is_four_two计算得来

    function hit_four_two($initiative,$passive_num){

        if($this->is_bomb($initiative))

            return true;

        if($this->compare_num($this->is_four_two($initiative),$passive_num)==1)

        {

            return true;

        }

    }

    //顺子

    function is_sequence($cards){

        $count = count($cards);

        if($count<5)return false;

        $nums = array();

        foreach($cards as $ca){

            //小二,王都不算顺子牌

            if(in_array($ca[‘num‘],array(2,14)))return false;

            $nums[] = $ca[‘num‘];

        }

        $min = min($nums);

        if($min==1)return false;

        $index = array_search($min,$this->rule);

        for($i=1;$i<$count;++$i)

            if(!in_array($this->rule[++$index],$nums))return false;

        return $min;

    }

    //$passive_num 由is_sequence计算得来

    function hit_sequence($initiative,$passive_num){

        if($this->is_bomb($initiative))

            return true;

        if($this->compare_num($this->is_sequence($initiative),$passive_num)==1)

        {

            return true;

        }

    }

    //多联对

    function is_more_twins($cards){

        $count = count($cards);

        if($count<6 || $count%2==1)return false;

        $nums = $tongji = array();

        foreach($cards as $ca){

            //小二,王都不算顺子牌

            if(in_array($ca[‘num‘],array(2,14)))return false;

            $nums[] = $ca[‘num‘];

            ++$tongji[$ca[‘num‘]];

        }

        foreach($tongji as $v){

            if($v!=2)return false;

        }

        $min = min($nums);

        if($min==1)return false;

        $index = array_search($min,$this->rule);

        $count = $count/2;

        for($i=1;$i<$count;++$i)

            if(!in_array($this->rule[++$index],$nums))return false;

        return $min;

    }

    //$passive_num 由is_more_twins计算得来

    function hit_more_twins($initiative,$passive_num){

        if($this->is_bomb($initiative))

            return true;

        if($this->compare_num($this->is_more_twins($initiative),$passive_num)==1)

        {

            return true;

        }

    }

    //飞机

    function is_plan($cards){

        $count = count($cards);

        if($count<6)return false;

        $nums = $tongji = array();

        foreach($cards as $ca){

            //王都不算飞机牌

            if(in_array($ca[‘num‘],array(14)))return false;

            $nums[] = $ca[‘num‘];

            ++$tongji[$ca[‘num‘]];

        }

        $main = array();

        $append_count = 0;

        foreach($tongji as $k=>$v){

            if($v>3)

                return false;

            elseif($v==3)

                $main[] = $k;

            else

                ++$append_count;

        }

        $main_count = count($main);

        if($main_count!=$append_count)return false;

        $min = min($main);

        $index = array_search($min,$this->rule);

        $count = $count/2;

        for($i=1;$i<$count;++$i)

            if(!in_array($this->rule[++$index],$nums))return false;

        return $min;

    }

    function hit_plan($initiative,$passive_num){

        if($this->is_bomb($initiative))

            return true;

        if($this->compare_num($this->is_plan($initiative),$passive_num)==1)

        {

            return true;

        }

    }

}

现在看来,当时还是挺不规范的

时间: 2024-10-13 22:03:01

2014年七月写过的代码,现在看来,还有待改进呀的相关文章

2014年七月华为校招机试题目--最难的一道, 呵呵!

今天百无聊赖之时, 漫心看到14年的华为校招机试题目, 一共三道, 前两道皆是平平, 第三道却柳暗花明, 让人眼前一亮. 咋一看, 饶有趣味, 看似平淡无奇, 然而却玄机颇深(对我这种弱渣而言).(不过对于ACMer, 好像应该用基础算法, 就能解决!) (然而我也只会基础的算法!!忏愧的紧!!!).如果有幸被大神看到, 能指点我一两招, 不胜感激!  下面是题目和我的详细题解思路(可供巨巨一笑!嘿嘿!). 2014年七月华为校招机试题目: 第三题: 输入一个正整数X,在下面的等式左边的数字之间

jQuery表单验证插件----通过name属性来关联字段来验证,改变默认的提示信息,将校验规则写到 js 代码中

一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二. 添加一个另外一个插件jquery.validate.messages_cn.js. 改变默认提示方式. 三.jQuery表单验证插件----通过name属性来关联字段来验证,将校验规则写到 js 代码中. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.

[Eclipse]已经写好的代码怎样切换为unix下的换行符?

切换换行符的显示格式, 一般的设置是这样的: 但是这样只能对新文件起作用,原来已经写好的还是那熊样.(怎么办呢,得吃后悔药啊,谁让你开始没准备好呢?!T_T) 下面就是后悔药: 这样就OK了. 附:有图有真相 转换前: 转换后: [Eclipse]已经写好的代码怎样切换为unix下的换行符?,布布扣,bubuko.com

java写的迷宫代码

迷宫代码:截图如下:package com.zxl.maze; /* * 抽象类表示选择不同的算法*/ public abstract class AbstractMap { /* * 得到数据*/ public abstract boolean[][] getData(int m,int n); /* * 重置*/ public abstract void reset(int m,int n); } package com.zxl.maze; /* *深度优先,生成迷宫*/ import ja

写出优美代码的方式,两个习惯:一步到位VS迭代优化

最近把手头这个安卓APP的所有事务性方法都写完了,有了以下体会,新手体会,老鸟轻拍 想写成优美代码的人一般都会有这样的想法: 一定要在写每一句代码,写每一个方法,构造每一个类的时候,都要记得优化:解耦以复用,拆分方法以复用,使用循环减少冗余,限制循环次数减少无效操作,等等.. 这个想法一定没有错,但很多时候往往会是这样的情况: 当功能一复杂,比如你已经分解了几个方法,比如你已经使用了几层循环(有点过分...),比如在多线程中 你经常无法一步到位地完成那么多优化 这往往造成你写一句代码会思考很久

写了个代码统计工具

周末想统计下XCL-Charts到底写了多少行代码,就用Go语言写了个代码统计工具,效果还不错,统计速度也快. 就把统计报表部份今天完善了下,硬生生把很短的代码澎涨到200多行了. 除了必要的代码行数和文件总计外,增加了代码行数有300,500,1000,5000及5000以上这几个级别的代码文件个数统计功能. 同时,用户可以指定显示出代码行数大于等于某值的所有文件明细,并按从大到小顺序排列. 贴下效果: E:\XCLTools\src\crname>go run scounter.go -l=

2014年七月最佳jQuery插件荟萃

本月的jQuery插件荟萃我们将介绍几款非常不错的jQuery插件,涵盖了表单,幻灯,页面设计等等方面,相信大家肯定会喜欢! Select or Die 一款帮助开发者美化并且强化选择框的jQuery插件 jPList 可 以非常灵活帮助开发人员生成可过滤,分页和排序的HTML结构.支持数据源:PHP+mysql,ASP.net +SQL,PHP+SQLite.可以和知名的javascript模板,例如,handlebars,Mustache等等配合工作.支持所有的主流浏 览器.非商业项目,个人

PHP写一段代码,确保多个进程同时写入一个文件成功

这个需求是在软件设计过程常见的加锁.学计算机的同学都应该知道,这个是在<计算机操作系统>课程上有这个知识点.主要要考虑的是进程的同步,也就是进程对资源的互斥访问.OK,用程序说话吧! <?phpfunction writefile( $filename, $data ){ $fp = fopen( $filepath, 'a' );   //以追加的方式打开文件,返回的是指针 do{   usleep( 100 );  //暂停执行程序,参数是以微秒为单位的 }while( !flock

当一个程序员写不出代码了,该怎么办?(转载)

翻译作者:码农网-小峰 转载地址:http://www.codeceo.com/article/what-to-do-programming-sucks.html 原文标题:What Do You Do When Programming Sucks ? 原文地址:https://blog.klimczyk.pl/2017/05/04/what-to-do-when-programming-sucks/ 你已经对着电脑 N 个小时了.不知道该写什么代码,或者一种摔键盘的冲动正在你的胸中酝酿. 咖啡