Author Topic: Creating a simple "Guess the Number" game  (Read 4076 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Creating a simple "Guess the Number" game
« on: December 23, 2006, 03:09:53 PM »
Here's a tutorial I wrote on how to create a Guess the Number game.
It's aimed for those who know basic PHP, and are still new to it. I've basically taken almost every line of code and explained what it does and why.
Please leave some comments :)

For the tutorial, please see the attachments.
« Last Edit: December 23, 2006, 03:16:15 PM by Zeggy »

Offline Karlos

  • Level 7
  • *
  • Posts: 31
  • Reputation: +2/-0
    • View Profile
Re: Creating a simple "Guess the Number" game
« Reply #1 on: March 08, 2009, 12:34:20 PM »
Code: [Select]
<?php
$Message 
'';
if (isset(
$_POST['Submit'])) 
{
$Guess trim(intval($_POST['guess']));
$Random mt_rand('1''100');

if ($Guess '100' OR $Guess '1')
$Message 'Numbers Between 1-100 Only Please!';
else if (!ctype_digit($Guess))
$Message 'No Letters Fool!';
else if ($Guess == $Random)
$Message 'Congratulations!<br /> You Guessed The Correct Number: '.$Guess;
else
$Message 'Unlucky!<br /> You Guessed: '.$Guess.'! The Number was '.$Random;
}

echo 
'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Guess The Number!</title>
<style type="text/css">
body {
background-color : #131c20;
font-family : Arial;
font-size : 15px;
color : #92aab8;
width : 405px;
}

div.LBox {
float : left;
width : 50px;
padding-right : 5px;
}

div.RBox {
clear : right;
}

img {
border : 0;
}
</style>
</head>
<body>
<div>
<h3>Guess The Number!</h3>
Numbers Are Between 1-100.<br /><br />'
;
echo sprintf("%s"$Message);
echo '
<br /><br />'
;
echo sprintf("<form method='post' action='%s'>"$_SERVER['PHP_SELF']);
echo '
<div class="LBox">Guess:</div>
<div class="RBox"><input type="text" name="guess" /><br /></div>

<div class="LBox">&nbsp;</div>
<div class="RBox"><input type="submit" value="Guess!" name="Submit" /></div>
</form>
<br />
<br />
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Strict" style="width:88px;height:31px" />
</a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" style="width:88px;height:31px" />
</a>
</div>
</body>
</html>
'
;

?>

Bug Found & Fixed: Allowed You To Put A Number Then A Letter, Script Updated!

Functions I used:
« Last Edit: March 08, 2009, 01:22:48 PM by Karlos »

Offline Crazy-T

  • Level 19
  • *
  • Posts: 197
  • Reputation: +0/-0
  • Building Games
    • View Profile
Re: Creating a simple "Guess the Number" game
« Reply #2 on: October 05, 2009, 03:33:29 AM »
Quote
Functions I used
Quote
echo()

As from the php Manual.
echo() is not actually a function :)
Crazy-T

Offline Nox

  • Level 35
  • **
  • Posts: 738
  • Reputation: +12/-2
    • View Profile
Re: Creating a simple "Guess the Number" game
« Reply #3 on: October 05, 2009, 06:27:01 AM »
Like it's essential :P
neither are isset, if/elseif/else, all are language constructs but in this case it's rather playing with words ;)
Meet us at an IRC irc.freenode.net #bbg as well
Enjoy http://spiritbeacon.noxart.cz/ !

Offline Crazy-T

  • Level 19
  • *
  • Posts: 197
  • Reputation: +0/-0
  • Building Games
    • View Profile
Re: Creating a simple "Guess the Number" game
« Reply #4 on: October 05, 2009, 06:34:02 AM »
Like it's essential :P
neither are isset, if/elseif/else, all are language constructs but in this case it's rather playing with words ;)
Haha lol i knew someone would post more :L
Crazy-T

Offline SYKO

  • Level 4
  • *
  • Posts: 13
  • Reputation: +0/-0
    • View Profile
Re: Creating a simple "Guess the Number" game
« Reply #5 on: July 13, 2010, 12:37:54 PM »
I can't download the Tutorial Attachment.
Can you re-upload it?

Offline MystressNyx

  • Administrator
  • Level 16
  • *****
  • Posts: 139
  • Reputation: +6/-0
    • View Profile
    • eXtremeCast Games
Re: Creating a simple "Guess the Number" game
« Reply #6 on: July 13, 2010, 12:42:46 PM »
No, sorry. When we took over, we lost any previous attachments. Also, please do not use the report post function for anything other than to report forum violations.

Offline SYKO

  • Level 4
  • *
  • Posts: 13
  • Reputation: +0/-0
    • View Profile
Re: Creating a simple "Guess the Number" game
« Reply #7 on: July 13, 2010, 12:46:17 PM »
Also, please do not use the report post function for anything other than to report forum violations.

ahh, sorry for that.

btw, is there anyboy who can share the pdf file to me?
I saw that the file is downloaded 15 times. I think someone will share me.

Thanks

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal