猜数字游戏
- 规则:输入一个数字,随机产生一个1~10的整数,把猜的数填入文本框,如果猜的数小了,提示“猜小了“,猜大了就提示“猜大了”,如果相等,提示“猜对了”,并重新产生随机数,进行下一次游戏。
- d1.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>猜数字</title>
<style>
input {
padding: 5px 20px;
height: 50px;
background-color: #3b4b59;
border: 1px solid #c0c0c0;
box-sizing: border-box;
color: #fff;
font-size: 20px;
}
button {
padding: 5px 20px;
height: 50px;
font-size: 16px;
}
</style>
</head>
<body>
<form action="check.php" method="get">
<?php if (isset($message)): ?>
<p><?php echo $message; ?></p>
<?php endif ?>
<fieldset>
<h1>猜数字游戏</h1>
<legend>填一个1~10的整数</legend>
<div>
<input type="number" name="num" placeholder="随便猜">
<button type="submit">猜一猜</button>
</div>
</form>
</body>
</html>
<?php
if (empty($_COOKIE['num']) || empty($_GET['num'])) {
$num = rand(1, 10);
setcookie('num', $num);
} else {
$count = empty($_COOKIE['count']) ? 0 : (int)$_COOKIE['count'];
if ($count < 10) {
$result = (int)$_GET['num'] - (int)$_COOKIE['num'];
if ($result == 0) {
echo '猜对了';
setcookie('num');
setcookie('count');
} elseif ($result > 0) {
echo '太大了';
} else {
echo '太小了';
}
setcookie('count', $count + 1);
} else {
echo 'looooooooooooow!';
setcookie('num');
setcookie('count');
}
}
- d1.php 运行截图
- check.php 运行截图