Page 1 of 1

8ball game in PHP

#1 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Posted 05 September 2009 - 01:49 PM

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.

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



Then we do the second method
<?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:

<?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!
:)
Attached File  8ball.zip (677bytes)
Number of downloads: 232

Is This A Good Question/Topic? 1
  • +

Page 1 of 1