双色球
1 原理
- 红球: 33 选 6
- 蓝球: 16 选 1
- 取红球: 每取出一个后都要随机打乱,并且排序
- 取蓝球: 随机取出一个即可
- 中奖号: 6 个红球+1 个蓝球 = 7 个号球
- 页面: 用户只需要输入试机号数量即可
- 免费用户只能看 10 组, 更多得升级 VIP
2 数组函数
array_rand()
: 随机取值,返回值的键名array_push()
: 尾部追加成员array_splice()
: 删除或替换成员sort()
: 排序array_flip()
: 键值交换array_merge()
: 数组合并
3 代码示例:后端
<?php
// 双色球类
define('RED_BALLS', range(1, 33));
define('BLUE_BALLS', range(1, 16));
class DoubleColorBall
{
//红球
private static $redBalls = RED_BALLS;
//篮球
private static $blueBalls = BLUE_BALLS;
//选中红球
private static $electRedBalls = [];
//选中篮球
private static $electBlueBalls = [];
//保存试机号
private static $testNos = [];
//从33个红球中选中6个红球
private static function createRedBalls()
{
//循环拿红球
for ($i = 0; $i < 6; $i++) {
//1.随机取一个数,返回一个key
$key = array_rand(self::$redBalls);
//2.将这个球放入红球数组中
array_push(self::$electRedBalls, self::$redBalls[$key]);
//3.将选中的红球从33个红球中剔除
array_splice(self::$redBalls, $key, 1);
}
//排序输出
sort(self::$electRedBalls, SORT_NUMERIC);
}
//从16个篮球中选中1个篮球
private static function createBlueBalls()
{
self::$electBlueBalls = array_rand(array_flip(self::$blueBalls));
// print_r(self::$electBlueBalls);
}
//生成试机号
private static function createTestNos($n=5)
{
for ($i = 0; $i < $n; $i++) {
self::$redBalls = RED_BALLS;
self::$blueBalls = BLUE_BALLS;
self::$electRedBalls = [];
self::$electBlueBalls = null;
//生成红球
self::createRedBalls();
//生成篮球
self::createBlueBalls();
//合并红蓝球成为一个中奖机号
self::$testNos[] = array_merge(self::$electRedBalls, [self::$electBlueBalls]);
}
}
//获取试机号,供外部调用
public static function getTestNos($n)
{
self::createTestNos($n);
return self::$testNos;
}
}
// DoubleColorBall::createRedBalls();
// DoubleColorBall::createBlueBalls();
// print_r(DoubleColorBall::getTestNos(2));
// print_r(DoubleColorBall::$testNos);
$result = DoubleColorBall::getTestNos(5);
echo json_encode($result);
代码示例:前端
<?php
require __DIR__ . '/demo1.php';
// $n = isset($_GET['n']) ? $_GET['n'] : 5;
//null 合并运算符
$n = $_GET['n'] ?? 5;
$allBalls = DoubleColorBall::getTestNos($n);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>生成双色球中奖试机号</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="lottery">
<h2>双色球试机号</h2>
<form action="">
<input type="number" name="n" value="<?= $n ?>" onchange="isVip(this)">
<button>生成试机号</button>
</form>
<table>
<tbody>
<?php foreach ($allBalls as $key => $balls) : ?>
<tr>
<td><?= $key + 1 ?></td>
<?php foreach ($balls as $ball) : ?>
<td><?= $ball ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
<script>
function isVip(input){
if(input.value > 10){
alert('免费用户只能看10个,更多请升级VIP!');
input.max = 10;
input.value = 10;
location.reload(true);
return false;
}
}
</script>
</body>
</html>
代码示例:异步接口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="getData()">获取数据</button>
<script>
async function getData(){
const response = await fetch('demo1.php')
const data = await response.json()
console.log(data)
}
</script>
</body>
</html>