From 014166ef46e2cf9c0d00159aa8a07d69f8d37c35 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Thu, 13 Jan 2022 20:27:30 -0500 Subject: fix argv --- wordle.pl | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/wordle.pl b/wordle.pl index 21f9716..f6178be 100644 --- a/wordle.pl +++ b/wordle.pl @@ -19,12 +19,13 @@ $| = 1; my @words = read_file("dictionary.txt", chomp => 1); my $answer = ""; -if ($#ARGV < 1) { + +if ($#ARGV < 0) { $answer = $words[int rand@words]; say "fine i will play with myself! I will try to guess... $answer\n"; } else { - say "i will try to guess $answer\n"; $answer = $ARGV[0]; + say "i will try to guess $answer\n"; } my $wlen = length($answer); @@ -46,27 +47,37 @@ my %wl; # incorrect position, avoid relooking for words that match that. my %iwp; +# fill up the incorrect word position hash so it contains keys for each position +# (numeric) and has an array inside it... guarantee theres a better way in perl +# to do this for(0..$wlen-1) { $iwp{$_} = (); } my $guess = ""; + +# keep track of the tries, we must guess a 6 letter word in 6 tries. my $tries = 0; say "picking first word!"; + +# here's our basic loop, take our @words array from the dictionary and filter it +# down based on the results of each 'turn'. Since the wordle game tells you what +# you are right or not, we instead have a "check" function that does the same. while($guess ne $answer ) { - #this can happen if you input a word that doesnt exist in the dictionary.. + #this can happen if you input a word that doesnt exist in the dictionary. if(scalar @words eq 0) { say "I declare a scrabble challenge! Get out your dictionary!"; exit; } - # pull our guess... + # pull our guess from the top of the list. + # should this be a shift? $guess = $words[0]; # optimization for first guess, avoid multiple of same letter, and rank - # letters by importance by weigning the frequency they occour in the english + # letters by importance by weighing the frequency they occour in the english # language and assigning a point value to score that from. unless($tries) { my @good_start_words = grep { uniqletters($_) } @words; @@ -89,10 +100,10 @@ while($guess ne $answer ) { # keep track of how many right letters my $corr = 0; - # get a check results (requivelant to the colors after sumbitting a word on + # get a check results (requivelmnt to the colors after submitting a word on # the wordle site), and parse through them. for my $res (check($guess)) { - # @res = [pos, char, status] + # @res = [word position, char, status (in the word, not in the word, correct)] # if correct, add it to the correct position array if($res->[2] == CORRECT) { @@ -127,6 +138,8 @@ while($guess ne $answer ) { # meaning at pos 0, a b c are in the word but not that position. # and so forth # + # but if we put an "A" in the first position and its in the wrong position + # and in the word, we can eliminate all words that start with A. my $ignore = ""; foreach (sort keys %iwp) { my $posstr = ""; @@ -144,7 +157,8 @@ while($guess ne $answer ) { @words = grep { $_ =~ /$ignore/ } @words; } print "filtering: " . scalar @words; - sleep 1; + # for dramatic effect + # sleep 1; print "..."; @@ -163,7 +177,7 @@ while($guess ne $answer ) { @words = grep { $_ =~ /$pattern/ } @words; } print scalar @words; - sleep 1; + # sleep 1; print "..."; # if our guess has some correct letters, grep @@ -174,7 +188,7 @@ while($guess ne $answer ) { @words = grep { $_ =~ /$pattern/ } @words; } print scalar @words; - sleep 1; + # sleep 1; print "..."; # filter out words with letters on the wrong @@ -192,9 +206,11 @@ say "\nmy final answer is '$guess'\n"; say "it took me $tries/$wlen tries\n"; if($tries <= $wlen) { say "I won!"; + exit 0; } else{ say "Sorry, I lose!"; + exit 1; } # say "remaining:" . scalar @words; # if($corr == $wlen-1) { -- cgit v1.2.1