From 3c28dd046a08a11163f9b11f3020ff78b6b678c8 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 31 Aug 2022 18:27:31 -0700 Subject: [PATCH] Plugin/WordMorph: allow hinting from right-hand side --- lib/PBot/Plugin/WordMorph.pm | 97 +++++++++++++++++++++++++----------- lib/PBot/VERSION.pm | 4 +- 2 files changed, 71 insertions(+), 30 deletions(-) diff --git a/lib/PBot/Plugin/WordMorph.pm b/lib/PBot/Plugin/WordMorph.pm index a4519c55..fea424f4 100644 --- a/lib/PBot/Plugin/WordMorph.pm +++ b/lib/PBot/Plugin/WordMorph.pm @@ -35,7 +35,7 @@ sub unload { } use constant { - USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom | solve | show | hint | giveup', + USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom | solve | show | hint [from direction] | giveup', NO_MORPH_AVAILABLE => "There is no word morph available. Use `wordmorph start [steps to solve [word length]]` to create one.", DB_UNAVAILABLE => "Word morph database not available.", }; @@ -59,42 +59,82 @@ sub wordmorph { return NO_MORPH_AVAILABLE; } - $self->{$channel}->{hint}++; + if (@args > 1) { + return 'Usage: wordmorph hint [from direction]; from direction can be `left` or `right`'; + } - if ($self->{$channel}->{hint} > @{$self->{$channel}->{morph}} - 2) { - $self->{$channel}->{hint} = @{$self->{$channel}->{morph}} - 2; + my $direction; + + if (@args == 0) { + $direction = 1; + } elsif ($args[0] eq 'left' || $args[0] eq 'l') { + $direction = 1; + } elsif ($args[0] eq 'right' || $args[0] eq 'r') { + $direction = -1; + } else { + return "Unknown direction `$args[0]`; usage: wordmorph hint [from direction]; from direction can be `left` or `right`"; + } + + if ($direction == 1) { + $self->{$channel}->{hintL}++; + + if ($self->{$channel}->{hintL} > $#{$self->{$channel}->{morph}}) { + $self->{$channel}->{hintL} = $#{$self->{$channel}->{morph}}; + } + } else { + $self->{$channel}->{hintR}--; + + if ($self->{$channel}->{hintR} < 0) { + $self->{$channel}->{hintR} = 0; + } } my @hints; - for (my $i = 0; $i < $self->{$channel}->{hint}; $i++) { + my $size = @{$self->{$channel}->{morph}}; + + $hints[0] = $self->{$channel}->{morph}->[0]; + $hints[$size - 1] = $self->{$channel}->{morph}->[$size - 1]; + + for (my $i = 1; $i < $self->{$channel}->{hintL}; $i++) { my $hint = ''; - if ($i > 0) { - my $word1 = $self->{$channel}->{morph}->[$i - 1]; - my $word2 = $self->{$channel}->{morph}->[$i]; + my $word1 = $self->{$channel}->{morph}->[$i - 1]; + my $word2 = $self->{$channel}->{morph}->[$i]; - for (0 .. length $word1) { - if (substr($word1, $_, 1) eq substr($word2, $_, 1)) { - $hint .= substr($word1, $_, 1); - } else { - $hint .= "?"; - } + for (0 .. length $word1) { + if (substr($word1, $_, 1) eq substr($word2, $_, 1)) { + $hint .= substr($word1, $_, 1); + } else { + $hint .= "?"; } - } else { - $hint = $self->{$channel}->{morph}->[$i]; } - push @hints, $hint; + $hints[$i] = $hint; } - my $hint = '_' x length $self->{$channel}->{morph}->[0]; + my $blank_hint = '_' x length $self->{$channel}->{morph}->[0]; - for (my $i = $self->{$channel}->{hint}; $i < @{$self->{$channel}->{morph}} - 1; $i++) { - push @hints, $hint; + for (my $i = $self->{$channel}->{hintL}; $i < $self->{$channel}->{hintR} + 1; $i++) { + $hints[$i] = $blank_hint; } - push @hints, $self->{$channel}->{morph}->[@{$self->{$channel}->{morph}} -1]; + for (my $i = $#{$self->{$channel}->{morph}} - 1; $i > $self->{$channel}->{hintR}; $i--) { + my $hint = ''; + + my $word1 = $self->{$channel}->{morph}->[$i]; + my $word2 = $self->{$channel}->{morph}->[$i + 1]; + + for (0 .. length $word1) { + if (substr($word1, $_, 1) eq substr($word2, $_, 1)) { + $hint .= substr($word1, $_, 1); + } else { + $hint .= "?"; + } + } + + $hints[$i] = $hint; + } return "Hint: " . join(' > ', @hints); } @@ -194,10 +234,7 @@ sub wordmorph { push @solution, $self->{$channel}->{word2}; } - my $i = 0; - - my $last_word = $solution[$i]; - my $word; + my $last_word = $solution[0]; return DB_UNAVAILABLE if not $self->{db}; @@ -205,14 +242,17 @@ sub wordmorph { return "I do not know this word `$last_word`."; } - for ($i = 1; $i < @solution; $i++) { + my $length = length $last_word; + my $word; + + for (my $i = 1; $i < @solution; $i++) { $word = $solution[$i]; if (not exists $self->{db}->{length $word}->{$word}) { return "I do not know this word `$word`."; } - if (distance($word, $last_word) != 1 || length($word) != length($last_word)) { + if (distance($word, $last_word) != 1 || length($word) != $length) { return "Wrong. `$word` does not follow from `$last_word`."; } @@ -265,7 +305,8 @@ sub set_up_new_morph { my ($self, $channel) = @_; $self->{$channel}->{word1} = $self->{$channel}->{morph}->[0]; $self->{$channel}->{word2} = $self->{$channel}->{morph}->[$#{$self->{$channel}->{morph}}]; - $self->{$channel}->{hint} = 1; + $self->{$channel}->{hintL} = 1; + $self->{$channel}->{hintR} = $#{$self->{$channel}->{morph}} - 1; } sub make_morph_by_steps { diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 5c2231d8..85b3c826 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4577, - BUILD_DATE => "2022-08-26", + BUILD_REVISION => 4578, + BUILD_DATE => "2022-08-31", }; sub initialize {}