mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-29 07:19:23 +01:00
Plugin/WordMorph: minor refactoring/clean-up
This commit is contained in:
parent
3c28dd046a
commit
f71e08cf7f
@ -38,6 +38,8 @@ use constant {
|
|||||||
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> <word2> | solve <solution> | show | hint [from direction] | giveup',
|
USAGE => 'Usage: wordmorph start [steps to solve [word length]] | custom <word1> <word2> | solve <solution> | 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.",
|
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.",
|
DB_UNAVAILABLE => "Word morph database not available.",
|
||||||
|
LEFT => 0,
|
||||||
|
RIGHT => 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
sub wordmorph {
|
sub wordmorph {
|
||||||
@ -66,16 +68,16 @@ sub wordmorph {
|
|||||||
my $direction;
|
my $direction;
|
||||||
|
|
||||||
if (@args == 0) {
|
if (@args == 0) {
|
||||||
$direction = 1;
|
$direction = LEFT;
|
||||||
} elsif ($args[0] eq 'left' || $args[0] eq 'l') {
|
} elsif ($args[0] eq 'left' || $args[0] eq 'l') {
|
||||||
$direction = 1;
|
$direction = LEFT;
|
||||||
} elsif ($args[0] eq 'right' || $args[0] eq 'r') {
|
} elsif ($args[0] eq 'right' || $args[0] eq 'r') {
|
||||||
$direction = -1;
|
$direction = RIGHT;
|
||||||
} else {
|
} else {
|
||||||
return "Unknown direction `$args[0]`; usage: wordmorph hint [from direction]; from direction can be `left` or `right`";
|
return "Unknown direction `$args[0]`; usage: wordmorph hint [from direction]; from direction can be `left` or `right`";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($direction == 1) {
|
if ($direction == LEFT) {
|
||||||
$self->{$channel}->{hintL}++;
|
$self->{$channel}->{hintL}++;
|
||||||
|
|
||||||
if ($self->{$channel}->{hintL} > $#{$self->{$channel}->{morph}}) {
|
if ($self->{$channel}->{hintL} > $#{$self->{$channel}->{morph}}) {
|
||||||
@ -90,50 +92,26 @@ sub wordmorph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
my @hints;
|
my @hints;
|
||||||
|
my $end = $#{$self->{$channel}->{morph}};
|
||||||
my $size = @{$self->{$channel}->{morph}};
|
|
||||||
|
|
||||||
$hints[0] = $self->{$channel}->{morph}->[0];
|
$hints[0] = $self->{$channel}->{morph}->[0];
|
||||||
$hints[$size - 1] = $self->{$channel}->{morph}->[$size - 1];
|
$hints[$end] = $self->{$channel}->{morph}->[$end];
|
||||||
|
|
||||||
for (my $i = 1; $i < $self->{$channel}->{hintL}; $i++) {
|
for (my $i = 1; $i < $self->{$channel}->{hintL}; $i++) {
|
||||||
my $hint = '';
|
|
||||||
|
|
||||||
my $word1 = $self->{$channel}->{morph}->[$i - 1];
|
my $word1 = $self->{$channel}->{morph}->[$i - 1];
|
||||||
my $word2 = $self->{$channel}->{morph}->[$i];
|
my $word2 = $self->{$channel}->{morph}->[$i];
|
||||||
|
$hints[$i] = $self->form_hint($word1, $word2);
|
||||||
for (0 .. length $word1) {
|
|
||||||
if (substr($word1, $_, 1) eq substr($word2, $_, 1)) {
|
|
||||||
$hint .= substr($word1, $_, 1);
|
|
||||||
} else {
|
|
||||||
$hint .= "?";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$hints[$i] = $hint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
my $blank_hint = '_' x length $self->{$channel}->{morph}->[0];
|
my $blank_hint = '_' x length $self->{$channel}->{morph}->[0];
|
||||||
|
|
||||||
for (my $i = $self->{$channel}->{hintL}; $i < $self->{$channel}->{hintR} + 1; $i++) {
|
for (my $i = $self->{$channel}->{hintL}; $i < $self->{$channel}->{hintR} + 1; $i++) {
|
||||||
$hints[$i] = $blank_hint;
|
$hints[$i] = $blank_hint;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (my $i = $#{$self->{$channel}->{morph}} - 1; $i > $self->{$channel}->{hintR}; $i--) {
|
for (my $i = $#{$self->{$channel}->{morph}} - 1; $i > $self->{$channel}->{hintR}; $i--) {
|
||||||
my $hint = '';
|
|
||||||
|
|
||||||
my $word1 = $self->{$channel}->{morph}->[$i];
|
my $word1 = $self->{$channel}->{morph}->[$i];
|
||||||
my $word2 = $self->{$channel}->{morph}->[$i + 1];
|
my $word2 = $self->{$channel}->{morph}->[$i + 1];
|
||||||
|
$hints[$i] = $self->form_hint($word1, $word2);
|
||||||
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);
|
return "Hint: " . join(' > ', @hints);
|
||||||
@ -224,6 +202,8 @@ sub wordmorph {
|
|||||||
return NO_MORPH_AVAILABLE;
|
return NO_MORPH_AVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return DB_UNAVAILABLE if not $self->{db};
|
||||||
|
|
||||||
my @solution = grep { length > 0 } split /\W/, join(' ', @args);
|
my @solution = grep { length > 0 } split /\W/, join(' ', @args);
|
||||||
|
|
||||||
if ($solution[0] ne $self->{$channel}->{word1}) {
|
if ($solution[0] ne $self->{$channel}->{word1}) {
|
||||||
@ -236,23 +216,20 @@ sub wordmorph {
|
|||||||
|
|
||||||
my $last_word = $solution[0];
|
my $last_word = $solution[0];
|
||||||
|
|
||||||
return DB_UNAVAILABLE if not $self->{db};
|
|
||||||
|
|
||||||
if (not exists $self->{db}->{length $last_word}->{$last_word}) {
|
if (not exists $self->{db}->{length $last_word}->{$last_word}) {
|
||||||
return "I do not know this word `$last_word`.";
|
return "I do not know this word `$last_word`.";
|
||||||
}
|
}
|
||||||
|
|
||||||
my $length = length $last_word;
|
my $length = length $last_word;
|
||||||
my $word;
|
|
||||||
|
|
||||||
for (my $i = 1; $i < @solution; $i++) {
|
for (my $i = 1; $i < @solution; $i++) {
|
||||||
$word = $solution[$i];
|
my $word = $solution[$i];
|
||||||
|
|
||||||
if (not exists $self->{db}->{length $word}->{$word}) {
|
if (not exists $self->{db}->{length $word}->{$word}) {
|
||||||
return "I do not know this word `$word`.";
|
return "I do not know this word `$word`.";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (distance($word, $last_word) != 1 || length($word) != $length) {
|
if (length($word) != $length || distance($word, $last_word) != 1) {
|
||||||
return "Wrong. `$word` does not follow from `$last_word`.";
|
return "Wrong. `$word` does not follow from `$last_word`.";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,9 +246,8 @@ sub wordmorph {
|
|||||||
return "Correct! " . join(' > ', @solution);
|
return "Correct! " . join(' > ', @solution);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (@solution < $expected_steps) {
|
# this should never happen ... but just in case
|
||||||
return "Correct! " . join(' > ', @solution) . " is shorter than the expected solution. Congratulations!";
|
return "Correct! " . join(' > ', @solution) . " is shorter than the expected solution. Congratulations!";
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default {
|
default {
|
||||||
@ -309,6 +285,22 @@ sub set_up_new_morph {
|
|||||||
$self->{$channel}->{hintR} = $#{$self->{$channel}->{morph}} - 1;
|
$self->{$channel}->{hintR} = $#{$self->{$channel}->{morph}} - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub form_hint {
|
||||||
|
my ($self, $word1, $word2) = @_;
|
||||||
|
|
||||||
|
my $hint = '';
|
||||||
|
|
||||||
|
for (0 .. length $word1) {
|
||||||
|
if (substr($word1, $_, 1) eq substr($word2, $_, 1)) {
|
||||||
|
$hint .= substr($word1, $_, 1);
|
||||||
|
} else {
|
||||||
|
$hint .= "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $hint;
|
||||||
|
}
|
||||||
|
|
||||||
sub make_morph_by_steps {
|
sub make_morph_by_steps {
|
||||||
my ($self, $db, $steps, $length) = @_;
|
my ($self, $db, $steps, $length) = @_;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ use PBot::Imports;
|
|||||||
# These are set by the /misc/update_version script
|
# These are set by the /misc/update_version script
|
||||||
use constant {
|
use constant {
|
||||||
BUILD_NAME => "PBot",
|
BUILD_NAME => "PBot",
|
||||||
BUILD_REVISION => 4578,
|
BUILD_REVISION => 4579,
|
||||||
BUILD_DATE => "2022-08-31",
|
BUILD_DATE => "2022-08-31",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user