<?php
// Memulai sesi untuk menyimpan data game
session_start();

// Inisialisasi game jika belum ada atau tombol reset ditekan
if (!isset($_SESSION['target_number']) || isset($_POST['reset'])) {
    $_SESSION['target_number'] = rand(1, 100);
    $_SESSION['attempts'] = 0;
    $_SESSION['history'] = array(); // Menggunakan array() gaya lama PHP 5.6
    $_SESSION['message'] = "Game dimulai! Silakan tebak angka dari 1 sampai 100.";
    $_SESSION['status'] = "info"; // info, success, danger
}

// Proses tebakan ketika form dikirim
if (isset($_POST['guess']) && isset($_POST['number'])) {
    $user_guess = intval($_POST['number']);
    $_SESSION['attempts']++;
    
    if ($user_guess < 1 || $user_guess > 100) {
        $_SESSION['message'] = "Masukkan angka yang valid antara 1 sampai 100!";
        $_SESSION['status'] = "danger";
    } else if ($user_guess < $_SESSION['target_number']) {
        $_SESSION['message'] = "Tebakan Anda (" . $user_guess . ") Terlalu RENDAH!";
        $_SESSION['status'] = "danger";
        array_unshift($_SESSION['history'], $user_guess . " (Terlalu Rendah)");
    } else if ($user_guess > $_SESSION['target_number']) {
        $_SESSION['message'] = "Tebakan Anda (" . $user_guess . ") Terlalu TINGGI!";
        $_SESSION['status'] = "danger";
        array_unshift($_SESSION['history'], $user_guess . " (Terlalu Tinggi)");
    } else {
        $_SESSION['message'] = "SELAMAT! Anda berhasil menebak angka " . $_SESSION['target_number'] . " dalam " . $_SESSION['attempts'] . " kali percobaan.";
        $_SESSION['status'] = "success";
        array_unshift($_SESSION['history'], $user_guess . " (BENAR!)");
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Game Tebak Angka - PHP 5.6</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f7f6;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
        }
        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            max-width: 450px;
            width: 100%;
            text-align: center;
        }
        h2 {
            color: #333;
            margin-bottom: 20px;
        }
        .alert {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 4px;
            font-weight: bold;
        }
        .alert-info { background-color: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
        .alert-danger { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .alert-success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        
        input[type="number"] {
            width: 70%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
            font-weight: bold;
        }
        .btn-guess { background-color: #007bff; }
        .btn-guess:hover { background-color: #0056b3; }
        .btn-reset { background-color: #6c757d; margin-top: 15px; width: 100%; }
        .btn-reset:hover { background-color: #545b62; }
        
        .history-box {
            margin-top: 25px;
            text-align: left;
            background: #f9f9f9;
            padding: 15px;
            border-radius: 4px;
            border: 1px solid #ddd;
            max-height: 180px;
            overflow-y: auto;
        }
        .history-box h4 { margin-top: 0; color: #555; border-bottom: 1px solid #ddd; padding-bottom: 5px;}
        .history-box ul { padding-left: 20px; margin: 0; }
        .history-box li { margin-bottom: 5px; color: #666; }
    </style>
</head>
<body>

<div class="container">
    <h2>Game Tebak Angka 🎯</h2>
    <p>Sistem sudah memilih angka acak antara 1 - 100.</p>

    <!-- Notifikasi Pesan -->
    <div class="alert alert-<?php echo $_SESSION['status']; ?>">
        <?php echo $_SESSION['message']; ?>
    </div>

    <!-- Form Tebakan -->
    <?php if ($_SESSION['status'] !== 'success'): ?>
        <form method="post" action="">
            <input type="number" name="number" min="1" max="100" placeholder="Ketik angka..." required autofocus>
            <button type="submit" name="guess" class="btn-guess">Tebak</button>
        </form>
    <?php endif; ?>

    <!-- Tombol Reset -->
    <form method="post" action="">
        <button type="submit" name="reset" class="btn-reset">Main Lagi / Reset</button>
    </form>

    <!-- Riwayat Tebakan -->
    <?php if (!empty($_SESSION['history'])): ?>
        <div class="history-box">
            <h4>Riwayat Tebakan Anda (Total: <?php echo $_SESSION['attempts']; ?>):</h4>
            <ul>
                <?php foreach ($_SESSION['history'] as $index => $history_item): ?>
                    <li>Tebakan ke-<?php echo ($_SESSION['attempts'] - $index); ?>: <strong><?php echo $history_item; ?></strong></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
</div>

</body>
</html>
