3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 05:49:27 +01:00

Plugin/Wordle: minor refactor

This commit is contained in:
Pragmatic Software 2024-03-07 08:59:41 -08:00
parent 435faaa18c
commit 440a72c71e
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 13 additions and 15 deletions

View File

@ -27,7 +27,7 @@ use constant {
USAGE => 'Usage: wordle start [word length] | guess <word> | show | giveup', USAGE => 'Usage: wordle start [word length] | guess <word> | show | giveup',
NO_WORDLE => 'There is no Wordle yet. Use `wordle start` to begin a game.', NO_WORDLE => 'There is no Wordle yet. Use `wordle start` to begin a game.',
DEFAULT_LENGTH => 5, DEFAULT_LENGTH => 5,
MIN_LENGTH => 4, MIN_LENGTH => 3,
MAX_LENGTH => 10, MAX_LENGTH => 10,
WORDLIST => '/usr/share/dict/words', WORDLIST => '/usr/share/dict/words',
}; };
@ -90,10 +90,10 @@ sub wordle($self, $context) {
return "Failed to load words: $@"; return "Failed to load words: $@";
} }
my @words = keys $self->{$channel}->{words}->%*; my @words = keys $self->{$channel}->{words}->%*;
my @wordle = split //, $words[rand @words]; my @wordle = split //, $words[rand @words];
$self->{$channel}->{wordle} = \@wordle;
$self->{$channel}->{wordle} = \@wordle;
$self->{$channel}->{guesses} = []; $self->{$channel}->{guesses} = [];
$self->{$channel}->{correct} = 0; $self->{$channel}->{correct} = 0;
@ -115,7 +115,7 @@ sub wordle($self, $context) {
return "Wordle already solved. " . $self->show_wordle($channel); return "Wordle already solved. " . $self->show_wordle($channel);
} }
return $self->guess_wordle($channel, uc $args[0]); return $self->guess_wordle($channel, $args[0]);
} }
default { default {
@ -133,7 +133,8 @@ sub load_words($self, $length) {
my %words; my %words;
while (chomp(my $line = <$fh>)) { while (my $line = <$fh>) {
chomp $line;
next if $line !~ /^[a-z]+$/; next if $line !~ /^[a-z]+$/;
if (length $line == $length) { if (length $line == $length) {
@ -167,21 +168,18 @@ sub guess_wordle($self, $channel, $guess) {
my %seen; my %seen;
my %correct; my %correct;
foreach my $letter (@wordle) {
$count{$letter}++;
$seen{$letter} = 0;
$correct{$letter} = 0;
}
my @result;
my $correct = 0;
for (my $i = 0; $i < @wordle; $i++) { for (my $i = 0; $i < @wordle; $i++) {
$count{$wordle[$i]}++;
$seen{$wordle[$i]} = 0;
if ($guess[$i] eq $wordle[$i]) { if ($guess[$i] eq $wordle[$i]) {
$correct{$guess[$i]}++; $correct{$guess[$i]}++;
} }
} }
my @result;
my $correct = 0;
for (my $i = 0; $i < @wordle; $i++) { for (my $i = 0; $i < @wordle; $i++) {
if ($guess[$i] eq $wordle[$i]) { if ($guess[$i] eq $wordle[$i]) {
$seen{$guess[$i]}++; $seen{$guess[$i]}++;

View File

@ -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 => 4707, BUILD_REVISION => 4708,
BUILD_DATE => "2024-03-07", BUILD_DATE => "2024-03-07",
}; };