From 9c8d26ee7735c117b664d28915947d39f2593efd Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Thu, 31 Jan 2008 05:15:34 -0500 Subject: 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. --- lib/Games/Word.pm | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) 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 ''; -- cgit v1.2.3-54-g00ecf