php编程:用php实现随机发放扑克牌
描述:一副扑克牌共54张(包括大、小王),用php制作一发牌器,向三人随机发牌;
编辑poker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<?php
function poker(){
//建立数组保存牌组
$num = [ 'a' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , 'j' , 'q' , 'k' ];
$icon = [ '♥' => 'red' , '♦️' => 'red' , '♠' => 'black' , '♣' => 'black' ];
//生成扑克牌组
foreach ( $icon as $key => $vi ) {
foreach ( $num as $vn ) {
$poker [] = "<font style ='color:$vi;'> $vn $key </font>" ;
}
}
$poker [] = "<font style = 'color:red;'>大王</font>" ;
$poker [] = "<font style = 'color:black;'>小王</font>" ;
shuffle( $poker ); // 乱序
return $poker ;
}
?>
</body>
</html>
|
添加样式(poker1.php),调用poker.php:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!doctype html>
<html>
<head>
<title>扑克大赛</title>
<style type= "text/css" >
div{margin: 15px 0;}
font{border: 1px solid #ccc;padding: 6px 3px; margin-right: 10px;}
</style>
</head>
<body>
<div>刘德华的牌</div>
<?php
include './poker.php' ; //调用
$sp = poker();
for ( $i =1; $i <=12 ; $i ++) {
echo current( $sp );
next( $sp );
}
?>
<div>周润发的牌</div>
<?php
for ( $i =1; $i <=12 ; $i ++) {
echo current( $sp );
next( $sp );
}
?>
<div>李飞扬的牌</div>
<?php
for ( $i =1; $i <=12 ; $i ++) {
echo current( $sp );
next( $sp );
}
?>
</body>
</html>
|
运行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43514711/article/details/105633507