#! 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, just typing them in. my $file = '/usr/share/dict/words'; open( FIN, '<', $file ) or die("$file"); my @words = map { chomp; $_ } ; 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 words\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 = ); 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 length!\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;