summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjluehrs2 <jluehrs2@uiuc.edu>2008-01-31 05:15:34 -0500
committerjluehrs2 <jluehrs2@uiuc.edu>2008-01-31 05:15:34 -0500
commit9c8d26ee7735c117b664d28915947d39f2593efd (patch)
treede158cdded64b2d19252d9c8318e798c78f10baf
parent7b6e6c87bf10203dabaf007ad155fd29fd550989 (diff)
downloadgames-word-9c8d26ee7735c117b664d28915947d39f2593efd.tar.gz
games-word-9c8d26ee7735c117b664d28915947d39f2593efd.zip
add (very inefficient at the moment) functions for generating random words and for checking to see if a given string is a word. these will be optimized later.
-rw-r--r--lib/Games/Word.pm35
1 files changed, 34 insertions, 1 deletions
diff --git a/lib/Games/Word.pm b/lib/Games/Word.pm
index d9f4801..1abdcf0 100644
--- a/lib/Games/Word.pm
+++ b/lib/Games/Word.pm
@@ -2,13 +2,46 @@
package Games::Word;
require Exporter;
@ISA = qw/Exporter/;
-@EXPORT_OK = qw/random_permutation is_permutation/;
+@EXPORT_OK = qw/random_word is_word set_word_list
+ random_permutation is_permutation/;
use strict;
use warnings;
use Math::Combinatorics qw/factorial/;
use Test::Deep::NoTest;
+my $word_list = '';
+
+sub set_word_list {
+ $word_list = shift;
+}
+
+sub random_word {
+ my $word;
+
+ open my $fh, '<', $word_list
+ or die "Couldn't open word list: $word_list";
+ while (<$fh>) {
+ chomp;
+ $word = $_ if int(rand($.)) == 0;
+ }
+
+ return $word;
+}
+
+sub is_word {
+ my $word = shift;
+
+ open my $fh, '<', $word_list
+ or die "Couldn't open word list: $word_list";
+ while (<$fh>) {
+ chomp;
+ return 1 if $_ eq $word;
+ }
+
+ return 0;
+}
+
sub random_permutation {
my $word = shift;
return '' if $word eq '';