| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Guessing_Game_(PHP)_M_Leishman

Page history last edited by Mike Leishman 16 years, 9 months ago

This is code written to be used at the command line.

Ref: http://au2.php.net/features.commandline

 

 

<?php

//Guessing game program written in PHP and designed for use at the command line

//ref PHP manual http://au2.php.net/manual/en

$secret = (rand()%100); // random number between 1 and 100

$count = 1;

fwrite(STDOUT, "\n \n");

fwrite(STDOUT, "A number guessing game. <1-10> HOT <11-20> WARM otherwise COLD. \n \n");

//fwrite(STDOUT, $secret." \n ");

fwrite(STDOUT, "Guess the secret number <0..100> -> ");

fscanf(STDIN, "%d\n", $guess);

// Loop until the number is guessed

while ($guess != $secret)

   {

   //determine if high or low

   if ($guess > $secret)

     {fwrite(STDOUT, "Too High - ");}

   else

  {fwrite(STDOUT, "Too Low - ");}

 // determine how far off the mark

   $temperature = floor((abs($guess - $secret)-1) / 10);

   //fwrite(STDOUT, $temperature."\n");

   switch ($temperature) {

     case 0:

      fwrite(STDOUT, "You are HOT \n");

   break;

     case 1:

      fwrite(STDOUT, "You are WARM \n");

   break;

     default:

        fwrite(STDOUT, "You are COLD \n");

   break;

     }

   $count++;

   fwrite(STDOUT, "Guess the secret number <0..100> -> ");

   fscanf(STDIN, "%d\n", $guess);

   }

// number has been guessed

fwrite(STDOUT, "You guessed it. It took you ".$count." guesses.\n");

exit(0);

?>

Comments (0)

You don't have permission to comment on this page.