I am new to Perl, but I used this as a learning exercise (very little of this is original code. Ref: Learning Perl, and Perlmonks). Basically a game to see how fast you can type, with an output of stats at the end.

The dictionary is hopefully on everyone's system, it doesn't exactly have real words though...

Just run the program on Terminal.

If you don't have List::Utils, just take that line out, and the declaration line that uses shuffle, and replace @shuffled_words with @words in the foreach loop. If you don't have that dictionary... find another text file and use appropriately, or just type up your own(not recommended). ENJOY:

#! usr/bin/perl use strict; use warnings; # -- TYPING SPEED GAME -- # #OPTIONAL (but currently implemented): use List::Util 'shuffle'; #Or use substitute/ don't sort. # WORD LIST: #my @words = qw{ fred barney pebbles dino wilma betty }; #Could get different list of words from another file or, like above, j +ust typing them in. my $file = '/usr/share/dict/words'; open( FIN, '<', $file ) or die("$file"); my @words = map { chomp; $_ } <FIN>; close FIN; #sort words; randomly my @shuffled_words = shuffle(@words); #Instructions: print "This is a game to test how fast you can type these 'words'.\nThe game + ends when you get 5 errors or reach the end of the dictionary\n"; print "You can exit by pressing ctrl-c or command .\n"; print "\n First choose the MAXIMUM number letters you want in the w +ords\n"; print " Then type the word that shows up:\n"; print " After typing, press Enter.\n"; print " Type Enter without any entry to skip the word\n"; print " Don't forget: Case Matters!\n"; #Begin: #Max letters print "\nWhat maximum number of letters do you want?\n\n"; chomp( my $max_length = <> ); #Should check if it's a number? if ($max +_length =~ /\D/) {try again} my $errors = 0; #time start my $starttime = time(); #If don't want sort, use `@words` in loop: foreach (@shuffled_words) { #Stop the Game if ( $errors >= 5 ) { print "Game Over. Too many errors!\n\n"; last; } #Main Type Game: ## redo comes here ## unless ( length $_ >= $max_length ) { print "\nType the word\n '$_': "; chomp( my $try = <STDIN> ); next if ( $try eq '' ); if ( $try ne $_ ) { print "Sorry - That's not right.\n\n"; $errors++; redo; # jump back up to the top of the loop } } } #end time my $endtime = time(); my $totaltime = $endtime - $starttime; #Post game Report print "Game Stats:\n"; print "Max word length: $max_length\n"; print "Your current time is $totaltime s, with $errors errors.\n"; print "\nTry again. You didn't get all the words.\n\n"; #Winner?! print "Wow! You went through the entire dictionary! Try a longer word leng +th!\n\n" unless ( $errors eq 5 ); #Future: #Could implement saving highscores by sending to a file... and testing + against it's contents #Add count for skips #For output: use Term::ANSIColor;

Feel free to add suggestions or comments. This is a learning experience after all!

Replies are listed 'Best First'.
Re: Speed Typing Game
by i5513 (Pilgrim) on Jul 08, 2011 at 19:50 UTC

    Why not adding rank to your game (argh, I see your future note after writting this comment!)?

    I searched in cpan but seems like there are not storable (permanent) rank / scoreboard module for this task !

    There are at least on module (Sort::Rank that could be extended to has a write_to_file function

    I was thinking cpan would have docens of modules like that, but I was not in the true. Maybe I didn't search well ..

    Regards,