Blogger Information
Blog 94
fans 0
comment 0
visits 92413
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【PHP】双色球实战
可乐随笔
Original
2125 people have browsed it

双色球

1 原理

  1. 红球: 33 选 6
  2. 蓝球: 16 选 1
  3. 取红球: 每取出一个后都要随机打乱,并且排序
  4. 取蓝球: 随机取出一个即可
  5. 中奖号: 6 个红球+1 个蓝球 = 7 个号球
  6. 页面: 用户只需要输入试机号数量即可
  7. 免费用户只能看 10 组, 更多得升级 VIP

2 数组函数

  1. array_rand(): 随机取值,返回值的键名
  2. array_push(): 尾部追加成员
  3. array_splice(): 删除或替换成员
  4. sort(): 排序
  5. array_flip(): 键值交换
  6. array_merge(): 数组合并

3 代码示例:后端

  1. <?php
  2. // 双色球类
  3. define('RED_BALLS', range(1, 33));
  4. define('BLUE_BALLS', range(1, 16));
  5. class DoubleColorBall
  6. {
  7. //红球
  8. private static $redBalls = RED_BALLS;
  9. //篮球
  10. private static $blueBalls = BLUE_BALLS;
  11. //选中红球
  12. private static $electRedBalls = [];
  13. //选中篮球
  14. private static $electBlueBalls = [];
  15. //保存试机号
  16. private static $testNos = [];
  17. //从33个红球中选中6个红球
  18. private static function createRedBalls()
  19. {
  20. //循环拿红球
  21. for ($i = 0; $i < 6; $i++) {
  22. //1.随机取一个数,返回一个key
  23. $key = array_rand(self::$redBalls);
  24. //2.将这个球放入红球数组中
  25. array_push(self::$electRedBalls, self::$redBalls[$key]);
  26. //3.将选中的红球从33个红球中剔除
  27. array_splice(self::$redBalls, $key, 1);
  28. }
  29. //排序输出
  30. sort(self::$electRedBalls, SORT_NUMERIC);
  31. }
  32. //从16个篮球中选中1个篮球
  33. private static function createBlueBalls()
  34. {
  35. self::$electBlueBalls = array_rand(array_flip(self::$blueBalls));
  36. // print_r(self::$electBlueBalls);
  37. }
  38. //生成试机号
  39. private static function createTestNos($n=5)
  40. {
  41. for ($i = 0; $i < $n; $i++) {
  42. self::$redBalls = RED_BALLS;
  43. self::$blueBalls = BLUE_BALLS;
  44. self::$electRedBalls = [];
  45. self::$electBlueBalls = null;
  46. //生成红球
  47. self::createRedBalls();
  48. //生成篮球
  49. self::createBlueBalls();
  50. //合并红蓝球成为一个中奖机号
  51. self::$testNos[] = array_merge(self::$electRedBalls, [self::$electBlueBalls]);
  52. }
  53. }
  54. //获取试机号,供外部调用
  55. public static function getTestNos($n)
  56. {
  57. self::createTestNos($n);
  58. return self::$testNos;
  59. }
  60. }
  61. // DoubleColorBall::createRedBalls();
  62. // DoubleColorBall::createBlueBalls();
  63. // print_r(DoubleColorBall::getTestNos(2));
  64. // print_r(DoubleColorBall::$testNos);
  65. $result = DoubleColorBall::getTestNos(5);
  66. echo json_encode($result);

代码示例:前端

  1. <?php
  2. require __DIR__ . '/demo1.php';
  3. // $n = isset($_GET['n']) ? $_GET['n'] : 5;
  4. //null 合并运算符
  5. $n = $_GET['n'] ?? 5;
  6. $allBalls = DoubleColorBall::getTestNos($n);
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <title>生成双色球中奖试机号</title>
  15. <link rel="stylesheet" href="style.css">
  16. </head>
  17. <body>
  18. <div class="lottery">
  19. <h2>双色球试机号</h2>
  20. <form action="">
  21. <input type="number" name="n" value="<?= $n ?>" onchange="isVip(this)">
  22. <button>生成试机号</button>
  23. </form>
  24. <table>
  25. <tbody>
  26. <?php foreach ($allBalls as $key => $balls) : ?>
  27. <tr>
  28. <td><?= $key + 1 ?></td>
  29. <?php foreach ($balls as $ball) : ?>
  30. <td><?= $ball ?></td>
  31. <?php endforeach ?>
  32. </tr>
  33. <?php endforeach ?>
  34. </tbody>
  35. </table>
  36. </div>
  37. <script>
  38. function isVip(input){
  39. if(input.value > 10){
  40. alert('免费用户只能看10个,更多请升级VIP!');
  41. input.max = 10;
  42. input.value = 10;
  43. location.reload(true);
  44. return false;
  45. }
  46. }
  47. </script>
  48. </body>
  49. </html>

代码示例:异步接口

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <button onclick="getData()">获取数据</button>
  11. <script>
  12. async function getData(){
  13. const response = await fetch('demo1.php')
  14. const data = await response.json()
  15. console.log(data)
  16. }
  17. </script>
  18. </body>
  19. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!