2024-03-07 12:17:07 +01:00
|
|
|
# File: Wordle.pm
|
|
|
|
#
|
|
|
|
# Purpose: Wordle game. Try to guess a word by submitting words for clues about
|
|
|
|
# which letters belong to the word.
|
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2024 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package PBot::Plugin::Wordle;
|
|
|
|
use parent 'PBot::Plugin::Base';
|
|
|
|
|
|
|
|
use PBot::Imports;
|
|
|
|
|
2024-03-17 01:29:09 +01:00
|
|
|
use Text::Unidecode;
|
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
sub initialize($self, %conf) {
|
|
|
|
$self->{pbot}->{commands}->add(
|
|
|
|
name => 'wordle',
|
|
|
|
help => 'Wordle game! Guess target word by submitting words for clues about which letters belong to the word!',
|
|
|
|
subref => sub { $self->wordle(@_) },
|
|
|
|
);
|
2024-03-17 00:14:07 +01:00
|
|
|
|
|
|
|
$self->{datadir} = $self->{pbot}->{registry}->get_value('general', 'data_dir');
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unload($self) {
|
|
|
|
$self->{pbot}->{commands}->remove('wordle');
|
|
|
|
}
|
|
|
|
|
|
|
|
use constant {
|
2024-03-17 00:14:07 +01:00
|
|
|
USAGE => 'Usage: wordle start [length [wordlist]] | custom <word> <channel> [wordlist] | guess <word> | letters | show | giveup',
|
2024-03-07 12:17:07 +01:00
|
|
|
NO_WORDLE => 'There is no Wordle yet. Use `wordle start` to begin a game.',
|
2024-03-17 00:14:07 +01:00
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
DEFAULT_LENGTH => 5,
|
2024-03-17 00:14:07 +01:00
|
|
|
MIN_LENGTH => 3,
|
|
|
|
MAX_LENGTH => 22,
|
|
|
|
|
2024-03-10 00:42:59 +01:00
|
|
|
LETTER_CORRECT => 1,
|
|
|
|
LETTER_PRESENT => 2,
|
|
|
|
LETTER_INVALID => 3,
|
2024-03-07 12:17:07 +01:00
|
|
|
};
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
my %wordlists = (
|
|
|
|
default => '/wordle/american',
|
|
|
|
american => '/wordle/american',
|
|
|
|
insane => '/wordle/american-insane',
|
|
|
|
british => '/wordle/british',
|
|
|
|
canadian => '/wordle/canadian',
|
|
|
|
french => '/wordle/french',
|
|
|
|
german => '/wordle/german',
|
|
|
|
italian => '/wordle/italian',
|
|
|
|
spanish => '/wordle/spanish',
|
|
|
|
);
|
|
|
|
|
|
|
|
my %color = (
|
|
|
|
correct => "\x0301,03",
|
|
|
|
correct_a => "\x0303,03",
|
|
|
|
|
|
|
|
present => "\x0301,07",
|
|
|
|
present_a => "\x0307,07",
|
|
|
|
|
|
|
|
invalid => "\x0301,15",
|
|
|
|
|
|
|
|
reset => "\x0F",
|
|
|
|
);
|
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
sub wordle($self, $context) {
|
|
|
|
my @args = $self->{pbot}->{interpreter}->split_line($context->{arguments});
|
|
|
|
|
|
|
|
my $command = shift @args;
|
|
|
|
|
|
|
|
if (not length $command) {
|
|
|
|
return USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $channel = $context->{from};
|
|
|
|
|
|
|
|
given ($command) {
|
|
|
|
when ('show') {
|
|
|
|
if (not defined $self->{$channel}->{wordle}) {
|
|
|
|
return NO_WORDLE;
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
return $self->show_wordle($channel, 1);
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
when ('giveup') {
|
|
|
|
if (not defined $self->{$channel}->{wordle}) {
|
|
|
|
return NO_WORDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $wordle = join '', $self->{$channel}->{wordle}->@*;
|
|
|
|
$self->{$channel}->{wordle} = undef;
|
|
|
|
|
|
|
|
return "The word was $wordle. Better luck next time.";
|
|
|
|
}
|
|
|
|
|
|
|
|
when ('start') {
|
2024-03-17 00:14:07 +01:00
|
|
|
if (@args > 2) {
|
|
|
|
return "Invalid arguments; Usage: wordle start [word length [wordlist]]";
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $self->{$channel}->{wordle} && $self->{$channel}->{correct} == 0) {
|
|
|
|
return "There is already a Wordle underway! Use `wordle show` to see the current progress or `wordle giveup` to end it.";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $length = DEFAULT_LENGTH;
|
|
|
|
|
|
|
|
if (defined $args[0]) {
|
|
|
|
if ($args[0] !~ m/^[0-9]+$/ || $args[0] < MIN_LENGTH || $args[0] > MAX_LENGTH) {
|
|
|
|
return "Invalid word length `$args[0]`; must be integer >= ".MIN_LENGTH." and <= ".MAX_LENGTH.".";
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = $args[0];
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
my $wordlist = $args[1] // 'default';
|
|
|
|
|
|
|
|
if (not exists $wordlists{$wordlist}) {
|
|
|
|
return 'Invalid wordlist; options are: ' . (join ', ', sort keys %wordlists);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->make_wordle($channel, $length, undef, $wordlist);
|
2024-03-08 00:59:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
when ('custom') {
|
2024-03-17 00:14:07 +01:00
|
|
|
if (@args < 2 || @args > 3) {
|
|
|
|
return "Usage: wordle custom <word> <channel> [wordlist]";
|
2024-03-08 00:59:46 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 01:29:09 +01:00
|
|
|
my $custom_word = unidecode $args[0];
|
2024-03-17 00:14:07 +01:00
|
|
|
my $custom_channel = $args[1];
|
|
|
|
my $custom_wordlist = $args[2];
|
|
|
|
my $length = length $custom_word;
|
2024-03-08 00:59:46 +01:00
|
|
|
|
|
|
|
if ($length < MIN_LENGTH || $length > MAX_LENGTH) {
|
|
|
|
return "Invalid word length; must be >= ".MIN_LENGTH." and <= ".MAX_LENGTH.".";
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
2024-03-08 00:59:46 +01:00
|
|
|
if (not $self->{pbot}->{channels}->is_active($custom_channel)) {
|
|
|
|
return "I'm not on that channel!";
|
|
|
|
}
|
2024-03-07 12:17:07 +01:00
|
|
|
|
2024-03-08 00:59:46 +01:00
|
|
|
if (defined $self->{$custom_channel}->{wordle} && $self->{$custom_channel}->{correct} == 0) {
|
|
|
|
return "There is already a Wordle underway! Use `wordle show` to see the current progress or `wordle giveup` to end it.";
|
|
|
|
}
|
2024-03-07 12:17:07 +01:00
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
my $wordlist = $custom_wordlist // 'default';
|
|
|
|
|
|
|
|
if (not exists $wordlists{$wordlist}) {
|
|
|
|
return 'Invalid wordlist; options are: ' . (join ', ', sort keys %wordlists);
|
|
|
|
}
|
|
|
|
|
|
|
|
my $result = $self->make_wordle($custom_channel, $length, uc $custom_word, $wordlist);
|
2024-03-07 12:17:07 +01:00
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
if ($result !~ /Guess/) {
|
2024-03-08 00:59:46 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
|
|
|
|
my $message = {
|
|
|
|
nick => $botnick,
|
|
|
|
user => 'wordle',
|
|
|
|
host => 'localhost',
|
|
|
|
hostmask => "$botnick!wordle\@localhost",
|
|
|
|
command => 'wordle',
|
2024-03-10 05:29:25 +01:00
|
|
|
keyword => 'wordle',
|
2024-03-08 00:59:46 +01:00
|
|
|
checkflood => 1,
|
2024-03-17 00:14:07 +01:00
|
|
|
message => "$result (started by $context->{nick})",
|
2024-03-08 00:59:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
$self->{pbot}->{interpreter}->add_message_to_output_queue($custom_channel, $message, 0);
|
|
|
|
|
|
|
|
return "Custom Wordle started!";
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
when ('guess') {
|
|
|
|
if (!@args || @args > 1) {
|
|
|
|
return "Usage: wordle guess <word>";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $self->{$channel}->{wordle}) {
|
|
|
|
return NO_WORDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{$channel}->{correct}) {
|
|
|
|
return "Wordle already solved. " . $self->show_wordle($channel);
|
|
|
|
}
|
|
|
|
|
2024-03-07 17:59:41 +01:00
|
|
|
return $self->guess_wordle($channel, $args[0]);
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
2024-03-10 00:42:59 +01:00
|
|
|
when ('letters') {
|
|
|
|
if (@args > 1) {
|
|
|
|
return "Usage: wordle letters";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $self->{$channel}->{wordle}) {
|
|
|
|
return NO_WORDLE;
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
return $self->show_letters($channel);
|
2024-03-10 00:42:59 +01:00
|
|
|
}
|
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
default {
|
|
|
|
return "Unknown command `$command`; " . USAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
sub load_words($self, $length, $wordlist = 'default') {
|
|
|
|
$wordlist = $self->{datadir} . $wordlists{$wordlist};
|
|
|
|
|
|
|
|
if (not -e $wordlist) {
|
|
|
|
die "Wordle database `" . $wordlist . "` not available. Set WORDLIST to a valid location of a wordlist file.\n";
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 01:29:09 +01:00
|
|
|
open my $fh, '<:encoding(UTF-8)', $wordlist or die "Failed to open Wordle database.";
|
2024-03-07 12:17:07 +01:00
|
|
|
|
|
|
|
my %words;
|
|
|
|
|
2024-03-07 17:59:41 +01:00
|
|
|
while (my $line = <$fh>) {
|
|
|
|
chomp $line;
|
2024-03-17 01:29:09 +01:00
|
|
|
$line = unidecode $line;
|
2024-03-07 12:17:07 +01:00
|
|
|
if (length $line == $length) {
|
|
|
|
$words{uc $line} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close $fh;
|
|
|
|
return \%words;
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
sub make_wordle($self, $channel, $length, $word = undef, $wordlist = 'default') {
|
2024-03-08 00:59:46 +01:00
|
|
|
eval {
|
2024-03-17 00:14:07 +01:00
|
|
|
$self->{$channel}->{words} = $self->load_words($length, $wordlist);
|
2024-03-08 00:59:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
return "Failed to load words: $@";
|
|
|
|
}
|
|
|
|
|
|
|
|
my @wordle;
|
|
|
|
|
|
|
|
if (defined $word) {
|
|
|
|
if (not exists $self->{$channel}->{words}->{$word}) {
|
|
|
|
return "I don't know that word.";
|
|
|
|
}
|
|
|
|
@wordle = split //, $word;
|
|
|
|
} else {
|
|
|
|
my @words = keys $self->{$channel}->{words}->%*;
|
|
|
|
@wordle = split //, $words[rand @words];
|
|
|
|
}
|
|
|
|
|
2024-03-10 00:42:59 +01:00
|
|
|
$self->{$channel}->{wordle} = \@wordle;
|
2024-03-17 00:14:07 +01:00
|
|
|
$self->{$channel}->{guess} = '';
|
2024-03-10 00:42:59 +01:00
|
|
|
$self->{$channel}->{correct} = 0;
|
2024-03-08 00:59:46 +01:00
|
|
|
$self->{$channel}->{guess_count} = 0;
|
2024-03-10 00:42:59 +01:00
|
|
|
$self->{$channel}->{letters} = {};
|
|
|
|
|
|
|
|
foreach my $letter ('A'..'Z') {
|
|
|
|
$self->{$channel}->{letters}->{$letter} = 0;
|
|
|
|
}
|
2024-03-08 00:59:46 +01:00
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
$self->{$channel}->{guess} = $color{invalid};
|
|
|
|
$self->{$channel}->{guess} .= ' ? ' x $self->{$channel}->{wordle}->@*;
|
|
|
|
$self->{$channel}->{guess} .= $color{reset};
|
|
|
|
|
2024-03-17 01:29:09 +01:00
|
|
|
my $language = $wordlist;
|
|
|
|
$language = ucfirst $language;
|
|
|
|
$language = 'American' if $language eq 'Default';
|
|
|
|
|
|
|
|
return $self->show_wordle($channel) . " Guess the $language word! Legend: $color{invalid}X $color{reset} not in word; $color{present}X$color{present_a}?$color{reset} wrong position; $color{correct}X$color{correct_a}*$color{reset} correct position";
|
2024-03-17 00:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub show_letters($self, $channel) {
|
|
|
|
my $result = 'Letters: ';
|
|
|
|
|
|
|
|
foreach my $letter (sort keys $self->{$channel}->{letters}->%*) {
|
|
|
|
if ($self->{$channel}->{letters}->{$letter} == LETTER_CORRECT) {
|
|
|
|
$result .= "$color{correct}$letter$color{correct_a}*";
|
|
|
|
$result .= "$color{reset} ";
|
|
|
|
} elsif ($self->{$channel}->{letters}->{$letter} == LETTER_PRESENT) {
|
|
|
|
$result .= "$color{present}$letter$color{present_a}?";
|
|
|
|
$result .= "$color{reset} ";
|
|
|
|
} elsif ($self->{$channel}->{letters}->{$letter} == 0) {
|
|
|
|
$result .= "$letter ";
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 00:59:46 +01:00
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
return $result . "$color{reset}";
|
2024-03-08 00:59:46 +01:00
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
sub show_wordle($self, $channel, $with_letters = 0) {
|
|
|
|
if ($with_letters) {
|
|
|
|
return $self->{$channel}->{guess} . "$color{reset} " . $self->show_letters($channel);
|
|
|
|
} else {
|
|
|
|
return $self->{$channel}->{guess} . "$color{reset}";
|
|
|
|
}
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub guess_wordle($self, $channel, $guess) {
|
|
|
|
if (length $guess != $self->{$channel}->{wordle}->@*) {
|
|
|
|
return "The length of your guess does not match length of current Wordle. Try again.";
|
|
|
|
}
|
|
|
|
|
2024-03-17 01:29:09 +01:00
|
|
|
$guess = unidecode $guess;
|
2024-03-07 12:17:07 +01:00
|
|
|
$guess = uc $guess;
|
|
|
|
|
|
|
|
if (not exists $self->{$channel}->{words}->{$guess}) {
|
|
|
|
return "I don't know that word. Try again."
|
|
|
|
}
|
|
|
|
|
2024-03-07 19:20:53 +01:00
|
|
|
$self->{$channel}->{guess_count}++;
|
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
my @guess = split //, $guess;
|
|
|
|
my @wordle = $self->{$channel}->{wordle}->@*;
|
|
|
|
|
|
|
|
my %count;
|
|
|
|
my %seen;
|
|
|
|
my %correct;
|
|
|
|
|
|
|
|
for (my $i = 0; $i < @wordle; $i++) {
|
2024-03-07 17:59:41 +01:00
|
|
|
$count{$wordle[$i]}++;
|
|
|
|
$seen{$wordle[$i]} = 0;
|
2024-03-07 18:09:59 +01:00
|
|
|
$correct{$wordle[$i]} = 0 unless exists $correct{$wordle[$i]};
|
2024-03-07 17:59:41 +01:00
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
if ($guess[$i] eq $wordle[$i]) {
|
|
|
|
$correct{$guess[$i]}++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
my $result = '';
|
2024-03-07 17:59:41 +01:00
|
|
|
my $correct = 0;
|
|
|
|
|
2024-03-07 12:17:07 +01:00
|
|
|
for (my $i = 0; $i < @wordle; $i++) {
|
|
|
|
if ($guess[$i] eq $wordle[$i]) {
|
|
|
|
$correct++;
|
2024-03-17 00:14:07 +01:00
|
|
|
$result .= "$color{correct} $guess[$i]$color{correct_a}*";
|
2024-03-10 00:42:59 +01:00
|
|
|
$self->{$channel}->{letters}->{$guess[$i]} = LETTER_CORRECT;
|
2024-03-07 12:17:07 +01:00
|
|
|
} else {
|
|
|
|
my $present = 0;
|
|
|
|
|
|
|
|
for (my $j = 0; $j < @wordle; $j++) {
|
|
|
|
if ($wordle[$j] eq $guess[$i]) {
|
|
|
|
if ($seen{$wordle[$j]} + $correct{$wordle[$j]} < $count{$wordle[$j]}) {
|
|
|
|
$present = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$seen{$wordle[$j]}++;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($present) {
|
2024-03-17 00:14:07 +01:00
|
|
|
$result .= "$color{present} $guess[$i]$color{present_a}?";
|
2024-03-10 00:42:59 +01:00
|
|
|
$self->{$channel}->{letters}->{$guess[$i]} = LETTER_PRESENT;
|
2024-03-07 12:17:07 +01:00
|
|
|
} else {
|
2024-03-17 00:14:07 +01:00
|
|
|
$result .= "$color{invalid} $guess[$i] ";
|
2024-03-10 05:29:25 +01:00
|
|
|
|
|
|
|
if ($self->{$channel}->{letters}->{$guess[$i]} == 0) {
|
|
|
|
$self->{$channel}->{letters}->{$guess[$i]} = LETTER_INVALID;
|
|
|
|
}
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-17 00:14:07 +01:00
|
|
|
$self->{$channel}->{guess} = $result;
|
2024-03-07 12:17:07 +01:00
|
|
|
|
|
|
|
if ($correct == length $guess) {
|
|
|
|
$self->{$channel}->{correct} = 1;
|
2024-03-07 19:20:53 +01:00
|
|
|
my $guesses = $self->{$channel}->{guess_count};
|
2024-03-17 00:14:07 +01:00
|
|
|
return $self->show_wordle($channel) . " Correct in $guesses guess" . ($guesses != 1 ? 'es! ' : '! ');
|
2024-03-07 12:17:07 +01:00
|
|
|
} else {
|
2024-03-17 00:14:07 +01:00
|
|
|
return $self->show_wordle($channel, 1);
|
2024-03-07 12:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|