VoiceXML Adventure Game - Mother of Perl | 6 | WebReference

VoiceXML Adventure Game - Mother of Perl | 6

To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

VoiceXML Adventure Game

Making Our Adventure Dynamic

Now that we have a working VoiceXML document that provides some dynamic capabilities, it's time to tie in into a back-end Perl script that provides true dynamic capabilities. For this first run of our dynamic Perl script, we'll develop a script that dynamically generates rooms. Each room will always have three doors. Because the script is random, there are many different possible combinations. Initially, users won't be able to interact with any of the objects in the rooms, they'll only be able to open doors. In Part III, we'll add this capability.

room.pl

use CGI;
use strict;
my @rooms = (
             'large ball room',
	     'pool hall',
             'large red barn',
             'seven eleven',
             'bedroom',
             'bathroom',
             'funeral parlor',
             'windy room with bark walls',
             'dimly lit tunnel',
             'prison cell',
             'hospital waiting room',
             'underground parking garage',
             'circular stairwell',
             'arboretum',
             'janitors closet',
             'closet',
             'subway car',
             'cubicle',
             'lighthouse tower',
             'graveyard'
            );
my @objects = (
               'full of clowns dressed in red',
               'full of smoke but no people',
               'with a dead horse on the floor',
               'with a broken slurpy machine. In the corner, a troll is sniffing the burritos',
               'with black curtains and something under the covers',
               'with padded walls',
               'with wood bark walls',
               'with flickering florescent lights',
               'with an oil spotted shag carpet',
               'full of mad raving lunatics',
               'with a man and his monkey',
               'full of monkeys',
               'with William Shatner singing tambourine man',
               'with a large hungry monkey',
               'full of drunken sailors',
               'full of blind dwarves',
               'full of Windows programmers',
               'with a man scratching his monkey'
              );
my $q = new CGI;
print "Content-Type: text/xml\n\n";
&prompt;
sub prompt {
my $room   = $rooms[int(rand(scalar(@rooms)))];
my $object = $objects[int(rand(scalar(@objects)))];
   print <<VXML;
<?xml version="1.0"?>
<vxml version="1.0">
  <form>
    <field name="answer">
      <grammar>
        <![CDATA[
          [
            [dtmf-1 one first] {<option "door1">}
            [dtmf-2 two second] {<option "door2">}
            [dtmf-3 three third] {<option "door3">}
            [(touch the monkey)] {<option "monkey">}
            [dtmf-0 huh help what doh] {<option "help">}
          ]
        ]]&gt;
      </grammar>
      <prompt>
	<audio>You are in a $room $object. There are three doors.</audio> 
      </prompt>
      <filled>
        <submit next="http://www.textant.com/cgi-bin/room.pl"/>
      </filled>
      <nomatch count="1">
        <audio>Wrong door.</audio>
        <audio>To open the first door, press or say 1.</audio>
        <pause>300</pause>
        <audio>To open the second door, press or say 2.</audio>
        <pause>300</pause>
        <audio>To open the third door, press or say 3.</audio>
        <listen/>
      </nomatch>
      <nomatch count="2">
        <audio>You fool! That is not a choice I am giving you.</audio>
        <pause>300</pause>
        <audio>Press or say 1, 2, or 3.</audio>
        <listen/>
      </nomatch>
      <nomatch count="3">
        <audio>Pretend you have a brain and press 1, 2, or 3.</audio>
        <listen/>
      </nomatch>
      <noinput count="1">
        <audio>You need to select a door.</audio>
        <pause>300</pause>
        <audio>Press or say 1, 2, or 3.</audio>
        <listen/>
      </noinput>
      <noinput count="2">
        <audio>If you do not press 1, 2, or 3, I am going to kill
        the monkey!</audio>
        <listen/>
      </noinput>
      <noinput count="3">
        <audio>Ok. That's it. The monkey is dead and it's your
        fault because you did not choose 1, 2, or 3.</audio>
        <listen/>
      </noinput>
      <noinput count="4">
        <audio>1, 2, or 3.</audio>
        <listen/>
      </noinput>
      <help>
        <audio>Why are you asking for help?</audio>
        <pause>300</pause>
        <audio>Go away.</audio>
        <reprompt/>
      </help>
    </field>
  </form>
</vxml>
VXML
}
                


To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

http://www.internet.com

Produced by Jonathan Eisenzopf
All Rights Reserved. Legal Notices.
Created: March 8, 2001

URL: http://www.webreference.com/perl/tutorial/21/6.html