<?php
header('Content-type:text/html;charset=utf-8');
function getPoker(){
//先定义一个空数组
$poker = array();
//外循环遍历花色
for($i=0;$i<4;$i++){
//内循环遍历13张纸牌
for($j=1;$j<13;$j++){
//根据$i $j得到字符串
switch ($i) {
case 0:
$str = "<font color='black'>黑桃";
break;
case 1:
$str = "<font color='red'>红桃";
break;
case 2:
$str = "梅花";
break;
case 3:
$str = "方块";
break;
}
switch ($j) {
case 1:
$str.="A";
break;
case 11:
$str.="J";
break;
case 12:
$str.="Q";
break;
case 13:
$str.="K";
break;
default:
$str.=$j;
}
$poker[] = $str."</font>";
}
}
$poker[] ="<font color='red'>大鬼</font>";
$poker[] ="<font color='black'>小鬼</font>";
return $poker;
shuffle($poker);
echo "<pre>";
//count — 计算数组中的单元数目或对象中的属性个数
for($a=0;$a<count($poker);$a++){
echo $poker[$a]."<br/>";
}
}
$new_poker = getPoker();
shuffle($new_poker);
//建立三个数组(模拟三个人)
//array_slice — 从数组中取出一段
$person1 = array_slice($new_poker,0,17); //0~16
$person2 = array_slice($new_poker,17,17); //17~34
$person3 = array_slice($new_poker,34,17); //34~51
$lord = array_slice($new_poker,51,3); //51 52 53
//从三人中随机抽取一位
$person_name = 'person'.mt_rand(1,3);
//array_merge — 合并一个或多个数组
$$person_name = array_merge($$person_name,$lord);
echo "<pre>";
var_dump($person1);
var_dump($person2);
var_dump($person3); |
|