-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtebak_angka.php
120 lines (109 loc) · 3.27 KB
/
tebak_angka.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
session_start();
// Inisialisasi atau reset game
if (!isset($_SESSION['number']) || isset($_POST['reset'])) {
$_SESSION['number'] = rand(1, 100);
$_SESSION['attempts'] = 0;
$_SESSION['history'] = array();
}
$message = '';
$gameOver = false;
// Proses tebakan
if (isset($_POST['guess'])) {
$guess = intval($_POST['guess']);
$_SESSION['attempts']++;
if ($guess == $_SESSION['number']) {
$message = "Selamat! Anda berhasil menebak angka {$_SESSION['number']} dalam {$_SESSION['attempts']} percobaan!";
$gameOver = true;
} elseif ($guess < $_SESSION['number']) {
$message = "Terlalu rendah! Coba lagi dengan angka yang lebih besar.";
} else {
$message = "Terlalu tinggi! Coba lagi dengan angka yang lebih kecil.";
}
// Simpan history tebakan
$_SESSION['history'][] = array(
'guess' => $guess,
'message' => $message
);
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Game Tebak Angka</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f0f0f0;
}
.container {
background-color: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.history {
margin-top: 20px;
padding: 10px;
background-color: #f8f9fa;
border-radius: 5px;
}
.message {
padding: 10px;
margin: 10px 0;
border-radius: 5px;
background-color: #e9ecef;
}
button, input {
padding: 8px 15px;
margin: 5px;
}
button {
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h1>Game Tebak Angka</h1>
<p>Tebak angka antara 1 sampai 100!</p>
<?php if ($message): ?>
<div class="message">
<?php echo $message; ?>
</div>
<?php endif; ?>
<?php if (!$gameOver): ?>
<form method="post">
<input type="number" name="guess" min="1" max="100" required>
<button type="submit">Tebak!</button>
</form>
<?php endif; ?>
<form method="post">
<button type="submit" name="reset">Mulai Game Baru</button>
</form>
<?php if (!empty($_SESSION['history'])): ?>
<div class="history">
<h3>Riwayat Tebakan:</h3>
<?php foreach (array_reverse($_SESSION['history']) as $entry): ?>
<div class="message">
Tebakan: <?php echo $entry['guess']; ?> -
<?php echo $entry['message']; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<p>Jumlah percobaan: <?php echo $_SESSION['attempts']; ?></p>
</div>
</body>
</html>