School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,141 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,762 people online right now. Registration is fast and FREE... Join Now!




8ball game in PHP

 
Reply to this topicStart new topic

> 8ball game in PHP

TMKCodes
Group Icon



post 5 Sep, 2009 - 12:49 PM
Post #1


Hey everyone, this tutorial will teach you 8ball game in PHP.

We have seen this game done in JAVA and C++ and now in PHP.

First we have to do the random number generator method.

CODE

<?php
function randomNum()
{
   // Generate random number
   $NUM = rand(1,4);
   // Return the random number
   return $NUM;
}
?>


Then we do the second method
CODE

<?php
function randomNum()
{
   // Generate random number
   $NUM = rand(1,4);
   // Return the random number
   return $NUM;
}

function check($line, $NUM)
{
   $randomCheck = $NUM;
   for($i = 0; $i < strlen($line); $i++)
   {
     if($line[$i]=='f')
     {
        for($j = 0; $j < 7; $j++)
        {
           $tmp += $line[$i];
           $i++;
        }
     }
   }
   if($tmp == "forever")
   {
     $answer = "Hell no...<br />";
   } else if($randomCheck == 1)
   {
     $answer = "Yes...<br />";
   } else if($randomCheck == 2)
   {
     $answer = "Maybe.. <br />";
   } else if($randomCheck == 3)
   {
     $answer = "Only time can tell...<br />";
   } else if($randomCheck == 4)
   {
     $answer = "Not enough information...<br />";
   } else uf($line == "stop?")
   {
     $answer = "Thanks for playing. Please be back! <br />";
   }
   return $answer;
}
?>


Now we have the needed methods, except the main code.
It will look like this:

CODE

<?php
function randomNum()
{
   // Generate random number
   $NUM = rand(1,4);
   // Return the random number
   return $NUM;
}

function check($line, $NUM)
{
   $randomCheck = $NUM;
   for($i = 0; $i < strlen($line); $i++)
   {
     if($line[$i]=='f')
     {
        for($j = 0; $j < 7; $j++)
        {
           $tmp += $line[$i];
           $i++;
        }
     }
   }
   if($tmp == "forever")
   {
     $answer = "Hell no...<br />";
   } else if($randomCheck == 1)
   {
     $answer = "Yes...<br />";
   } else if($randomCheck == 2)
   {
     $answer = "Maybe.. <br />";
   } else if($randomCheck == 3)
   {
     $answer = "Only time can tell...<br />";
   } else if($randomCheck == 4)
   {
     $answer = "Not enough information...<br />";
   } else uf($line == "stop?")
   {
     $answer = "Thanks for playing. Please be back! <br />";
   }
   return $answer;
}

$NUM = randomNum();

echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$<br />";
echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$<br />";
echo "                        WELCOME TO THE NUMBER 8 BALL GAME                            <br />";
echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$<br />";
echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$<br />";

echo "Enter question:";

// Input to question
echo '<form method="POST"><input type="text" name="line"><input type="submit" value="Submit" /></form>';

// output the answer to your question
$line = $_POST['line'];
$output = check($line, $NUM);
echo $line . "<br />";
echo $output . "<br />";

?>


This was the code. Save it into .php file and run in your web server and play!
smile.gif
Attached File  8ball.zip ( 677bytes ) Number of downloads: 25
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 03:30PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month