PHP开发【石头剪刀布小游戏】

石头剪刀布小游戏

玩法超级简单,你只需要在下面选择石头、剪刀或者布,然后提交,系统就会随机生成电脑的选择,告诉你最终的结果哦!

游戏规则:

  1. 如果你的选择和电脑一样,那么就是平局。
  2. 如果你赢了,会有相应的提示告诉你"你赢了"。
  3. 如果你不幸输了,系统也会显示电脑的选择,让你知道差距在哪里。

使用指南:

  1. 代码如下,保存到一个忘记中:如 index.php。
  2. 代码部署到PHP服务器,比如 phpstudy。
  3. 运行网站,访问 index.php 文件即可。

代码

php 复制代码
<?php
session_start();

// 服务器端生成的随机选择对应的图片
$computer_choices_images = array(
    '石头' => 'stone.png',
    '剪刀' =>'scissors.png',
    '布' => 'paper.png'
);

// 生成随机选择
$computer_choice = array_rand($computer_choices_images);
$computer_choice_image = $computer_choices_images[$computer_choice];

// 检查是否有用户输入
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user_choice = $_POST['user_choice'];
    if ($user_choice === $computer_choice) {
        $_SESSION['result'] = '平局';
    } elseif (($user_choice === '石头' && $computer_choice === '剪刀') ||
              ($user_choice === '剪刀' && $computer_choice === '布') ||
              ($user_choice === '布' && $computer_choice === '石头')) {
        $_SESSION['result'] = '你赢了';
    } else {
        $_SESSION['result'] = '你输了';
        $_SESSION['computer_choice'] = $computer_choice;
    }
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>石头、剪刀、布游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f7f7f7;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #333;
        }
        form {
            margin: 20px 0;
        }
        label {
            margin-right: 10px;
        }
        input[type="radio"] {
            margin-right: 5px;
        }
        button {
            padding: 10px 20px;
            background-color: #5cb85c;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #4cae4c;
        }
        p {
            margin-top: 20px;
            font-size: 24px;
            font-weight: bold;
        }
        img {
            max-width: 100px;
            height: auto;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>石头、剪刀、布游戏</h1>
    <p>请猜出我选择的是哪个。</p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
        <div>
            <input type="radio" id="stone" name="user_choice" value="石头">
            <label for="stone">石头</label>
        </div>
        <div>
            <input type="radio" id="scissors" name="user_choice" value="剪刀">
            <label for="scissors">剪刀</label>
        </div>
        <div>
            <input type="radio" id="paper" name="user_choice" value="布">
            <label for="paper">布</label>
        </div>
        <button type="submit">提交</button>
    </form>
    <?php
    if (isset($_SESSION['result'])) {
        echo '<p>结果:'. $_SESSION['result']. '</p>';
        if ($_SESSION['result'] === '你输了') {
            echo '<p>电脑选择了:'. $_SESSION['computer_choice']. '</p>';
        }
    }
   ?>
</body>
</html>
相关推荐
devlei7 小时前
从源码泄露看AI Agent未来:深度对比Claude Code原生实现与OpenClaw开源方案
android·前端·后端
&&Citrus8 小时前
【CPN学习笔记(二)】Chap2 非分层颜色 Petri 网——从一个简单协议开始读懂 CPN
笔记·学习·php·cpn·petri网
努力的小郑9 小时前
Canal 不难,难的是用好:从接入到治理
后端·mysql·性能优化
Victor35610 小时前
MongoDB(87)如何使用GridFS?
后端
Victor35610 小时前
MongoDB(88)如何进行数据迁移?
后端
小红的布丁10 小时前
单线程 Redis 的高性能之道
redis·后端
GetcharZp10 小时前
Go 语言只能写后端?这款 2D 游戏引擎刷新你的认知!
后端
宁瑶琴12 小时前
COBOL语言的云计算
开发语言·后端·golang
普通网友12 小时前
阿里云国际版服务器,真的是学生党的性价比之选吗?
后端·python·阿里云·flask·云计算
IT_陈寒13 小时前
Vue的这个响应式问题,坑了我整整两小时
前端·人工智能·后端