-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
61 lines (55 loc) · 2.03 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!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>ElePHPant Number</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>
<?php
$userInput = (int) $_POST["userInput"];
$toGuess = (int) $_POST["toGuess"];
$attempts = getAttempts();
if (checkWin()) echo "★ You guessed the number ★";
elseif (in_array((string) $userInput, $attempts)) echo "⇒ Alredy tried that number ⇐";
elseif($userInput > $toGuess) echo "⇑ number was higher ⇑";
elseif($userInput < $toGuess) echo "⇓ Your number was lower ⇓";
else echo "⧫ Find the Number ⧫";
?>
</h1>
</header>
<main>
<section>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="number" name="userInput" min="0" max="50" minlength="1" maxlength="2" pattern="\d{1}"
value="<?php echo isset($_POST["userInput"])? $_POST["userInput"]:"0";?>" autocomplete="off"
step="1" required <?php echo isset($_POST["userInput"])? "autofocus":"";?>
title="Enter numbers in the range of “0-50”" />
<input type="hidden" name="toGuess"
value="<?php echo isset($_POST["toGuess"])? $_POST["toGuess"]:random_int(0, 50)?>">
<input type="hidden" name="attempts" value="<?php
$attempts = getAttempts();
if (isset($_POST["userInput"]))
echo in_array($_POST["userInput"], $attempts)? "":$_POST["attempts"].",".$_POST["userInput"];
else echo "51";
?>">
<input type="submit" name="guess" value="Try" <?php if(checkWin()) echo "disabled";?>>
</form>
</section>
</main>
</body>
<?php
function checkWin() {
if(
isset($_POST["toGuess"])
&& isset($_POST["userInput"])
&& $_POST["toGuess"] == $_POST["userInput"]
) return true;
}
function getAttempts() {return explode(",", $_POST["attempts"]);}
?>
</html>