php_扑克类

 1 <?php
 2 session_start();
 3 //定义扑克牌
 4 $poker = array(
 5     '101', '111', '121', '131', '141',
 6     '102', '112', '122', '132', '142',
 7     '103', '113', '123', '133', '143',
 8     '104', '114', '124', '134', '144',
 9     '15', '16'
10 );
11 $microtime = microtime(true);  
12 
13 //循环统计
14 for($i = 0; $i < 500000; $i++)
15 {
16     $a_Player  = $b_Player = array();    //初始化A,B两玩家
17     $fp = array_rand($poker, 6);        //随机抽六张
18     foreach($fp as $v)
19     {
20         if(count($a_Player) < 3)
21         {
22             $a_Player[] = $poker[$v];    //前3张给A
23         }
24         else
25         {
26             $b_Player[] = $poker[$v];    //后3张给B
27         }
28     }
29         
30     three($a_Player, $b_Player);        //若A形成双王
31     three($b_Player, $a_Player);        //若B形成双王
32 }
33 
34 //形成三条后,比对方进行比较
35 function three($var1, $var2)
36 {
37     //去掉花色信息
38     for ($x=0; $x<count($var1); $x++)
39     {
40         $var1[$x] = substr($var1[$x],0,2);
41     }
42     for ($y=0; $y<count($var2); $y++)
43     {
44         $var2[$y] = substr($var2[$y],0,2);
45     }
46     
47     //一方抽到双王
48     if(is_integer(array_search("15", $var1)) && is_integer(array_search("16", $var1)))
49     {
50         if(count(array_count_values($var2)) == 1)
51         {
52             $player_other = array_values(array_diff($var1, array(15,16)));
53             if($player_other[0] == $var2[0])
54             {
55                 $_SESSION["result"] .= "双王_";
56             }
57         }
58     }
59     else
60     {
61         //一方只抽到一个王
62         if((is_integer(array_search("15", $var1)) || is_integer(array_search("16", $var1))) && count(array_count_values(array_diff($var1, array(15, 16)))) == 1)
63         {
64             if((is_integer(array_search("15", $var2)) || is_integer(array_search("16", $var2))) && count(array_count_values(array_diff($var2, array(15, 16)))) == 1)
65             {
66                 $player_other1 = array_values(array_diff($var1, array(15,16)));
67                 $player_other2 = array_values(array_diff($var2, array(15,16)));
68                 if($player_other1[0] == $player_other2[0])
69                 {
70                     $_SESSION["result"] .= "单王_";
71                 }
72             }
73         }
74         //一方抽到硬三条
75         elseif(count(array_count_values($var1)) == 1) 
76         {
77             if(is_integer(array_search("15", $var2)) && is_integer(array_search("16", $var2)))
78             {
79                 $player_other = array_values(array_diff($var2, array(15,16)));
80                 if($player_other[0] == $var1[0])
81                 {
82                     $_SESSION["result"] .= "硬三条_";
83                 }
84             }
85         }
86     }
87 }
88 echo "共执行:" . $_SESSION["num"]++ . "次比对运算,每次共抽取了{$i}次<br>";
89 echo "本次执行时间:";
90 echo microtime(TRUE) - $microtime . "秒<br>";
91 echo "比对结果:" . $_SESSION["result"];
92 
93 
94 
95 ?>
96 
97 <meta http-equiv="refresh" content="15"> 

 

posted @ 2013-03-12 14:12  王翔(kingfly)  阅读(891)  评论(0编辑  收藏  举报