The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WWW::20Q - Perl interface to the classic 20 Questions game as provided by 20Q.net

SYNOPSIS

  use WWW::20Q;

  # Create a new object
  my $q = new WWW::20Q;

  # Start a new game and get the first question
  # ("Is it an animal, vegetable, or mineral?")
  my $first = $q->start;

  # Print the first question and our options.
  print "$first\n"
    . $q->choices . "\n";

  # Loop while we're playing.
  while ($q->playing) {
    # Give the user a chance to answer.
    my $answer = <STDIN>;
    chomp $answer;

    # Send the answer into the game and get the next question
    # (or the same question if the answer was unacceptable)
    my $next = $q->answer ($answer);

    # Print the next question and our choices.
    print "$next\n"
      . $q->choices . "\n";

  }

  print "Game Over\n";

DESCRIPTION

This module serves as an interface to the Classic 20 Questions game as provided on 20Q.net. Currently the module only supports the English version of the Classic game; the "20Q Music" and "20Q People" and other like games are *not* yet supported.

METHODS

new

Create a new instance of WWW::20Q. The only argument you should pass is debug. Before doing so, take note of everything debug is going to do. See "DEBUG MODE".

setErrorHandler (CODEREF)

Set a custom error handler. If you are making a GUI frontend for 20Q, this will help your program to respond to and show error messages when a console wouldn't be available. The error handler receives ($object,$error_string) in @_. The default handler is to just warn the errors to STDERR.

start

Start a new game of 20 Questions. This method will return the first question, which is typically as follows:

  Q1.  Is it classified as Animal, Vegetable or Mineral?

answer (ANSWER)

Answer the previously asked question. ANSWER must be one of the answers allowed for the previous question (see method choices below).

This method will return the next question down the line. If the answer given was not acceptible for the last question asked, the last question is returned from this method.

When the game comes to an end, this method will not return a new question, but will return the final statement. This statement might look like either of these:

  20Q Won!
  You were thinking of a piranha.

  You won!
  You have stumped 20Q!

choices

Returns your list of choices in a comma-separated scalar. One of these values must be given in an answer to the last question.

question

Returns (repeats) the last question that was asked by 20Q.

playing

Returns true if the game is currently in progress. This is best used as your main program loop, as shown in the "SYNOPSIS". As long as a question is pending a response, this method returns true.

callError (ERRSTR) *Internal

This method provokes your error handler with a message.

debug (STRING) *Internal

This prints a debug message when debug mode is on.

request (METHOD, URL, ARGS) *Internal

Make an HTTP request. Returns the HTML content of the page if successful.

dump (FILENAME, DATA) *Internal

Dump HTML data DATA into file FILENAME. Also dumps the hash structure of the object into the file core.txt. Used in debug mode.

DEBUG MODE

When debug mode is activated:

  - Several debug messages are printed to STDOUT.
  - The "Start New Game" page and all subsequent question pages have their HTML codes
    dumped into start.html or q.html, respectfully.
  - All internal hash data is dumped into core.txt on every game request.

If your program needs files by the same names as these, use debug mode when in a safer environment.

SEE ALSO

The official website of 20 Questions: http://www.20q.net/

CHANGES

  0.01  Sun Dec 24 19:54:46 2006
        - Original version.

COPYRIGHT AND LICENSE

Copyright (C) 2006 Casey Kirsle

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.