Spawnfestis Dev. blog

Simple PHP random guessing game!

Posted by: spawnfestis on: March 24, 2009

default.php

Create a form with the name “guessedNum”, it should be an input form, and it should refer to guessgame.php

guessgame.php

<?php
// Declarations
$randNum = mt_rand(1,5);

// $_POST['nameofpostedarea'] returns what you sent from the default.php file,
// thus how you can use it to build a simple game :)
echo “You guessed “.$_POST['guessedNum'].”.

“;
if ($_POST['guessedNum'] == $randNum) {
echo “And you won! The right number was indeed “.$_POST['guessedNum'].” ! :) “;
}
else if($_POST['guessedNum'] > 5 || $_POST['guessedNum'] < 0) { // If the user tries to break the rules ;)
echo “Omg. I said between 1-5 which means “.$_POST['guessedNum'].” is invalid.. :( “;
} else {
echo “But you lost.. because the right number was “.$randNum.” :( “;
}
?>

Advertisement

3 Responses to "Simple PHP random guessing game!"

Det hade varit mer awesome om du gjorde så att det var samma nummer tills man gissade rätt / startade en ny runda.
Otherwise great progress!

Haha :) I’ve made even more progress. What I put up on the testside was all done within a few hours, which makes me remember – Oh! Programming in a new language is way too easy when you know another language :- )

Seems like something very basic.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Blog Stats

  • 3,875 hits
Follow

Get every new post delivered to your Inbox.