Jump to content

Trying to Create a Text Game...


firelight4321

Recommended Posts

The text game I want to create is going to follow closely along the lines of the old text game called ZORK.

 

The problem I'm having, is that I have this great design, and still can't figure out how to implement it.

 

Does anyone know of a way to take the text the Player inputs, have my php script read it, and then output the correct text?

I've looked at examples for superglobal arrays: $_REQUEST, $_POST, and $_GET. But, all the examples that are given just take the information the Player inputs and outputs it in a variables place.

I need the Player's input text to be read by my script and matched, or not matched, accordingly and give an output based on what they wrote.

 

By the way, I'm new to PHP. It's having a hard time growing on me.

 

HELP?!?!  ???  :'(

Link to comment
https://forums.phpfreaks.com/topic/95520-trying-to-create-a-text-game/
Share on other sites

As a simple example.

 

<?php

 if (isset($_POST['submit'])) {
   switch ($_POST['text'])) {
     case 'hello':
       echo "Hello!!!";
       break;
     case 'foo':
        echo "What the hell is foo?";
        break;
     default:
        echo "Invalid command";
   }
 }

?>
<form action="" method="post">
 <input type="text" name="text"> (type foo or hello)
 <input type="submit" name="submit">
</form>

Does anyone know of a way to take the text the Player inputs, have my php script read it, and then output the correct text?

 

Regular Expressions (REGEX) might be of interest to you... if you wanted commands like

 

/pickup item name

 

and you had a text element <input type="text" name="input" />

 

your script would look something like this:

 

<?php

$items = array('robe', 'wizard hat');

if (preg_match('=^/pickup (.+)$=i', $_POST['input'], $match) ) {
   if (in_array(strtolower($match[1]), $items) )
      echo 'You have picked up a ' . ucwords($match[1]);
  else
      echo 'Could not find ' . ucwords($match[1]);
} else {
   echo 'Unknown command';
}

?>

 

That should give you a bit of a start... study up on those functions and regular expressions. Good luck with the game

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.