Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Quiz Game

by dooberwah (Pilgrim)
on May 28, 2001 at 19:39 UTC ( #83740=sourcecode: print w/replies, xml ) Need Help??
Category: CGI
Author/Contact Info Ben Jacobs (dooberwah@crosswinds.net)
Description: This is a quiz game that I wrote to run on my website. It was designed to help you test your language skills when learning another language (the current words are in English and Esperanto). The script asks you a question and you have to type in the answer in a text box and submit it. Hope you enjoy, and comments are very welcome.
#!/usr/bin/perl

use strict;
use warnings;
use CGI qw/:standard/;

my @words = ( 'amiko::friend',
          'kafo::coffee',
          'filo::son',
          'kuko::cake',
          'frato::brother',
          'lakto::milk',
          'instruisto::teacher',
          'pano::bread',
          'knabo::boy',
          'sukero::sugar',
          'patro::father',
          'teo::tea',
          'akvo::water',
          'butiko::shop',
          'limonado::lemonade',
          'papero::paper',
          'plumo::pen',
          'taso::cup',
          );

srand ( time() ^ ($$ + ($$ << 15)) );

my $answer = param('answer');
($answer) = ( $answer =~ /^(\w*)$/ );

my $language = param('language');
$language = 'Esperanto' unless $language;

my $last_question = param('last_question');
($last_question) = ($last_question =~ /^(\w*)$/);

my ($esperanto, $english) = split /::/, @words[rand(scalar(@words))];
my $question = sprintf("%s\n", $language eq 'Esperanto' ? $esperanto :
+ $english);

my $match = gradeit( $last_question, $answer );

sub gradeit {
    my $first = shift;
    my $second = shift;
    
    my $together;
    if ($language eq 'Esperanto') {
    $together = join "::", $first, $second;
    } else {
    $together = join "::", $second, $first;
    }

    my $match = "No. $first != $second";
    foreach my $word (@words) {
    if ($word eq $together) {
        $match = "Correct. $first = $second";
        last;
    }
    }

    return $match;
}

param('last_question', $question);
print header,
    start_html( -title => 'Esperanto Quiz' ),
    h1('Esperanto Quiz'),
    p( b( 'Question: '),
       'What does ', 
       b($question),
       ' mean?' ),
    p( $match ),
    start_form,
    textfield( -name => 'answer',
           -size => 40,
           -maxlength => 40 ),
    br, 
    radio_group( -name => 'language',
         -values => ['English', 'Esperanto'],
         -default => 'Esperanto', 
         -linebreak => 'true'), 
    hidden( 'last_question' ),
    submit,
    end_form,
    end_html;
Replies are listed 'Best First'.
Re: Quiz Game
by malloc (Pilgrim) on May 31, 2001 at 18:35 UTC
    Very nice, but i must admit, the only reason i am ++ing this is because i have never seen esperanto before, and it is cool. :) -malloc
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://83740]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2018-02-19 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (265 votes). Check out past polls.

    Notices?