The game has not been started yet.
<?php
session_name('treasuregame');
session_start();
include('../includes/header.php');
error_reporting(E_ALL);
if (!isset($_SESSION['playerName'])) {
echo('<p class="red">The game has not been started yet.</p>');
echo('<p><a href="treasure_start.php">Go to Treasure Start Page</a></p>');
include('../includes/footer.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !$_SESSION['gameOver']) {
$chosenBox = isset($_POST['treasureBox']) ? $_POST['treasureBox'] : '';
$result = rand(1, 3);
if ($result == 1) {
$_SESSION['score'] += 10;
$logResult = 'Found treasure and earned 10 points.';
$_SESSION['message'] =
$_SESSION['playerName'] . ', you opened ' . $chosenBox .
' and found TREASURE! You earned 10 points.';
} elseif ($result == 2) {
$logResult = 'The box was empty.';
$_SESSION['message'] =
$_SESSION['playerName'] . ', you opened ' . $chosenBox .
' but it was empty.';
} else {
$_SESSION['score'] -= 5;
$logResult = 'Triggered a trap and lost 5 points.';
$_SESSION['message'] =
$_SESSION['playerName'] . ', you opened ' . $chosenBox .
' and triggered a TRAP! You lost 5 points.';
}
$file = '../doc/treasure_results.txt';
if (!file_exists($file)) {
$handle = fopen($file, 'w');
if ($handle) {
fclose($handle);
}
}
$entry =
$_SESSION['playerName'] . '||' .
$chosenBox . '||' .
$logResult . '||' .
$_SESSION['score'] . "\n";
$handle = fopen($file, 'a');
if ($handle) {
fwrite($handle, $entry);
fclose($handle);
}
if ($_SESSION['round'] >= $_SESSION['maxRounds']) {
$_SESSION['gameOver'] = true;
} else {
$_SESSION['round']++;
}
}
$endingMessage = '';
if ($_SESSION['gameOver']) {
if ($_SESSION['score'] >= 30) {
$endingMessage = 'Outstanding work, matey! You are a Treasure Master!';
} elseif ($_SESSION['score'] >= 10) {
$endingMessage = 'Nice job! You collected a respectable amount of treasure.';
} else {
$endingMessage = 'Better luck next voyage, pirate!';
}
}
?>
<div class="treasure-banner">
<h1>Treasure Hunt in Progress</h1>
<p>Choose carefully... not every box holds treasure.</p>
</div>
<div class="treasure-stats">
<p><strong>Pirate Name:</strong> <?php echo(htmlspecialchars($_SESSION['playerName'])); ?></p>
<p><strong>Round:</strong> <?php echo($_SESSION['round']); ?> of <?php echo($_SESSION['maxRounds']); ?></p>
<p><strong>Score:</strong> <?php echo($_SESSION['score']); ?></p>
</div>
<div class="treasure-message">
<?php echo(htmlspecialchars($_SESSION['message'])); ?>
</div>
<?php
if (!$_SESSION['gameOver']) {
?>
<form action="treasure_play.php" method="post">
<div class="treasure-box-row">
<button class="treasure-box" type="submit" name="treasureBox" value="Treasure Box 1">
<span>📦</span>
Treasure Box 1
</button>
<button class="treasure-box" type="submit" name="treasureBox" value="Treasure Box 2">
<span>📦</span>
Treasure Box 2
</button>
<button class="treasure-box" type="submit" name="treasureBox" value="Treasure Box 3">
<span>📦</span>
Treasure Box 3
</button>
</div>
</form>
<?php
} else {
?>
<div class="treasure-panel">
<h2>Game Over</h2>
<p class="final-score">Final Score: <?php echo($_SESSION['score']); ?></p>
<p><?php echo(htmlspecialchars($endingMessage)); ?></p>
<form class="play-again" action="treasure_reset.php" method="post">
<input class="treasure-button" type="submit" value="Play Again">
</form>
</div>
<?php
}
?>
<p><a href="treasure_log.php">View Treasure Log</a></p>
<?php
echo('<h2>SESSION Data</h2>');
echo('<pre>');
print_r($_SESSION);
echo('</pre>');
?>
<?php
include('../includes/footer.php');
?>