Last active
November 3, 2023 16:30
PHP based number guessing game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
// Check if the target number and attempts are set in the session | |
if (!isset($_SESSION['target_number'])) { | |
// Generate a random number between 1 and 10 if it's not set | |
$_SESSION['target_number'] = rand(1, 10); | |
$_SESSION['attempts'] = 0; | |
$_SESSION['previous_attempts'] = array(); | |
} | |
// Initialize other variables | |
$guess_message = ''; | |
if (isset($_POST['submit'])) { | |
// Check if a guess was submitted | |
if (isset($_POST['guess'])) { | |
$guess = filter_input(INPUT_POST, 'guess', FILTER_SANITIZE_NUMBER_INT); | |
$_SESSION['attempts']++; | |
// Add the guess to the array of previous attempts | |
$_SESSION['previous_attempts'][] = $guess; | |
// Check if the guess is correct | |
if ($guess == $_SESSION['target_number']) { | |
$guess_message = "Congratulations! You guessed the correct number ({$_SESSION['target_number']}) in {$_SESSION['attempts']} attempts."; | |
// Clear the target number, attempts, and previous attempts | |
unset($_SESSION['target_number']); | |
unset($_SESSION['attempts']); | |
unset($_SESSION['previous_attempts']); | |
} else { | |
if ($guess < $_SESSION['target_number']) { | |
$guess_message = 'Try again! The target number is higher.'; | |
} else { | |
$guess_message = 'Try again! The target number is lower.'; | |
} | |
} | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Coding Arena</title> | |
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,600,700" rel="stylesheet"> | |
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous"> | |
<style> | |
* { | |
font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen-Sans", Ubuntu, "Cantarell", "Helvetica Neue", sans-serif; | |
} | |
body { | |
font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen-Sans", Ubuntu, "Cantarell", "Helvetica Neue", sans-serif; | |
background-color: #f2f2f2; | |
margin: 0; | |
padding: 0; | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
height: 100vh; | |
} | |
.container { | |
background-color: #fff; | |
padding: 20px; | |
border-radius: 5px; | |
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); | |
max-width: 600px; | |
} | |
h1, h2, h3 { | |
color: #333; | |
} | |
div { | |
background-color: #f5f5f5; | |
padding: 5px; | |
color: #4f4f4f; | |
border: 1px solid #ddd; | |
border-radius: 3px; | |
display: block; | |
} | |
/* Style input fields and buttons */ | |
/* Add more styles as needed */ | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>My Coding Arena</h1> | |
<p>The code shows here:</p> | |
<div> | |
<!-- Add your code below this line --> | |
<h2>Guess the Number Game</h2> | |
<?php | |
// Display guess message (result or hint) | |
if (!empty($guess_message)) { | |
echo "<h3>" . htmlspecialchars($guess_message) . "</h3>"; | |
} | |
?> | |
<p>Guess a number between 1 and 10:</p> | |
<form method="post"> | |
<input type="number" name="guess" min="1" max="10" required> | |
<input type="submit" name="submit" value="Submit Guess"> | |
</form> | |
<?php | |
// Display previous attempts | |
if (isset($_SESSION['previous_attempts']) && !empty($_SESSION['previous_attempts'])) { | |
echo '<h2>Previous Attempts:</h2>'; | |
echo '<ul>'; | |
foreach ($_SESSION['previous_attempts'] as $attempt) { | |
echo "<li>" . htmlspecialchars($attempt) . "</li>"; | |
} | |
echo '</ul>'; | |
} | |
?> | |
<!-- Add your code above this line --> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment