summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjluehrs2 <jluehrs2@uiuc.edu>2008-02-01 01:24:46 -0500
committerjluehrs2 <jluehrs2@uiuc.edu>2008-02-01 01:24:46 -0500
commit752338be7644da462041deea124252c006c68248 (patch)
treefe4e7affd08c60354495807c46c0b1aaab9dc844
parent917b8b98497bf3f685883b01494475c53c2d944b (diff)
downloadgames-word-752338be7644da462041deea124252c006c68248.tar.gz
games-word-752338be7644da462041deea124252c006c68248.zip
add methods to add and remove elements from the word list, and to get the number of words
-rw-r--r--lib/Games/Word/Wordlist.pm34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/Games/Word/Wordlist.pm b/lib/Games/Word/Wordlist.pm
index 51ee9b6..d6045db 100644
--- a/lib/Games/Word/Wordlist.pm
+++ b/lib/Games/Word/Wordlist.pm
@@ -40,6 +40,40 @@ sub new {
return $self;
}
+sub add_words {
+ my $self = shift;
+ my $word_list = shift;
+
+ die "Can't add words to a non-cached word list"
+ unless $self->{cache};
+ if (ref($word_list) eq 'ARRAY') {
+ for (@$word_list) {
+ $self->{word_hash}{$_} = 1;
+ }
+ }
+ else {
+ open my $fh, '<', $word_list or die "Opening $word_list failed";
+ for (<$fh>) {
+ $self->{word_hash}{$_} = 1;
+ }
+ }
+ $self->{word_list} = [keys %{$self->{word_hash}}];
+}
+
+sub remove_words {
+ my $self = shift;
+ for (@_) {
+ delete $self->{word_hash}{$_};
+ }
+ $self->{word_list} = [keys %{$self->{word_hash}}];
+}
+
+sub words {
+ my $self = shift;
+ return unless $self->{cache};
+ return @{$self->{word_list}};
+}
+
sub _random_word_cache {
my $self = shift;
my @word_list = @{ $self->{word_list} };